Answer to Question #255274 in Databases | SQL | Oracle | MS Access for Tarurendra Kushwah

Question #255274

Write a program to implement a dequeue with the help of a linked list.


1
Expert's answer
2021-10-22T16:46:43-0400
CODE IN C++ :

//Implement Queue by using Linked List in C++ Program
#include <iostream>
using namespace std;
struct node {
int data;
struct node *next;
};
struct node* top = NULL;
struct node* bottom = NULL;
struct node* temp;
void Insert() {
int val;
cout<<"Insert the element in queue : "<<endl;
cin>>val;
if (bottom == NULL) {
bottom = (struct node *)malloc(sizeof(struct node));
bottom->next = NULL;
bottom->data = val;
top = bottom;
} else {
temp=(struct node *)malloc(sizeof(struct node));
bottom->next = temp;
temp->data = val;
temp->next = NULL;
bottom = temp;
}
}
void Delete() {
temp = top;
if (top == NULL) {
cout<<"Underflow"<<endl;
return;
}
else
if (temp->next != NULL) {
temp = temp->next;
cout<<"Element deleted from queue is : "<<top->data<<endl;
free(top);
top = temp;
} else {
cout<<"Element deleted from queue is : "<<top->data<<endl;
free(top);
top = NULL;
bottom = NULL;
}
}
void Display() {
temp = top;
if ((top == NULL) && (bottom == NULL)) {
cout<<"Queue is empty"<<endl;
return;
}
cout<<"Queue elements are: ";
while (temp != NULL) {
cout<<temp->data<<" ";
temp = temp->next;
}
cout<<endl;
}
int main() {
int choice;


do {
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>>choice;
switch (choice) {
case 1: Insert();
break;
case 2: Delete();
break;
case 3: Display();
break;
case 4: cout<<"Exit"<<endl;
break;
default: cout<<"Invalid choice"<<endl;
}
} while(choice!=4);
return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS