You are on page 1of 14

LINKED LISTS, STACKS AND QUEUES

Q. No. 41) Write a menu based program using Array to do Stack operations with the
following options on the basis of user’s choice.
1. Push an Element
2. Pop an Element
3. Print Stack
4. Exit

#include<iostream.h>
#include<conio.h>
#include<process.h>
const int MAX=5;
class stack
{
int stk[MAX];
int top;
public:
stack()
{
top=-1;
}

void push(int x)
{
if(top > MAX - 1)
{
cout <<"stack over flow";
return;
}
++top;
stk[top]=x;
cout <<"inserted" <<x;
}

void Pop()
{
if(top <0)
{
cout <<"stack under flow";
return;
}
cout <<"deleted" <<stk[top--];
}
void Display()
{
if(top<0)
{
cout <<" stack empty";
return;
}

for(int i=top;i>=0;i--)
cout <<stk[i] <<" ";
}
};
int main()
{
int ch;
stack st;
while(1)
{ cout <<"\n1.push 2.Pop 3.Display 4.exit\n";
cout<<"\nEnter your choice\n";
cin >> ch;
switch(ch)
{
case 1: cout <<"enter the element";
cin >> ch;
st.push(ch);
break;
case 2: st.Pop(); break;
case 3: st.Display();break;
case 4: exit(0);
}
}
getch();
return (0);
}
Sample Output:
Q. No. 42) Write a menu based program to perform Push, Pop & Display operation in a
dynamically allocated STACK containing Roll No using following class.

struct NODE
{
int RollNo;
NODE *next;
};
class STACK
{
NODE *Top;
public:
STACK() { Top = NULL; }
void Push();
void Pop();
void Display();
};

#include<iostream.h>
#include<conio.h>
#include<process.h>

struct NODE
{
int RollNo;
struct NODE *next;
};

class stack
{
NODE *top;
public:
STACK()
{
top=NULL;
}
void Push(int x)
{
if (top==NULL )
{
top=new NODE;
top->next=NULL;
top->RollNo=x;
}
else
{
NODE *temp,*temp1;
temp=top;
while(temp->next != NULL)
temp=temp->next;
temp1=new NODE;
temp->next=temp1;
temp1->next=NULL;
temp1->RollNo=x;
}
}
void Display()
{
NODE *temp;
temp=top;
if (top ==NULL)
{
cout <<" stack under flow";
return;
}
while(temp != NULL)
{
cout <<temp->RollNo<< " ";
temp=temp->next;

}
}
void Pop()
{
NODE *temp;
temp=top;
if( top == NULL)
{
cout <<"stack under flow";
return;
}
while(temp->next->next!=NULL)
{
temp=temp->next;
}
temp->next=NULL;
}
};
int main()
{
stack s1;
int ch;
int rno;
while(1)
{
cout <<"\n1.PUSH\n2.POP\n3.DISPLAY\n4.EXIT\n";
cout<<"\nEnter your choice:";
cin >> ch;
switch(ch)
{
case 1: cout <<"\n Enter Roll No\n";
cin >> rno;
s1.Push(rno);
break;
case 2: s1.Pop();
break;
case 3: s1.Display();
break;
case 4: exit(0);
}
}
return 0;
}

Sample Output:
Q. No. 43) Write a menu based program using array to do Queue operations with the
following options on the basis of user’s choice.

1. Insert an Element
2. Delete an Element
3. Print Queue
4. Exit

#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
#define SIZE 5
int q[SIZE],front=0,rear=0;
void main()
{
int ch;
clrscr();
void enqueue();
void dequeue();
void display();
while(1)
{
cout<<"\n 1. add element";
cout<<"\n 2. remove element";
cout<<"\n 3.display";
cout<<"\n 4.exit";
cout<<"\n enter your choice:";
cin>>ch;
clrscr();
switch(ch)
{
case 1:
enqueue();
break;
case 2:
dequeue();
break;
case 3:
display();
break;
case 4:
exit(0);
default:
cout<<"\n invalid choice";
}
}
}

