0% found this document useful (0 votes)
0 views

Stack

.........................

Uploaded by

Shruti Gupta
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views

Stack

.........................

Uploaded by

Shruti Gupta
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

#include<iostream.

h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
#define maxs 5
int top=-1;
int s_arr[maxs];
void push()
{
int item;
if(top==maxs-1)
cout<<"\n Stack overflow";
else
{
cout<<"\n Enter the item to be pushed into the stack";
cin>>item;
top=top+1;
s_arr[top]=item;
}
}
void pop()
{
if (top==-1)
cout<<"\n Stack underflow";
else
{
cout<<"\n Popped item is : "<<s_arr[top];
top = top-1;
}
}
void display()
{
int i;
if(top==-1)

cout<<"\n Stack is empty";


else
{
cout<<"\n Stack elements are : ";
for(i=top;i>=0;i--)
cout<<s_arr[i];
}
}
struct stack
{
int n;
struct stack *next;
} *tops=NULL;
typedef struct stack st;
void pushd()
{
st *node;
node=(st *) malloc(sizeof(st));
cout<<"\n Enter a number";
cin>>node->n;
node->next=tops;
tops=node;
}
int popd()
{
st *temp;
temp=tops;
if(tops==NULL)
{
cout<<"\n Stack is empty";
}
else
{
tops=tops->next;
free(temp);
}
return 0;
}
void displayd()
{
st *temp;
temp=tops;
while(temp->next!=NULL)
{
cout<<"\n Number= "<<temp->n;
temp=temp->next;
}
cout<<"\n Number= "<<temp->n;
}
char str[20];
void rev(char str[])
{
int l,i;
cout<<"\n Reverse : ";
l=strlen(str);
for(i=l-1;i>=0;i--)
cout<<str[i];
}
void palind(char str[])
{
int l=0,f=1;
int h=strlen(str)-1;
while(h>l)
{
if(str[l++]!=str[h--])
f=0;
}
if(f==1)
cout<<str<<" is a palindrome string\n";
else
cout<<str<<" is not a palindrome string\n";
}
void main()
{
int ch1,ch2,y,x=0,z=1,a,r=1,s,q=1;
clrscr();
do
{
ti:
cout<<"\n Enter 1 for Static implementation\n Enter 2 for Dynamic implementation \n Enter
3 for application \n enter 4 for exit\n";
cout<<"Please enter your choice";
cin>>ch1;
switch(ch1)
{
case 1:
while(z==1)
{
cout<<"Static\n";
cout<<"Enter 1 for Push\n 2 for Pop\n 3 for Display\n 4 for Exit Static\n";
cin>>ch2;
switch(ch2)
{
case 1:
push();
break;
case 2:
pop();
break;
case 3:
display();
break;
case 4:
goto ti;
default:
cout<<"\n Wrong choice";
}
cout<<"\n Do you wish to continue for Static implementation ?(Enter 0 to
continue/0 to exit)";
cin>>z;
}
break;
case 2:
while(r==1)
{
cout<<"Dynamic\n ";
cout<<"\nEnter 1 to push\n Enter 2 to pop \n Enter 3 to display\n Enter 4 to exit Dynamic\n";
cout<<"Enter your choice";
cin>>a;
switch(a)
{
case 1:
pushd();
break;
case 2:
popd();
break;
case 3:
displayd();
break;
case 4:
goto ti;
default:
cout<<"\n Wrong choice";
}
cout<<"\n Do you wish to continue for Dynamic implementation ?(Enter 1 to
continue/0 to exit)\n";
cin>>r;
}
break;
case 3:
while(q==1)
{
cout<<"Application\n";
cout<<"Enter a string";
cin>>str;
cout<<"\n Enter 1 to find Reverse of a string\n Enter 2 to check whether the given string
is palindrome or not\n Enter 3 to exit Application\n";
cout<<"Enter your choice";
cin>>s;
switch(s)
{
case 1:
rev(str);
break;
case 2:
palind(str);
break;
case 3:
goto ti;
default:
cout<<"\n Wrong choice";
}
cout<<"\n Do you wish to continue for Application implementation ? (Enter 1 to
continue/0 to exit)";
cin>>q;
}
break;
case 4:
exit(1);
default:
cout<<"\n Wrong choice";
}
cout<<"\nDo you wish to continue ?(Enter 1 to continue and 0 to exit)";
cin>>x;
}
while(x==1)
}

You might also like