You are on page 1of 12

Stack using Array

#include<iostream.h>
#include<process.h>
#include<conio.h>
class stack
{
int top,stk[5];
public:
stack()
{
top=-1;
}
void push();
void pop();
void display();
};
stack s;
void stack::push()
{
top++;
if(top==5)
{
cout<<"Overflow";
top--;
return;
}
cout<<"Enter the no";
cin>>stk[top];
cout<<"\nInserted";
}
void stack::pop()
{
if(top==-1)
{
cout<<"\n Under flow";
return;
}
cout<<stk[top]<<"deleted";
top--;
}
void stack::display()
{
if(top==-1)
{
cout<<"\n Under flow";
return;
}
for(int i=top;i>=0;i--)
cout<<stk[i];
}
void main()
{
clrscr();
int opt;
while(1)
{
cout<<"\nChosse \n1.push\n"<<"2.pop\n"<<"3.display\n"<<"4. Exit\n";
cout<<"Please enter your choice: ";
cin>>opt;
switch(opt)
{
case 1:
s.push();
break;

case 2:
s.pop();
break;

case 3:
s.display();
break;
case 4:exit(0);
break;
default :
cout<<"An Invalid Choice!!!\n";
}
}
}

Stack Using Linked List 1


#include<iostream.h>
#include<process.h>
#include<stdio.h>
#include<conio.h>
struct node
{
int roll,age;
char name[20];
node *next;
};
class stack
{
node *top;
public:
stack() // constructor
{
top=NULL;
}
void push(); // to insert an element
void pop(); // to delete an element
void show(); // to show the element
};
// PUSH Operation
void stack::push()
{
node *t=new node;
cout<<"\n Enter Roll number to insert: ";
cin>>t->roll;
cout<<"Enter Age:";
cin>>t->age;
cout<<"Enter Name:";
gets(t->name);
t->next=NULL;
if(top==NULL)
top=t;
else
{
t->next=top;
top=t;
}
cout<<"\n New item is inserted to the stack!!!";
}
void stack::pop()
{
node *t=new node;
if(top==NULL)
{
cout<<"\nThe stack is empty!!!";
}
else
{
t=top;
top=top->next;
cout<<"\nDeleted value is "<<t->roll;
delete t;
}
}
void stack::show()
{
node *t = new node;
if(top==NULL)
cout<<"Underflow";
else
{
cout<<"\nThe stack is\n";
while(t!=NULL)
{
t=top;
cout<<t->roll<<" "<<t->age<<" "<<t->name;
t=t->next;
}
}
}
void main()
{
clrscr();
stack s;
int choice;
while(1)
{
cout<<"\n Main Menu";
cout<<"\n1:PUSH\n2:POP\n3:DISPLAY \n4:EXIT";
cout<<"\nEnter your choice(1-4): ";
cin>>choice;
switch(choice)
{
case 1:
s.push();
break;
case 2:
s.pop();
break;
case 3:
s.show();
break;
case 4:exit(0);
break;
default:
cout<<"\nPlease enter correct choice(1-4)!!";
break;
}
}
}

Stack using lInked list 2:


#include<iostream.h>
#include<process.h>
#include<stdio.h>
#include<conio.h>
struct node
{
int ticketno;
char pname[20];
node *next;
};
class stack
{
node *top;
public:
stack() // constructor
{
top=NULL;
}
void push(); // to insert an element
void pop(); // to delete an element
void show(); // to show the element
};
// PUSH Operation
void stack::push()
{
node *t=new node;
cout<<"\n Enter Ticket No: ";
cin>>t->ticketno;
cout<<"Enter Name:";
gets(t->pname);
t->next=NULL;
if(top==NULL)
top=t;
else
{
t->next=top;
top=t;
}
cout<<"\n New item is inserted to the stack!!!";
}
void stack::pop()
{
node *t=new node;
if(top==NULL)
{
cout<<"\nThe stack is empty!!!";
}
else
{
t=top;
top=top->next;
cout<<"\nDeleted value is "<<t->ticketno;
delete t;
}
}
void stack::show()
{
node *t = new node;
if(top==NULL)
cout<<"Underflow";
else
{
cout<<"\nThe stack is\n";
while(t!=NULL)
{
t=top;
cout<<t->ticketno<<" "<<t->pname;
t=t->next;
}
}
}
void main()
{
clrscr();
stack s;
int choice;
while(1)
{
cout<<"\n Main Menu";
cout<<"\n1:PUSH\n2:POP\n3:DISPLAY \n4:EXIT";
cout<<"\nEnter your choice(1-4): ";
cin>>choice;
switch(choice)
{
case 1:
s.push();
break;
case 2:
s.pop();
break;
case 3:
s.show();
break;
case 4:exit(0);
break;
default:
cout<<"\nPlease enter correct choice(1-4)!!";
break;
}
}
}

Queue using Array


