C++ program for implementing a stack using arrays. It involves various operations such as push, pop, stack empty, stack full and display.
C++ program to implement stack using arrays
#include<stdio.h>
#include<iostream>
using namespace std;
class impstack
{
int top,a[10];
public:
impstack()
{
top=-1;
}
void pop()
{
if(top<0)
cout<<"Stack is Underflow"<<endl;
else
{
cout<<"Poped element is "<<a[top]<<endl;
top--;
}
}
void push()
{
if(top<10)
{
top++;
cout<<"Enter the element to be pushed : ";
cin>>a[top];
}
else
cout<<"Stack is overflow"<<endl;
}
void display()
{
for(int i=top;i>=0;i--)
cout<<a[i]<<" ";
cout<<endl;
}
};
int main()
{
impstack stack;
int ch,flag=1;
while(flag)
{
cout<<"Enter your choice\n1.)Push\n2.)Pop\n3.)Display\n4.)Exit\nEnter your choice ";
cin>>ch;
switch(ch)
{
case 1:
stack.push();
break;
case 2:
stack.pop();
break;
case 3:
stack.display();
break;
case 4:
flag=0;
break;
default:
cout<<"Wrong choice";
}
}
}
#include<iostream>
using namespace std;
class impstack
{
int top,a[10];
public:
impstack()
{
top=-1;
}
void pop()
{
if(top<0)
cout<<"Stack is Underflow"<<endl;
else
{
cout<<"Poped element is "<<a[top]<<endl;
top--;
}
}
void push()
{
if(top<10)
{
top++;
cout<<"Enter the element to be pushed : ";
cin>>a[top];
}
else
cout<<"Stack is overflow"<<endl;
}
void display()
{
for(int i=top;i>=0;i--)
cout<<a[i]<<" ";
cout<<endl;
}
};
int main()
{
impstack stack;
int ch,flag=1;
while(flag)
{
cout<<"Enter your choice\n1.)Push\n2.)Pop\n3.)Display\n4.)Exit\nEnter your choice ";
cin>>ch;
switch(ch)
{
case 1:
stack.push();
break;
case 2:
stack.pop();
break;
case 3:
stack.display();
break;
case 4:
flag=0;
break;
default:
cout<<"Wrong choice";
}
}
}
C++ program to implement stack using linked list
#include<stdio.h>
#include<iostream>
using namespace std;
struct links
{
int data;
links *next;
};
class impstack
{
links *top;
public:
impstack()
{
top=NULL;
}
void pop()
{
links *temp;
if(top==NULL)
cout<<"Stack is Underflow"<<endl;
else
{
cout<<"Popped element is "<<top->data<<endl;
temp=top;
top=top->next;
delete temp;
}
}
void push()
{
links *ne;
ne=new links;
if(ne!=NULL)
{
ne->next=top;
top=ne;
cout<<"Enter the element to be pushed : ";
cin>>top->data;
}
else
cout<<"Stack is overflow"<<endl;
}
void display()
{
links *temp=top;
while(temp!=NULL)
{
cout<<temp->data<<" ";
temp=temp->next;
}
cout<<"NULL"<<endl;
}
};
int main()
{
impstack stack;
int ch,flag=1;
while(flag)
{
cout<<"Enter your choice\n1.)Push\n2.)Pop\n3.)Display\n4.)Exit\nEnter your choice ";
cin>>ch;
switch(ch)
{
case 1:
stack.push();
break;
case 2:
stack.pop();
break;
case 3:
stack.display();
break;
case 4:
flag=0;
break;
default:
cout<<"Wrong choice"<<endl;
}
}
}
#include<iostream>
using namespace std;
struct links
{
int data;
links *next;
};
class impstack
{
links *top;
public:
impstack()
{
top=NULL;
}
void pop()
{
links *temp;
if(top==NULL)
cout<<"Stack is Underflow"<<endl;
else
{
cout<<"Popped element is "<<top->data<<endl;
temp=top;
top=top->next;
delete temp;
}
}
void push()
{
links *ne;
ne=new links;
if(ne!=NULL)
{
ne->next=top;
top=ne;
cout<<"Enter the element to be pushed : ";
cin>>top->data;
}
else
cout<<"Stack is overflow"<<endl;
}
void display()
{
links *temp=top;
while(temp!=NULL)
{
cout<<temp->data<<" ";
temp=temp->next;
}
cout<<"NULL"<<endl;
}
};
int main()
{
impstack stack;
int ch,flag=1;
while(flag)
{
cout<<"Enter your choice\n1.)Push\n2.)Pop\n3.)Display\n4.)Exit\nEnter your choice ";
cin>>ch;
switch(ch)
{
case 1:
stack.push();
break;
case 2:
stack.pop();
break;
case 3:
stack.display();
break;
case 4:
flag=0;
break;
default:
cout<<"Wrong choice"<<endl;
}
}
}
No comments:
Post a Comment