Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

#include<iostream> #include<stdlib.h> using namespace std; //STEP1: Create a con

ID: 3806046 • Letter: #

Question

#include<iostream>
#include<stdlib.h>
using namespace std;

//STEP1: Create a conceptual "idea" of a chunk. We'll build a stack of chunks
class chunk {
public:
   int value;
   chunk *next;
   //give our building blocks a default look and feel
   chunk() {
       value = 0;
       next = NULL;
   }
};

class Stack {
public:
   chunk *top; //gateway pointer to the structure

   //constructor
   Stack() {
       top = NULL; //start with an empty stack
   }
  
   //Functions, with an underlying philosophy: LIFO
   //Function 1: Add to the stack, popularly called 'push'
   void push(int x) {
       //get a new chunk, we are going to add this
       chunk *temp;
       temp = new chunk;
       temp->value = x;

       //is my stack empty?
       if(top == NULL) {
           //new element becomes the top
           top = temp;
       }
       else {
           //stack has more than one chunk in there already
           temp->next = top;
           top = temp;
       }
      
   }

   //Function 2: Delete from stack, popularly called 'pop'
   void pop() {
       if(top == NULL) {
           cout << "Empty stack, nothing to delete" << endl;
       }
       else {
           chunk *temp;
           temp = top;
           top = temp->next; //top points to the second element
           cout << "About to delete: " << temp->value << endl;
           delete temp;
       }
   }

   void display() {
       chunk *traverse = top;
       if(top == NULL) {
           cout << "Empty Stack. Nothing to display" << endl;
       }
       else {
           while(traverse != NULL) {
               int i;
               cout << traverse->value << "-->" << endl;
              
               traverse = traverse->next;

           }
       }
   }


};


int main()
{

   Stack ourStack;
   int choice = 0;

   while(1) {
       cout << "Press 1 to add to stack" << endl;
       cout << "Press 2 to delete from stack" << endl;
       cout << "Press 3 to display" << endl;
       cout << "Anything else to quit" << endl;
       cin >> choice;

       switch(choice) {
           case 1: cout << "Add what?" << endl;
                   int value;
                   cin >> value;
                   ourStack.push(value);
                   break;

           case 2: ourStack.pop();
                   break;

           case 3: ourStack.display();
                   break;

           default: exit(1);
       }
   }


}

Please base on this stack code, wirte a Queue code and Linked list code. Thank you!

Explanation / Answer

#include<iostream>
#include<cstdlib>
using namespace std;
//Class chunk definition
class chunk
{
public:
//Data member
int value;
chunk *next;
//Constructor
chunk()
{
value = 0;
next = NULL;
}//End of constructor
};//End of class

//Class Queue definition
class Queue
{
private:
//Data member
chunk *rear;
chunk *front;
public:
//Member function
Queue();
void Insert();
void Delete();
void Show();
};//End of class

//Constructor
Queue::Queue()
{
rear = NULL;
front = NULL;
}//End of constructor

//Insert operation
void Queue::Insert()
{
int data;
//Creates a chunk
chunk *temp = new chunk();
//Accept data to insert
cout<<"Enter the data to insert into the queue: ";
cin>>data;
//Assigns data to chunk
temp -> value = data;
//Assign chunk next to null
temp -> next = NULL;
//Checks if front is NULL then front will point to temp
if(front == NULL)
{
front = temp;
}//End of if
//Otherwise rear next will point to temp
else
{
rear->next = temp;
}//End of else
//Read points to temp
rear = temp;
}//End of function

//Function to delete a chunk
void Queue::Delete()
{
//Creates a chunk
chunk *temp = new chunk();
//Checks if front is NULL then nothing to delete
if(front == NULL)
{
cout<<" Queue is Empty ";
}//End of if
//Otherwise delete the chunk
else
{
//Chunk temp is assigned to front
temp = front;
//Front points to front next
front = front->next;
//Display the deleted chunk data
cout<<" The deleted data is: "<<temp -> value;
//Delete the chunk
delete temp;
}//End of else
}//End of function

//Function to display
void Queue::Show()
{
//Creates a chunk
chunk *temp = new chunk();
//Chunk is assigned with front
temp = front;
//Checks if front is NULL Queue is empty nothing to display
if(front == NULL)
{
cout<<" Nothing to Display ";
}//End of if
//Otherwise display the queue data
else
{
//Loops till end of queue
while(temp != NULL)
{
//Display current chunk data
cout<<endl<<temp -> value;
//Chunk points to next Chunk
temp = temp -> next;
}//End of while
}//End of else
}//End of function

//Main function
int main()
{
//Creates an object of queue
Queue queue;
int choice;
//Loops till user choice
while(true)
{
//Displays menu
cout<<" Press 1 to add to Queue Press 2 to delete from Queue Press 3 to display Anything else to quit: ";
//Accepts user choice
cin>>choice;
//Checks user choice
switch(choice)
{
case 1:
queue.Insert();
break;
case 2:
queue.Delete();
break;
case 3:
queue.Show();
break;
default:
exit(0);
}//End of switch
}//End of while
return 0;
}//End of main

Output:


Press 1 to add to Queue
Press 2 to delete from Queue
Press 3 to display
Anything else to quit: 1
Enter the data to insert into the queue: 11

Press 1 to add to Queue
Press 2 to delete from Queue
Press 3 to display
Anything else to quit: 1
Enter the data to insert into the queue: 22

Press 1 to add to Queue
Press 2 to delete from Queue
Press 3 to display
Anything else to quit: 1
Enter the data to insert into the queue: 33

Press 1 to add to Queue
Press 2 to delete from Queue
Press 3 to display
Anything else to quit: 1
Enter the data to insert into the queue: 44

Press 1 to add to Queue
Press 2 to delete from Queue
Press 3 to display
Anything else to quit: 3

11
22
33
44
Press 1 to add to Queue
Press 2 to delete from Queue
Press 3 to display
Anything else to quit: 2

The deleted data is: 11
Press 1 to add to Queue
Press 2 to delete from Queue
Press 3 to display
Anything else to quit: 3

22
33
44
Press 1 to add to Queue
Press 2 to delete from Queue
Press 3 to display
Anything else to quit: 5