void enqueue()
{
int no;
if (rear==SIZE && front==0)
cout<<"queue is full";
else
{
cout<<"enter the num:";
cin>>no;
q[rear]=no;
}
rear++;
}
void dequeue()
{
int no,i;
if (front==rear)
cout<<"queue is empty";
else
{ no=q[front];
front++;
cout<<"\n"<<no<<" -removed from the queue\n";
}
}
void display()
{
int i,temp=front;
if (front==rear)
cout<<"the queue is empty";
else
{
cout<<"\n element in the queue:";
for(i=temp;i<rear;i++)
{
cout<<q[i]<<" ";
}
}
}
Sample Output:
Q. No. 44) Write a menu based program using array to do Circular Queue operations with
the following options on the basis of user’s choice.

1. Insert an Element
2. Delete an Element
3. Print Queue
4. Exit

#include<iostream.h>
#include<conio.h>
#include<stdlib.h>

class cqueue
{
int q[5],front,rear;
public:
cqueue()
{
front=-1;
rear=-1;
}
void insert(int x)
{
if(front ==-1 && rear == -1)
{
q[++rear]=x;
front=rear;
return;
}
else if(front == (rear+1)%5 )
{
cout <<" Circular Queue over flow";
return;
}
rear= (rear+1)%5;
q[rear]=x;
}

void delqueue()
{
if(front==-1 && rear== -1)
{
cout <<"under flow";
return;
}
else if( front== rear )
{
front=rear=-1;
return;
}
front= (front+1)%5;
}

void display()
{
int i;
if( front <= rear)
{
for(i=front; i<=rear;i++)
cout << q[i]<<" ";
}
else
{
for(i=front;i<=4;i++)
{
cout <<q[i] << " ";
}
for(i=0;i<=rear;i++)
{
cout << q[i]<< " ";
}
}
}
};

void main()
{

int ch;
cqueue q1;
while( 1)
{
cout<<"\n1.INSERT 2.DELETE 3.DISPLAY 4.EXIT\nEnter ur choice";
cin >> ch;

switch(ch)
{
case 1: cout<<"enter element";
cin >> ch;
q1.insert(ch);
break;
case 2: q1.delqueue();
break;

case 3: q1.display();
break;
case 4: exit(0);
}
}
}

Sample Output:
Q. No. 45) Write a menu based program to perform Insert, Delete & Display operation in
a dynamically allocated QUEUE using following structure.

#include<iostream.h>
#include<stdlib.h>
#include<conio.h>
struct node
{
int data;
struct node *next;
}*front=NULL,*rear,*temp;
void ins()
{
temp=new node;
cout<<"Enter data:";
cin>>temp->data;
temp->next=NULL;

if(front==NULL)
front=rear=temp;
else
{
rear->next=temp;
rear=temp;
}
cout<<"Node has been inserted\n";
}
void del()
{
if(front==NULL)
cout<<"Queue is empty\n";
else
{
temp=front;
front=front->next;
cout<<"Deleted node is "<<temp->data<<"\n";
delete(temp);
}
}

void dis()
{
if(front==NULL)
cout<<"Queue is empty\n";
else
{ temp=front;
while(temp->next!=NULL)
{
cout<<temp->data<<"->";
temp=temp->next;
}

cout<<temp->data;
}
}
int main()
{
int ch;
while(1)
{
cout<<"\n*** Menu ***"<<"\n1. Insert\n 2.Delete\n 3.Display\n 4.Exit";
cout<<"\n\nEnter your choice(1-4):";
cin>>ch;
cout<<"\n";

switch(ch)
{
case 1: ins();
break;
case 2: del();
break;
case 3: dis();
break;
case 4: exit(0);
break;
default: cout<<"Wrong Choice!!!";
}
}
getch();
return 0;
}

Sample Output:

You might also like