Professional Documents
Culture Documents
Ali gohar
215168
Topic
QUEUE Using circular array
Source code
//c++ program to implement queue using circular array.
#include<iostream>
#include<conio.h>
using namespace std;
#define MAX_SIZE 5
class Queue
{
private:
int A[MAX_SIZE];
int front, rear;
public:
Queue()
{
front = -1;
rear = -1;
}
void Enqueue(int x)
{
if ((rear + 1) % MAX_SIZE == front)
{
cout << "Queue is Full" << endl;
return;
}
if (front == -1 && rear == -1)
{
front = rear = 0;
}
else
{
rear = (rear + 1) % MAX_SIZE;
}
A[rear] = x;
}
void Dequeue()
{
if (front == -1 && rear == -1)
{
cout << "Queue is Empty" << endl;
return;
}
else if (front == rear)
{
rear = front = -1;
}
else
{
front = (front + 1) % MAX_SIZE;
}
}
int Front()
{
if (front == -1 && rear == -1)
{
cout << "Error: cannot return front from empty queue" << endl;
return -1;
}
return A[front];
}
void Print()
{
int count = (rear + MAX_SIZE - front) % MAX_SIZE + 1;
cout << "Queue: ";
for (int i = 0; i < count; i++)
{
int index = (front + i) % MAX_SIZE;
cout << A[index] << " ";
}
cout << "\n\n";
}
};
int main()
{
Queue Q;
return 0;
}
Out put
Lab Task
Implement Queue using Link list
Source code
#include<iostream>
#include<cstdlib>
#include<conio.h>
using namespace std;
struct node
{
int data;
struct node* next;
};
void Enqueue()
{
system("cls");
int Val;
cout << "Insurt the element in queue" << endl;
cin >> Val;
if (rear == NULL) {
rear = (struct node*)malloc(sizeof(struct node));
rear->next = NULL;
rear->data = Val;
front = rear;
}
else
{
temp = (struct node*)malloc(sizeof(struct node));
rear->next = temp;
temp->data = Val;
temp->next = NULL;
rear = temp;
}
_getch();
}
void Dqueue()
{
system("cls");
temp = front;
if (front == NULL)
{
cout << "Underflow" << endl;
return;
}
else
if (temp->next != NULL)
{
temp = temp->next;
cout << "Element delete from the queue is:" << front->data << endl;
free(front);
front = temp;
}
else
{
cout << "Element delete from the queue is:" << front->data << endl;
free(front);
front = NULL;
rear = NULL;
}
_getch();
}
void Display()
{
system("cls");
temp = front;
if ((front == NULL) && (rear == NULL))
{
cout << "Queue is empty" << endl;
return;
}
cout << "Queue element are:";
while (temp != NULL)
{
cout << temp->data << " ";
temp = temp->next;
}
cout << endl;
_getch();
}
int main() {
int ch;
do {
system("cls");
cout << "1) Insert element to queue" << endl;
cout << "2) Delete element from queue" << endl;
cout << "3) Display all the elements of queue" << endl;
cout << "4) Exit" << endl;