1#include <iostream>
2using namespace std;
3int stack[100], n=100, top=-1;
4void push(int val) {
5 if(top>=n-1)
6 cout<<"Stack Overflow"<<endl;
7 else {
8 top++;
9 stack[top]=val;
10 }
11}
12void pop() {
13 if(top<=-1)
14 cout<<"Stack Underflow"<<endl;
15 else {
16 cout<<"The popped element is "<< stack[top] <<endl;
17 top--;
18 }
19}
20void display() {
21 if(top>=0) {
22 cout<<"Stack elements are:";
23 for(int i=top; i>=0; i--)
24 cout<<stack[i]<<" ";
25 cout<<endl;
26 } else
27 cout<<"Stack is empty";
28}
29int main() {
30 int ch, val;
31 cout<<"1) Push in stack"<<endl;
32 cout<<"2) Pop from stack"<<endl;
33 cout<<"3) Display stack"<<endl;
34 cout<<"4) Exit"<<endl;
35 do {
36 cout<<"Enter choice: "<<endl;
37 cin>>ch;
38 switch(ch) {
39 case 1: {
40 cout<<"Enter value to be pushed:"<<endl;
41 cin>>val;
42 push(val);
43 break;
44 }
45 case 2: {
46 pop();
47 break;
48 }
49 case 3: {
50 display();
51 break;
52 }
53 case 4: {
54 cout<<"Exit"<<endl;
55 break;
56 }
57 default: {
58 cout<<"Invalid Choice"<<endl;
59 }
60 }
61 }while(ch!=4);
62 return 0;
63}
1#include <stdio.h>
2
3int MAXSIZE = 8;
4int stack[8];
5int top = -1;
6
7int isempty() {
8
9 if(top == -1)
10 return 1;
11 else
12 return 0;
13}
14
15int isfull() {
16
17 if(top == MAXSIZE)
18 return 1;
19 else
20 return 0;
21}
22
23int peek() {
24 return stack[top];
25}
26
27int pop() {
28 int data;
29
30 if(!isempty()) {
31 data = stack[top];
32 top = top - 1;
33 return data;
34 } else {
35 printf("Could not retrieve data, Stack is empty.\n");
36 }
37}
38
39int push(int data) {
40
41 if(!isfull()) {
42 top = top + 1;
43 stack[top] = data;
44 } else {
45 printf("Could not insert data, Stack is full.\n");
46 }
47}
48
49int main() {
50 // push items on to the stack
51 push(3);
52 push(5);
53 push(9);
54 push(1);
55 push(12);
56 push(15);
57
58 printf("Element at top of the stack: %d\n" ,peek());
59 printf("Elements: \n");
60
61 // print stack data
62 while(!isempty()) {
63 int data = pop();
64 printf("%d\n",data);
65 }
66
67 printf("Stack full: %s\n" , isfull()?"true":"false");
68 printf("Stack empty: %s\n" , isempty()?"true":"false");
69
70 return 0;
71}
1typedef struct Nodo{
2 Elem val;
3 struct Nodo *next;
4} *Stack;
5Stack Empty(){return NULL;}
6bool IsEmpty(Stack a){return a==NULL;}
7Elem Top(Stack a){return a->val;}
8Stack Pop(Stack l){return l->next;}
9Stack Push(Elem x,Stack res){
10 Stack nuevo=(Stack)malloc(sizeof(struct Nodo));
11 nuevo->val=x;
12 nuevo->next=res;
13 return nuevo;
14}
1#include <iostream>
2using namespace std;
3int top = -1; //Globally defining the value of top as the stack is empty
4
5 void push (int stack[ ] , int x , int n)
6 {
7 if ( top == n-1 ) //If the top position is the last of position of the stack, this means that the stack is full.
8 {
9 cout << "Stack is full.Overflow condition!" ;
10 }
11 else
12 {
13 top = top +1 ; //Incrementing the top position
14 stack[ top ] = x ; //Inserting an element on incremented position
15 }
16 }
17 bool isEmpty ( )
18 {
19 if ( top == -1 ) //Stack is empty
20 return true ;
21 else
22 return false;
23 }
24 void pop ( )
25 {
26
27 if( isEmpty ( ) )
28 {
29 cout << "Stack is empty. Underflow condition! " << endl ;
30 }
31 else
32 {
33 top = top - 1 ; //Decrementing top’s position will detach last element from stack
34 }
35 }
36 int size ( )
37 {
38 return top + 1;
39 }
40 int topElement (int stack[])
41 {
42 return stack[ top ];
43 }
44 //Let's implement these functions on the stack given above
45
46 int main( )
47 {
48 int stack[ 3 ];
49 // pushing element 5 in the stack .
50 push(stack , 5 , 3 ) ;
51
52 cout << "Current size of stack is " << size ( ) << endl ;
53
54 push(stack , 10 , 3);
55 push (stack , 24 , 3) ;
56
57 cout << "Current size of stack is " << size( ) << endl ;
58
59 //As the stack is full, further pushing will show an overflow condition.
60 push(stack , 12 , 3) ;
61
62 //Accessing the top element
63 cout << "The current top element in stack is " << topElement(stack) << endl;
64
65 //Removing all the elements from the stack
66 for(int i = 0 ; i < 3;i++ )
67 pop( );
68 cout << "Current size of stack is " << size( ) << endl ;
69
70 //As the stack is empty , further popping will show an underflow condition.
71 pop ( );
72
73 }