write a C++ program of traversing of circular Queue by using Nodes?
Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
Related questions
Question
write a C++ program of traversing of circular Queue by using Nodes?
Expert Solution

C++ Program
#include <bits/stdc++.h>
using namespace std;
/* Structure for creating node and pointer */
struct node
{
int data;
struct node *next;
} *rear,*front,*temp,*newNode;
/* Initialising the pointers in the linked list */
void create()
{
front = rear = NULL;
}
/* Function to check if the queue is empty */
int empty()
{
if(front == NULL)
{
return 1;
}
else
return 0;
}
int getFront()
{
// check whether Deque is empty or not
if (empty())
{
cout << " Underflow\n" << endl;
return -1 ;
}
node *temp = front;
return temp->data;;
}
int getRear()
{
// check whether Deque is empty or not
if(empty() || rear < 0)
{
cout << " Underflow\n" << endl;
return -1 ;
}
node *temp = rear;
return temp->data;;
}
/* Function to insert elements in a circular queue */
void enqueue(int data)
{
newNode = (struct node*) malloc(sizeof(struct node));
newNode -> data = data;
newNode -> next = NULL;
if(front == NULL)
front = rear = newNode;
else
{
rear -> next = newNode;
rear = newNode;
}
rear -> next = front; // rear always points to the front
}
/* Function to delete an element from the circular queue */
int dequeue()
{
int x;
if (front == NULL)
{
return -1; //for empty queue
}
else if (front == rear) // Queue has only a single node
{
x = front->data;
delete front;
front = rear = NULL;
}
else
{
node *temp = front;
x = temp -> data;
front = front -> next;
// Make rear point to the front node
rear -> next = front;
delete temp;
}
return x;
}
/* Function to print the elements in the circular queue */
void display()
{
node *temp = front;
cout<<"\nCIRCULAR QUEUE : ";
do
{
cout<<temp->data;
temp = temp->next;
}while (temp != front);
}
/* Main function */
int main()
{
int num,choice;
while(1)
{
cout<<"\n\nQUEUE OPERATIONS\n\n1.ENQUEUE\n2.DEQUEUE\n3.DISPLAY\n4.GET FRONT\n5.GET REAR\nANYTHING TO EXIT\n\n";
cin>>choice;
switch (choice)
{
case 1:
cout<<"\nEnter item : ";
cin>>num;
enqueue(num);
break;
case 2:
if(!(empty()))
cout<<"\nDequeued element : "<<dequeue();
else
cout<<"\nEMPTY QUEUE\n";
break;
case 3:
display();
break;
case 4:
cout<<getFront()<<endl;
break;
case 5:
cout<<getRear()<<endl;
break;
default: exit(0);
}}
return 0;
}
Step by step
Solved in 2 steps with 1 images

Recommended textbooks for you

Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON

Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science

Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning

Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON

Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science

Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning

Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning

Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education

Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY