

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
The implementation of a stack data structure in c++ using two pointers. The user can choose to push, pop, or display the elements of the stack. The stack has a capacity of 10 elements.
Typology: Exercises
1 / 2
This page cannot be seen from the preview
Don't miss anything!


#include<conio.h> #include<iostream.h> void push (int,int); void pop (int,int);
void main () { int choice; int stack[10],top=-1; clrscr(); cout<<"Enter Your choice\n\t--> To push enter:\t1\n\t--> To pop enter:\t2\n\t--> To Display enter:\t3\n\t--> To exit enter:\t4\n"; cin>>choice; while (choice!=4) { switch(choice) { case 1: push(&stack[top+1],&top); break;
case 2: pop(&stack[top],&top); break;
case 3: for (int i=0;i<=top;i++) { cout<<stack[i]<<" "; } cout<<"\n"; break;
default: cout<<"Wrong choice!!! please enter again\n"; break; } cout<<"\nEnter choice:\t"; cin>>choice; } }
void push (int *st,int t) { int input; if (t>8) { cout<<"\nThe stack is full\n"; } else { cout<<"Enter the number you want to push:\t"; cin>>input; t=t+1; *st=input; st++;
void pop (int *s,int to) { if(to<0) { cout<<"The stack is empty\n"; } else { to=to-1; s--; } }