Question

In: Computer Science

C++ Data Structures: Implement a Stack and a Queue using Linked list In this lab you...

C++ Data Structures: Implement a Stack and a Queue using Linked list

In this lab you will implement the functionality of a stack and a queue using a linked list. Your program must use of the declaration of the Stack and Queue class in Stack.h and Queue.h

You have to implement the functionalities of queue (enq, deq, displayQueue) in a file called Queue.cpp. All the functions in Queue.cpp should follow the prototypes declared in Queue.h.

Your code should make use of external pointers ‘front’ and ‘rear’ properly. Remember

queue: rear increments on enq and front increments on deq.

Both enq and deq should be O(1). That is they should not have any loop.

----------------------------- Queue.h ------------------------------------

#pragma once
#include
using namespace std;

class Queue {
   struct Node {
       int x;
       Node *next = NULL;
   };
public:
   // Functions for Stack
   Queue(); //You need to implement the constructor
   void enq(int item);
   void deq();
   void displayQueue();

   // private member
private:
   Node *front; // this is the private member variable. It is just a pointer to the first Node
   Node *rear; // this is the private member variable. It is just a pointer to the first Node
};

---------------------Stack.h----------------------------------------------------

#pragma once
#include
using namespace std;

class Stack {
   struct Node {
       int x;
       Node *next = NULL;
   };
public:
   // Functions for Stack
   Stack();
   void push(int item);
   void pop();
   void displayStack();

   // private member
private:
   Node *top; // this is the private member variable. It is just a pointer to the first Node
};

----------------------------------Stack.cpp------------------------------------

#include "Stack.h"

// constructor
Stack::Stack() {
   top = NULL;
}

void Stack::push(int val)
{
   // Grow the list in a reverse direction.

   Node *n = new Node(); // create a new node
   n->x = val; // set the value to the value field of the new node
   n->next = top; // make the new node to point to the current top element
   top = n; // finally, move the top pointer so point the new node
}

void Stack::pop()
{
   struct Node* ptr = top; // temporary pointer pointing to the current top.
// Required because you have to delete the node
   top = top->next; // Move the top to point to the next node
   delete(ptr); // delete the previous top
}

void Stack::displayStack()
{
   struct Node* ptr = top; // Initialization: temporary pointer pointing at top
   while (ptr != NULL) { //Condition: Loop untill NULL (i.e. traverse the entire list)
       cout << ptr->x << " "; //Body: Print the loop
       ptr = ptr->next; //Increment: move the temporary pointer to the next node
   }
   cout << endl;
}

Solutions

Expert Solution

Code

Queue,cpp

#include"Queue.h"

Queue::Queue()

{

front=NULL;

rear=NULL;

}

void Queue::enq(int item)

{

Node *newNode=new Node();

newNode->x=item;

newNode->next=NULL;

if(front==NULL)

{

front = rear = newNode;

return;

}

rear->next = newNode;

rear = newNode;

}

void Queue:: deq()

{

// If queue is empty, return NULL.

if (front == NULL)

return;

Node* temp = front;

delete(temp);

front = front->next;

// If front becomes NULL, then

// change rear also as NULL

if (front == NULL)

rear = NULL;

}

void Queue::displayQueue()

{

struct Node* ptr = front; // Initialization: temporary pointer pointing at top

while (ptr != NULL) { //Condition: Loop untill NULL (i.e. traverse the entire list)

cout << ptr->x << " "; //Body: Print the loop

ptr = ptr->next; //Increment: move the temporary pointer to the next node

}

}

Main.cpp for test Queue.cpp

#include <iostream>

#include "Queue.h"

using namespace std;

int main()

{

Queue que;

que.enq(10);

que.enq(20);

que.enq(30);

que.enq(40);

cout<<"Main queue \n";

que.displayQueue();

que.deq();

que.deq();

cout<<"\n\nAfter performing 2 deq operation queue is : \n";

que.displayQueue();

}

output

If you have any query regarding the code please ask me in the comment i am here for help you. Please do not direct thumbs down just ask if you have any query. And if you like my work then please appreciates with up vote. Thank You.


Related Solutions

In this lab, using C++, you will create two data structures: a stack and a queue....
In this lab, using C++, you will create two data structures: a stack and a queue. You will use STL containers to demonstrate basic ADTs. Queue For the queue, you will simulate a buffer. Remember it is first-in-first-out. The user will enter a number for the number of rounds to run your simulation. You need one function that randomly generates a number. You will also have a user specified percentage, and the function uses this percentage to randomly put the...
In Java or C++, implement a stack and a queue using a linkedlist data structure.  You may...
In Java or C++, implement a stack and a queue using a linkedlist data structure.  You may not use any standard Java or C++ libraries. Assume your data structure only allows Strings. Implement the following operations for the data structure: Queue: enqueue, dequeue, create, isEmpty (10 points) Stack: push, pop, create, isEmpty (10 points) Here is a link to get started on transferring from Java to C++ http://www.horstmann.com/ccj2/ccjapp3.html (Links to an external site.) Upload a zip file with one implementation for...
In C++, Implement the queue ADT with a singly linked list
In C++, Implement the queue ADT with a singly linked list
using C++. edit this code down below so that it will implement stack with linked list...
using C++. edit this code down below so that it will implement stack with linked list contains a default constructor, a copy constructor, and a destructor. #include <iostream> #include <vector> #include <string> #include <stack> #include <limits> using namespace std; class Stack { public: bool isEmpty(); int top(); int pop(); void push(int); void printList(); private: vector<int> elements; }; bool Stack::isEmpty() { return elements.empty(); } int Stack::top() { if(isEmpty()) { throw runtime_error("error: stack is empty"); } return elements.back(); } int Stack::pop() {...
Implement a stack using a single queue. In particular, you are given a queue Q that...
Implement a stack using a single queue. In particular, you are given a queue Q that provides the method Q.size() to return its size at any point and the standard methods of queues (i.e, Q.enqueue(x) and Q.dequeue()). The requirement is to use such methods of Q to implement two methods S.push(x) and S.pop() for a stack S. What are the running times of your methods? Kindly answer using python programming
Description( IN C++)!! The purpose of this challenge is to implement a stack using a Linked...
Description( IN C++)!! The purpose of this challenge is to implement a stack using a Linked List as a backing data structure Requirements Write the following structs struct Location { string name; string address; }; struct VisitNode { Location loc; VisitNode * next; }; Create a class called Stack. In this class, create a private variable VisitNode * head. This will keep track of the location of the head node. Add the following private function. This function will be used...
How do you implement stack by using linked list? No code just explain it.
How do you implement stack by using linked list? No code just explain it.
write a java program to Implement a Priority Queue using a linked list. Include a main...
write a java program to Implement a Priority Queue using a linked list. Include a main method demonstrating enqueuing and dequeuing several numbers, printing the list contents for each.
Write a code to implement a python queue class using a linked list. use these operations...
Write a code to implement a python queue class using a linked list. use these operations isEmpty • enqueue. • dequeue    • size Time and compare the performances of the operations ( this is optional but I would appreciate it)
In C++ In this lab we will be creating a stack class and a queue class,...
In C++ In this lab we will be creating a stack class and a queue class, both with a hybrid method combining linked list and arrays in addition to the Stack methods(push, pop, peek, isEmpty, size, print) and Queue methods (enqueue, deque, peek, isEmpty, size, print). DO NOT USE ANY LIBRARY, implement each method from scratch. Both the Stack and Queue classes should be generic classes. Don't forget to comment your code.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT