Implement classes Stack and Queue that provide the stack and queue functionaliti
ID: 3672990 • Letter: I
Question
Implement classes Stack and Queue that provide the stack and queue functionalities needed by the test code Stack_test.cpp and Queue_test.cpp respectivelly. Stack_test.cpp is given below. You are to give your own Queue_test.cpp. When you do this, you should not use STL Stack or Queue class.
First get the simpler test program test.cpp given below working, then get Stack_test.cpp working.
Remember when you need to compile a program that needs class String from lab 3, you must include String.h and compile as follows:
Explanation / Answer
I have written the implementation of the queue class for the container. working c++ code for the same.
struct node{
int info;
struct node *next;
};
class Queue{
private:
node *rear;
node *front;
public:
Queue();
void enqueue();
void dequeue();
void display();
};
Queue::Queue(){
rear = NULL;
front = NULL;
}
void Queue::enqueue(){
int data;
node *temp = new node;
cout<<"Enter the data to enqueue: ";
cin>>data;
temp->info = data;
temp->next = NULL;
if(front == NULL){
front = temp;
}else{
rear->next = temp;
}
rear = temp;
}
void Queue::dequeue(){
node *temp = new node;
if(front == NULL){
cout<<" Queue is Emtpty ";
}else{
temp = front;
front = front->next;
cout<<"The data Dequeued is "<<temp->info;
delete temp;
}
}
void Queue::display(){
node *p = new node;
p = front;
if(front == NULL){
cout<<" Nothing to Display ";
}else{
while(p!=NULL){
cout<<endl<<p->info;
p = p->next;
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.