answersLogoWhite

0

How do you declare a stack in c?

Updated: 8/11/2023
User Avatar

Wiki User

15y ago

Best Answer

# #include

# #include

#

# const int TPILA=5; // TPILA es el valor maximo de elementos

# // que puede tener nuestra pila

#

# class PILA{

# public:

# int mipila[TPILA]; // Crea mi PILA de tamano TPILA

# int apilados; // Numero de objetos en LA PILA

# void reset(); // Vacia LA PILA

# void push(int v); // Agrega Valores en el tope de la PILA

# int pop(); // Retorna y elimina el tope de la PILA

# };

#

# void PILA::reset() // Vacia la PILA

# {

# apilados=0;

# }

#

# //Se Agrega(PUSH) un valor en el tope de la pila

# void PILA::push(int v)

# {

# // Comprueba que haya espacio dentro de la pila

# // para poder agregar el nuevo valor

# if(apilados

# {

# mipila[apilados++]=v;

# }

# }

#

# // Se Elimina (POP) el ultimo valor de la pila

# // y retorna el nuevo tope

# int PILA::pop()

# {

# if(apilados>0)

# {

# cout<<"El valor del tope eliminado era: ";

# // Retorna el valor del tope que fue eliminado

# return(mipila[--apilados]);

# }

# else

# cout<<"No existen datos para eliminar. ERROR ";

# return (0);

# }

#

# main()

# {

# PILA stack;

# stack.reset();

# int opc, i, dato;

# char out;

# do

# {

# do

# {

# clrscr();

# cout<<"El siguiente programa simula el funcionamiento de"<

# cout<<"una pila y es capaz de apilar un max. de 5 Valores"<

# cout<<"numericos enteros, para fines practicos\n\n";

# cout<<"Que operacion desea Realizar\n\n";

# cout<<"1.- Insertar un dato\n";

# cout<<"2.- Borrar un dato\n";

# cout<<"3.- Mostrar pila\n";

# cout<<"4.- Resetear Pila\n";

# cout<<"5.- Salir";

# cout<<"\n\n\nOpcion(1-5): ";

# cin>>opc;

# }

# while(opc>5);

#

# switch(opc)

# {

# case(1):

# if(stack.apilados

# {

# cout<<"dato?: ";

# cin>>dato;

# stack.push(dato);

# }

# else

# cout<<"ERROR! PILA LLENA!";

# cout<<"\n\nSalir?? (S/N): "; cin>>out;

# break;

# case(2):

# cout<

# cout<<"\n\nSalir?? (S/N): "; cin>>out;

# break;

# case(3):

# if(stack.apilados!=0)

# {

# cout<<"Los datos en la pila actualmente son: "<

# for(i=0;i

# { cout<

# }

# else

# cout<<"No existen datos en la pila"<

# cout<<"\n\nSalir?? (S/N): "; cin>>out;

# break;

# case(4):

# stack.reset();

# cout<<"\n\nSalir?? (S/N): "; cin>>out;

# break;

# case(5):

# out='s'; break;

# default:

# out='s'; break;

# }

# }

# while(out=='n' out=='N');

# }

User Avatar

Wiki User

15y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

15y ago

There are not Stack implementation in standard C libraries. In order to get stack functionality you will need to write your own implementation.

I suggest to read you web links attached to this question for detailed information about stack and it's implementation in C.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How do you declare a stack in c?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

How do you refresh stack using library functions in C programming?

What do you mean by stack-refreshing? Anyway, there are no stack handling functions in the standard C library.


Why you will declare top as -1 for stacks to be empty in c program?

You would do this if you implement a stack using an array. Using a zero-based index to keep track of the top of the stack (the end of the array) means we must use the value -1 to indicate an empty stack. This is because an array of n elements will have indices 0 through n-1, where index n-1 represents the element at top of the stack. An empty stack has 0 elements, therefore the top of the stack is represented by index -1.


How you use stack in c plus plus classes?

It would be easier to manipulate the stack in assembly language rather than C++.


Where local variables are stored in c?

On the stack.


Write a c program to perform stack operation?

int top=-1; int stack[10];


What has the author Raymond C Stack written?

Raymond C. Stack has written: 'Marketing Montana hay' -- subject- s -: Hay trade, Statistics, Hay


Can you declare a method within a method in c or c plus plus?

C: there are no methods in C. C++: no.


What is flowchart of stack using array in c prog?

Two little problems: 1. stack doesn't have a flow-chart 2. there are no flow-charts in a C program


How do you declare pointer in c?

ujkjkyjljlui kukhjkui


What you declare in the function prototype in c?

yes


C program to implement tower of hanoi using array implementation of stack abstract datatype?

stack abstract datatype


Stack c language program of palindrome?

what ever you assume