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<iostream>
2using namespace std;
3#define Max 100
4class stack{
5 public:
6 int top;
7 int size;
8 int *s;
9 int stack[Max];
10
11 void push()
12 {
13 int value;
14 if(top==size-1)
15 {
16 cout<<"overflow";
17 }
18 else
19 {
20 cout<<"Enter value to push \n";
21 cin>>value;
22 top++;
23 stack[top]=value;
24 }
25 }
26 int pop()
27 {
28 if(top==-1)
29 {
30 cout<<"Underflow";
31 }
32 else
33 {
34 cout<<"Deleted value is \n"<<stack[top];
35 top--;
36 }
37 }
38 void display()
39 {
40 int i;
41 for(i=top;i>=0;i--)
42 {
43 cout<<stack[i]<<endl;
44 }
45 }
46};
47int main()
48{
49 stack st;
50 cout<<"Enter the size of the stack";
51 cin>>st.size;
52 st.s=new int[st.size];
53 st.top=-1;
54 int ch;
55 while(st.size!=0)
56 {
57 cout<<endl<<" ##### STACK MENU ##### "<<endl;
58 cout<<"1. PUSH OPERATION \n2. POP OPERATION \n3. DISPLAY \n4.Exit \n";
59 cin>>ch;
60 switch(ch)
61 {
62 case 1:
63 st.push();
64 break;
65 case 2:
66 st.pop();
67 break;
68 case 3:
69 st.display();
70 break;
71 case 4:
72 exit(0);
73
74 default:cout<<"\n Choose correct option";
75 }
76}
77return 0;
78}
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}