Saturday, January 25, 2014

C++ Program to Queue ADT Implementation Using Linked List

with 0 Comment
Queue ADT- Linked list Implementation
AIM:

  • To Write a C++ Program to Queue ADT implementation using linked list.

Algorithm:
Step 1: Initialize the queue variables front =0 and rear = -1
Step 2: Read the queue operation type.
Step 3: Check the queue operations status.
i). If it is Insertion then do the following steps
3. Check rear not equal to null is true increment the rear by one and read the queue
element and also display queue. otherwise display the queue is full.
4. Go to step2.
ii). If it is deletion then do the following steps
6. Check rear< front is true then display the queue is empty.
7. Move the elements to one step forward (i.e. move to previous index ).
8. Decreases the rear value by one (rear=rear-1).
9. Display queue
10. Go to step2.

Program:
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
//using namespace std;
class node
{
public:
class node *next;
int data;
};
class queue : public node
{
node *head;
int front,rare;
public:
queue()
{
front=-1;
rare=-1;
}
void push(int x)
{
if (rare < 0)
{
head =new node;
head->next=NULL;
head->data=x;
rare ++;
}
else
{
node *temp,*temp1;
temp=head;
if(rare >= 4)
{
cout <<"queue over flow";
return;
}
rare++;
while(temp->next != NULL)
temp=temp->next;
temp1=new node;
temp->next=temp1;
temp1->next=NULL;
temp1->data=x;
} }
void display()
{
node *temp;
temp=head;
if (rare < 0)
{
cout <<" queue under flow";
return;
}
while(temp != NULL)
{
cout <<temp->data<< " ";
temp=temp->next;
}
}
void pop()
{
node *temp;
temp=head;
if( rare < 0)
{
cout <<"queue under flow";
return;
}
if(front == rare)
{
front = rare =-1;
head=NULL;
return;
}
front++;
head=head->next;
}
};
main()
{
Clrscr();
queue s1;
int ch;
while(1)
{
cout <<"\n1.PUSH\n2.POP\n3.DISPLAY\n4.EXIT\n enter ru choice:";
cin >> ch;
switch(ch)
{
case 1:
cout <<"\n enter a element";
cin >> ch;
s1.push(ch); break;
case 2: s1.pop();break;
case 3: s1.display();break;
case 4: exit(0);
}
}
return (0);
}

OUTPUT:
1.PUSH 2.POP 3.DISPLAY 4.EXIT
enter ru choice:1
enter a element23
1.PUSH 2.POP 3.DISPLAY 4.EXIT
enter ru choice:1
enter a element54
1.PUSH 2.POP 3.DISPLAY 4.EXIT
enter ru choice:3
23 54
1.PUSH 2.POP 3.DISPLAY 4.EXIT
enter ru choice:2
1.PUSH 2.POP 3.DISPLAY 4.EXIT
enter ru choice:2
1.PUSH 2.POP 3.DISPLAY 4.EXIT
enter ru choice:2
queue under flow
1.PUSH 2.POP 3.DISPLAY 4.EXIT
enter ru choice:4

Result:
  • Thus, the queue ADT- linked list implementation program has been written and executed successfully.

0 comments:

Post a Comment

Powered by Blogger.

Blog Archive