Write a program to perform the following operation on a simple queue ( using Array )
a. Insert an element
b. Remove an element
c. Display
#include<iostream.h> #include<stdlib.h> #include<conio.h> class Queue { int *que, F, R; int size; public : Queue(int s) { F = R = -1; size = s; que = new int[size]; } void insertElement(int); // insert element fucntion void display(); void delElement(); }; void Queue :: insertElement(int element) { if(R == (size - 1)) cout<<endl<<"Queue is Full"<<endl; else { if(F == -1 ) F = 0; R++; que[R] = element; } } void Queue :: delElement() { if(F == -1) cout<<endl<<"Queue is Empty"; else { if(F == R) { F = R = -1; } else { int element = que[F]; F++; cout<<endl<<"Deleted Element is : "<<element<<endl; } } } void Queue :: display() { cout<<endl<<"\t\t\t"; for(int i = F; i <= R; i++) { cout<<que[i]<<" "; } cout<<endl; } void main() { int ch, element, size; clrscr(); cout<<endl<<"Queue OPERATION"; cout<<endl<<"Enter Size for Queue: "; cin>>size; Queue q(size); //object of queue class while(1) { cout<<endl<<" 1. Insert"; cout<<endl<<" 2. delete"; cout<<endl<<" 3. Exit"<<endl; cin>>ch; switch(ch) { case 1: cout<<endl<<"Enter Element: "; cin>>element; q.insertElement(element); q.display(); break; case 2: q.delElement(); q.display(); break; case 3: exit(0); default: cout<<endl<<"wrong choice"<<endl; } } getch(); } |
0 comments :