#include<iostream.h>
#include<process.h>
#include<conio.h>
class queue
{
int front,rear,data[10];
public:
queue()
{
front=rear=-1;
}
void insert();
void del();
void display();
};
queue q;
void queue::insert()
{
rear++;
if(rear==5)
{
cout<<"Overflow";
rear--;
return;
}
cout<<"Enter the no";
cin>>data[rear];
cout<<"\nInserted";
if(front==-1)
front=0;
}
void queue::del()
{
if(front==-1)
{
cout<<"\n Under flow";
return;
}
cout<<data[front]<<"deleted";
front++;
if(front>rear)
front=rear=-1;
}
void queue::display()
{
if(front==-1)
{
cout<<"\n Under flow";
return;
}
for(int i=front;i<=rear;i++)
cout<<data[i];
}
void main()
{
clrscr();
int opt;
while(1)
{
cout<<"\nChosse \n1.Insert\n"<<"2.Delete\n"<<"3.display\n"<<"4. Exit\n";
cout<<"Please enter your choice: ";
cin>>opt;
switch(opt)
{
case 1:
q.insert();
break;

case 2:
q.del();
break;

case 3:
q.display();
break;
case 4:exit(0);
break;
default :
cout<<"An Invalid Choice!!!\n";
}
}
}

Queue Using Linked List:1


#include<iostream.h>
#include<process.h>
#include<stdio.h>
#include<conio.h>
struct node
{
long pincode;
char city[20];
node *next;
};
class queue
{
node *rear, *front;
public:
queue() // constructor
{
rear=front=NULL;
}
void insert(); // to insert an element
void del(); // to delete an element
void show(); // to show the element
};
void queue::insert()
{
node *t=new node;
cout<<"\n Enter Pincode to insert: ";
cin>>t->pincode;
cout<<"Enter City:";
cin>>t->city;
t->next=NULL;
if(rear==NULL)
front=rear=t;
else
{
rear->next=t;
rear=t;
}
cout<<"\n New item is inserted to the stack!!!";
}
void queue::del()
{
node *t=new node;
if(front==NULL)
{
cout<<"\nThe Queue is empty!!!";
}
else
{
t=front;
front=front->next;
cout<<"\nDeleted value is "<<t->pincode;
delete t;
}
}
void queue::show()
{
node *t = new node;
if(front==NULL)
cout<<"Underflow";
else
{
cout<<"\nThe queue is\n";
t=front;
while(t!=NULL)
{
cout<<t->pincode<<" "<<t->city;
t=t->next;
}
}
}
void main()
{
clrscr();
queue q;
int choice;
while(1)
{
cout<<"\n Main Menu";
cout<<"\n1:INSERT\n2:DELETE\n3:DISPLAY \n4:EXIT";
cout<<"\nEnter your choice(1-4): ";
cin>>choice;
switch(choice)
{
case 1:
q.insert();
break;
case 2:
q.del();
break;
case 3:
q.show();
break;
case 4:exit(0);
break;
default:
cout<<"\nPlease enter correct choice(1-4)!!";
break;
}
}
}

Queue using lInked lIst 2


#include<iostream.h>
#include<process.h>
#include<stdio.h>
#include<conio.h>
struct node
{
int marks;
char grade;
node *next;
};
class queue
{
node *rear, *front;
public:
queue() // constructor
{
rear=front=NULL;
}
void insert(); // to insert an element
void del(); // to delete an element
void show(); // to show the element
};
void queue::insert()
{
node *t=new node;
cout<<"\n Enter your Marks: ";
cin>>t->marks;
cout<<"Enter Grade:";
cin>>t->grade;
t->next=NULL;
if(rear==NULL)
front=rear=t;
else
{
rear->next=t;
rear=t;
}
cout<<"\n New item is inserted to the stack!!!";
}
void queue::del()
{
node *t=new node;
if(front==NULL)
{
cout<<"\nThe Queue is empty!!!";
}
else
{
t=front;
front=front->next;
cout<<"\nDeleted value is "<<t->marks;
delete t;
}
}
void queue::show()
{
node *t = new node;
if(front==NULL)
cout<<"Underflow";
else
{
cout<<"\nThe queue is\n";
t=front;
while(t!=NULL)
{
cout<<t->marks<<" "<<t->grade;
t=t->next;
}
}
}
void main()
{
clrscr();
queue q;
int choice;
while(1)
{
cout<<"\n Main Menu";
cout<<"\n1:INSERT\n2:DELETE\n3:DISPLAY \n4:EXIT";
cout<<"\nEnter your choice(1-4): ";
cin>>choice;
switch(choice)
{
case 1:
q.insert();
break;
case 2:
q.del();
break;
case 3:
q.show();
break;
case 4:exit(0);
break;
default:
cout<<"\nPlease enter correct choice(1-4)!!";
break;
}
}
}

You might also like