//stack implementation
#include<iostream>
#include<cstdlib>
using namespace std;
void push(),traversal(),pop();
int size,front=-1,*stack; //global declaration
int main()
{
int choise;
cout<<"What is size of tha stack: "; cin>>size;
stack=(int*)malloc(size*sizeof(int));
do
{
cout<<"\n1-INSERT\n2-DELETE\n3-TRAVERSAL\n4-EXIT\nINPUT CHOISE: "; cin>>choise;
switch(choise)
{
case 1: push(); break;
case 2: pop(); break;
case 3: traversal(); break;
case 4: exit(0);
default:cout<<"INVALID CHOICE"; break;
}
}while(1);
}
void push()
{
int item; char ch;
if(front<size-1)
{
front++;
cout<<"Insert item: "; cin>>item;
stack[front]=item;
}
else
{
cout<<"Stack is overflow,do you want increament the size of stack y/n: "; cin>>ch;
if(ch=='y')
{
size+=size; //increament size double
stack=(int*)realloc(stack,size*sizeof(int));
cout<<"Now size of stack is "<<size<<"\n";
}
else
cout<<"Your choise is NO !\n";
}
}
void pop()
{
if(front==-1)
cout<<"Satck is underflow\n";
else
{
int temp=stack[front];
stack[front]='\0';
cout<<temp<<" at index no "<<front+1<<" has been deleted !\n";
front--;
}
}
void traversal()
{
if(front==-1)
cout<<"STACK IS EMPTY, NO ELEMENT TO TRAVERSE\n";
else
{
cout<<"STACK IS: ";
for(int i=front;i>=0;i--)
cout<<"\n| "<<stack[i]<<" |";
cout<<"\n";
}
}
#include<iostream>
#include<cstdlib>
using namespace std;
void push(),traversal(),pop();
int size,front=-1,*stack; //global declaration
int main()
{
int choise;
cout<<"What is size of tha stack: "; cin>>size;
stack=(int*)malloc(size*sizeof(int));
do
{
cout<<"\n1-INSERT\n2-DELETE\n3-TRAVERSAL\n4-EXIT\nINPUT CHOISE: "; cin>>choise;
switch(choise)
{case 1: push(); break;
case 2: pop(); break;
case 3: traversal(); break;
case 4: exit(0);
default:cout<<"INVALID CHOICE"; break;
}
}while(1);
}
void push()
{
int item; char ch;
if(front<size-1)
{
front++;
cout<<"Insert item: "; cin>>item;
stack[front]=item;
}
else
{
cout<<"Stack is overflow,do you want increament the size of stack y/n: "; cin>>ch;
if(ch=='y')
{
size+=size; //increament size double
stack=(int*)realloc(stack,size*sizeof(int));
cout<<"Now size of stack is "<<size<<"\n";
}
else
cout<<"Your choise is NO !\n";
}
}
void pop()
{
if(front==-1)
cout<<"Satck is underflow\n";
else
{
int temp=stack[front];
stack[front]='\0';
cout<<temp<<" at index no "<<front+1<<" has been deleted !\n";
front--;
}
}
void traversal()
{
if(front==-1)
cout<<"STACK IS EMPTY, NO ELEMENT TO TRAVERSE\n";
else
{
cout<<"STACK IS: ";
for(int i=front;i>=0;i--)
cout<<"\n| "<<stack[i]<<" |";
cout<<"\n";
}
}
nice code
ReplyDeleteThanks for visiting
Deletenicecoding
ReplyDelete