C++ Stack Implementation, Exercises of Data Structures and Algorithms

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

2011/2012

Uploaded on 07/30/2012

dhanvantari
dhanvantari 🇮🇳

2

(2)

45 documents

1 / 2

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
#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++;
docsity.com
pf2

Partial preview of the text

Download C++ Stack Implementation and more Exercises Data Structures and Algorithms in PDF only on Docsity!

#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++;

docsity.com

void pop (int *s,int to) { if(to<0) { cout<<"The stack is empty\n"; } else { to=to-1; s--; } }

docsity.com