You are on page 1of 6

Data Structure and Algorithm

Ali gohar
215168
Topic
QUEUE Using circular array

Submitted by Ali Gohar

Submitted to Miss Hira Zaffar


Lab Task
Implement 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;

cout << "Queue after enqueue" << endl;


Q.Enqueue(2);
Q.Enqueue(4);
Q.Enqueue(6);
Q.Enqueue(8);
Q.Enqueue(10);
Q.Print();
cout << "\nQueue after dequeue an element " << endl;
Q.Dequeue();
Q.Print();

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;
};

struct node* front = NULL;


struct node* rear = NULL;
struct node* temp;

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;

cout << "Enter your choice : " << endl;


cin >> ch;
switch (ch) {
case 1: Enqueue();
break;
case 2: Dqueue();
break;
case 3: Display();
break;
case 4: cout << "Exit" << endl;
break;
default: cout << "Invalid choice" << endl;
}
} while (ch != 4);
return 0;
}
Out put

You might also like