0% found this document useful (0 votes)
36 views

Queue Using Array

This document describes how to implement a queue using an array data structure in C++. It defines a queue class with methods to add, delete, and display elements. The main function contains a menu to test the queue by adding two elements, deleting one, and displaying the remaining element before exiting. The output shows the queue operations working as intended.

Uploaded by

techthiru
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views

Queue Using Array

This document describes how to implement a queue using an array data structure in C++. It defines a queue class with methods to add, delete, and display elements. The main function contains a menu to test the queue by adding two elements, deleting one, and displaying the remaining element before exiting. The output shows the queue operations working as intended.

Uploaded by

techthiru
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

1.

QUEUE USING ARRAY

#include<iostream.h> #include<conio.h> #include<stdlib.h> #define max 5 class queue { private: int top,item[max],front,rear; public: queue() { rear=front=-1; } void add(); void del(); void display(); }; void queue::add() { int x; if(rear==max-1) cout<<"\n Queue is full"; else { rear++; cout<<"\n Enter the element:"; cin>>x; item[rear]=x; }

} void queue::del() { int x; if(front==rear) cout<<"\n Queue is empty"; else { front++; x=item[front]; cout<<"\n Popped element is:"<<x; } } void queue::display() { if(front==rear) cout<<"\n Queue is empty"; else { for(int i=front+1;i<rear;i++) { cout<<"\n Element is:"<<i+1<<item[i]; } } } void main() { queue q; int ch; clrscr(); cout<<"\n\t\t Queue using arrays"; cout<<"\n1.Add\n2.Delete\n3.Display\n4.Exit"; do

{ cout<<"\n Enter your choice:"; cin>>ch; switch(ch) { case 1: q.add(); break; case 2: q.del(); break; case 3: q.display(); break; } } while(ch!=4); getch(); }

OUTPUT:

Queue using arrays


1.Add 2.Delete 3.Display 4.Exit Enter your choice:1 Enter the element:56 Enter your choice:1 Enter the element:45 Enter your choice:2 Popped element is:56 Enter your choice:3 Element is:45 Enter your choice:4

You might also like