Tuesday 24 April 2012

2 nd Experiment QUEUE OPERATION ON ADT(abstract)


Algorithm: -QUEUE ADT:
------------------------------------------------------------------------------------------------------------------------------------

Step-1:  Start.
Step- 2: Create an object for the class queue.
Step- 3: do the following.
a)      Print the options
b)     read the choice
c)      i) If case is ‘addqueue’, read the value and use the object of the class to call the member function addqueue() and display()
ii) If case is ‘deletequeue’, use the object to call the member function delqueue() and display()
iii) If case is ‘quit’, display’Bye’.
Step- 4: while the case is not ‘quit’, repeat the step-3.
Step- 5: Stop.
---------------------------------------------------------------------------------------------------------------------------------------
FLOW CHARTs
---------------------------------------------------------------------------------------------------------------------------------------


-----------------------------------------------------------------------------------------------------------------------------------
C++ IMPLEMENTATION
----------------------------------------------------------------------------------------------------------------------------------
#include<iostream.h> #include<conio.h> #define max 50 class queue { private: int qarr[max]; int front,rear; public: queue() { front=-1; rear=-1; } void addqueue(int i) { if(rear==max-1) { cout<<"\n The queue is full\n"; getch(); } rear++; qarr[rear]=i; if(front==-1) front=0; } int delqueue() { int d; if(front==-1) { cout<<"\n Queue is empty\n"; getch(); return NULL; } else d=qarr[front]; if(front==rear) { front=-1; rear=-1; } else front++; return d; } void display() { cout<<"\n The elements in the queue:\n"; for(int i=front;i<=rear;i++) { cout<<qarr[i]<<endl; } getch(); } }; void main() { queue q1; int ch,item; clrscr(); do { cout<<"\n 1. Add queue \n 2. Delete queue \n 3. Quit \n Enter ur choice: "; cin>>ch; switch(ch) { case 1: cout<<"\n Enter the value: "; cin>>item; q1.addqueue(item); q1.display(); getch(); break; case 2: item=q1.delqueue(); cout<<"\n Deleted item: "<<item<<endl; q1.display(); getch(); break; case 3: cout<<"\n Bye"; break; } } while(ch!=3); } OUTPUT: 1.Add queue 2.Delelte queue 3.Quit Enter ur choice: 1 Enter the no: 1 The elements present in queue 1 1.Add queue 2.Delelte queue 3.Quit Enter ur choice: 1 Enter the no: 2 The elements present in queue 1 2 1.Add queue 2.Delelte queue 3.Quit Enter ur choice: 2 popped element is 1 The elements present in queue 2 1.Add queue 2.Delelte queue 3.Quit Enter ur choice: 3 Bye

No comments:

Post a Comment