Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
72 views

Queue Codes

The document describes two implementations of a queue data structure - using an array and using linked nodes. For the array implementation, it defines functions to check if the queue is empty, add elements to the rear, remove elements from the front, and display all elements. For the linked list implementation, similar functions are defined to manage a queue using pointers and dynamically allocated nodes. The main function demonstrates using menus to test the enqueue, dequeue, and display functions of the linked list queue.

Uploaded by

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

Queue Codes

The document describes two implementations of a queue data structure - using an array and using linked nodes. For the array implementation, it defines functions to check if the queue is empty, add elements to the rear, remove elements from the front, and display all elements. For the linked list implementation, similar functions are defined to manage a queue using pointers and dynamically allocated nodes. The main function demonstrates using menus to test the enqueue, dequeue, and display functions of the linked list queue.

Uploaded by

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

Queue Implementation with Array

#include <iostream>
using namespace std;

const int size = 5;

int Queue[::size];
int front = -1;
int rear = -1;

bool isempty()
{
if (front == -1 && rear == -1)
{
return true;
}
return false;
}

void enqueue(int value)


{
if (rear == ::size - 1)
{
cout << "Queue is full" << endl;
}
else
{
if (front == -1)
{
front = 0;
}
rear++;
Queue[rear] = value;
}
}
//function to remove element from queue
void dequeue()
{
if (isempty())
{
cout << "Queue is empty\n";
}
else
{
if (front == rear)
{
front = rear = -1;
}
else
{
front++;
}
}
}
//function to display elements of the queue
void displayQueue()
{
if (isempty())
{
cout << "Queue is empty" << endl;
}
else
{
for (int i = front; i <= rear; i++)
{
cout << Queue[i] << " ";
}
cout << endl << endl;
}
}

int main()
{
//inserting elements in queue
cout << "Inserting elements in queue\n";

enqueue(2);
displayQueue();

enqueue(3);
displayQueue();

enqueue(5);
displayQueue();

enqueue(7);
displayQueue();

enqueue(8);
displayQueue();

// queue is full
enqueue(9);

cout << "Removing elements from queue\n";


//removing elements from the queue

dequeue();
displayQueue();

dequeue();
displayQueue();

dequeue();
displayQueue();

dequeue();
displayQueue();

dequeue();

return 0;
}
Queue Implementation with Linked List
#include <iostream>
using namespace std;

// Structure of Node.
struct Node
{
int data;
Node *link;
};

Node *front = NULL;


Node *rear = NULL;

//Function to check if queue is empty or not


bool isempty()
{
if (front == NULL && rear == NULL)
{
return true;
}
else
{
return false;
}
}

//function to enter elements in queue


void enqueue(int value)
{
Node *ptr = new Node();
ptr->data = value;
ptr->link = NULL;

//If inserting the first element/node


if (front == NULL)
{
front = ptr;
rear = ptr;
}
else
{
rear->link = ptr;
rear = ptr;
}
}

//function to delete/remove element from queue


void dequeue()
{
if (isempty())
{
cout << "Queue is empty\n";
}
else
{ //only one element/node in queue.
if (front == rear)
{
free(front);
front = rear = NULL;
}
else
{
Node *ptr = front;
front = front->link;
free(ptr);
}
}
}

//function to display queue


void displayQueue()
{
if (isempty())
{
cout << "Queue is empty\n";
}
else
{
Node *ptr = front;
while (ptr != NULL)
{
cout << ptr->data << " ";
ptr = ptr->link;
}
}
}

//Main Function
int main()
{
int choice, flag = 1, value;

while (flag == 1)
{
cout << "\n1.enqueue 2.dequeue 3.displayQueue 4.exit\n";
cin >> choice;
switch (choice)
{
case 1:
{
cout << "Enter Value:\n";
cin >> value;
enqueue(value);
break;
}
case 2:
{
dequeue();
break;
}
case 3:
{
displayQueue();
break;
}
case 4:
{
flag = 0;
break;
}
}
}

return 0;
}

You might also like