Please show all the steps as detail as possible.... I need help with this.... **
ID: 3810496 • Letter: P
Question
Please show all the steps as detail as possible.... I need help with this....
*****************************************************************
Program 2
Write a queue class to implement a queue using a 2 directional linked list.
Test the program with the following sequence of code:
queue myQ; ( Use default constructor to initialize front and end to NULL. )
cout<< myQ.size() << endl; // number of elements in queue
myQ.dequeue(); // Try to deqeue when the queue is empty. Should catch UNDERFLOW
myQ.enqueue("Fred");
myQ.enqueue("Liv");
myQ.enqueue("Julie");
myQ.enqueue("Rich");
myQ.enqueue("William");
myQ.enqueue("Olo");
myQ.enqueue("Xi");
myQ.enqueue("Chu");
myQ.enqueue("Annie");
myQ.enqueue("Carlos");
myQ.enqueue("Tuyet");
myQ.enqueue("Sue");
cout<< myQ.front() << endl; // name at front, if not empty
cout<< myQ.end() << endl; // name at end, if not empty
cout<< myQ.size() << endl; // number of elements in queue
cout << myQ.dequeue() << endl;
cout << myQ.dequeue() << endl;
cout << myQ.dequeue() << endl;;
myQ.enqueue("Olive");
myQ.enqueue("Jim");
cout << myQ.dequeue() << endl;
cout << myQ.dequeue() << endl;
cout<< myQ.front() << endl; // name at front, if not empty
cout<< myQ.end() << endl; // name at end, if not empty
cout<< myQ.size() << endl; // number of elements in queue
Explanation / Answer
You cann use this program for adding elements in linked list queue. I am not able to test this with your given input values. Please test once from your side and let me know in case of any issues.
#include<iostream>
#include<stdio.h>
#include<conio.h>
using namespace std;
struct node
{
int data;
node *next;
}*begin = NULL,*last = NULL,*p = NULL,*np = NULL;
void push(int x)
{
np = new node;
np->data = x;
np->next = NULL;
if(begin == NULL)
{
begin = last = np;
last->next = NULL;
}
else
{
last->next = np;
last = np;
last->next = NULL;
}
}
int remove()
{
int x;
if(begin == NULL)
{
cout<<"empty queue ";
}
else
{
p = begin;
x = p->data;
begin = begin->next;
delete(p);
return(x);
}
}
int main()
{
int n,c = 0,x;
cout<<"Enter the number of values to be pushed into queue ";
cin>>n;
while (c < n)
{
cout<<"Enter the value to be entered into queue ";
cin>>x;
push(x);
c++;
}
cout<<" Removed Values ";
while(true)
{
if (begin != NULL)
cout<<remove()<<endl;
else
break;
}
getch();
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.