Question

C++ ((USE STL verison please)) Implement the queue class as shown above. Use your queue to...

C++

((USE STL verison please))

Implement the queue class as shown above. Use your queue to store the names of 5 students waiting outside Student Services to meet with advisors. You may want to add more data to each node including student ID, Major, etc. Add a member function displayQueue which displays the contents of the queue. Write the main function to test the queue.

CLASS:

#ifdef queue_h
#define queue_h
namespace queues
{
struct queueNode{
char data;
queueNode *link;
};
typedef queueNode* queueNodePtr;
class queue {
public:
queue();
queue(const queue& aQueue);
~queue();
void add(char item);
char remove();
bool empty() const;
private:
queueNodePtr front;
queueNodePtr back;
};
}
#endif

0 0
Add a comment Improve this question Transcribed image text
Answer #1

//queue.h

#include<iostream>
using namespace std;
struct queueNode{
string name;
int ID;
string major;
struct queueNode *next;
};
typedef queueNode* queueNodePtr;
class queue {
public:
queue(){
   front=NULL;
   back=NULL;
}
queue(const queue& aQueue){
   this->front = aQueue.front;
   this->back = aQueue.back;
}
~queue(){
   front=NULL;
   back=NULL;
}
void add(queueNodePtr s){
   if(back ==NULL){
       back=s;
       front=back;
       back->next=NULL;
       return;
   }
   back->next = s;
   back=s;
   back->next=NULL;
}
queueNodePtr remove(){
   if(front==NULL)return NULL;
   queueNodePtr temp = front;
   if(front==back){
       front=NULL;
       back=NULL;
       return temp;
   }
   front=front->next;
   return temp;
  
}
bool empty(){
   return front==NULL;
}
void display(){
   queueNodePtr temp=front;
   while(temp){
       cout<<temp->ID<<"\t"<<temp->name<<"\t"<<temp->major<<"\n";
       temp=temp->next;
   }
   cout<<"\n";
}
  
private:
queueNodePtr front;
queueNodePtr back;
};

//main.cpp

#include"queue.h"
#include<iostream>
using namespace std;

int main(){
   queueNodePtr s1,s2,s3;
   s1 = new queueNode;
   s1->ID = 111;
   s1->name="abc";
   s1->major= "CSE";
   s1->next=NULL;
  
   s2 = new queueNode;
   s2->ID = 222;
   s2->name="jkl";
   s2->major= "CSE";
   s2->next=NULL;
  
   s3 = new queueNode;
   s3->ID = 333;
   s3->name="xyz";
   s3->major= "CSE";
   s3->next=NULL;
  
   queue q;
   q.add(s1);
   q.add(s2);
   q.add(s3);
  
   q.display();
   q.add(q.remove());
   q.display();
   return 0;
  
}

//sample output

C\Users\IshuManish\ Documents mainqueuee.exe jkl xyz CSE CSE CSE jkl xyz albc CSE CSE CSE rocess exited after 0.06367 seconds

Add a comment
Know the answer?
Add Answer to:
C++ ((USE STL verison please)) Implement the queue class as shown above. Use your queue to...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Are based on the following Queue class code segment class QueueFull {/* Empty exception class */};...

    Are based on the following Queue class code segment class QueueFull {/* Empty exception class */}; Class Queue Empty {/* Empty exception class */}; struct Node//Node structure int data;//Holds an integer Node* next;//Pointer to next node in the queue}; Class Queue//Linked node implementation of Queue ADT {Private: Node* front;//Pointer to front node of queue Node* rear;//pointer to last node of queue Public: Queue ()://default constructor initializes queue to be empty -Queue ();//Deallocates all nodes in the queue Void Add (int...

  • //This is the implementation file queue.cpp. //This is the implementation of the template class Queue. //The...

    //This is the implementation file queue.cpp. //This is the implementation of the template class Queue. //The interface for the template class Queue is in the header file queue.h. #include <iostream> #include <cstdlib> #include <cstddef> #include "queue.h" using std::cout; namespace QueueSavitch { //Uses cstddef: template<class T> Queue<T>::Queue( ) : front(NULL), back(NULL) { //Intentionally empty. } //Uses cstddef: template<class T> bool Queue<T>::isEmpty( ) const { return (back == NULL);//front == NULL would also work } //Uses cstddef: template<class T> void Queue<T>::add(T item)...

  • C++ Implement a class template for a LinkedList.(doubly linked). Also, your class will have a tailPtr...

    C++ Implement a class template for a LinkedList.(doubly linked). Also, your class will have a tailPtr in addition to a headPtr along with methods to get, set, insert and remove values at either end of the list. Call these getFirst, getLast, setFirst, setLast, insertFirst, insertLast, removeFirst, removeLast. Don't forget, you also need a copy constructor and destructor plus getLength, isEmpty and clear methods. Overload the stream insertion operator as a friend function which outputs the list in format { 1,...

  • To solve real world problem, the programer uses ADT like list, stack, queue, set, map, ... etc. t...

    To solve real world problem, the programer uses ADT like list, stack, queue, set, map, ... etc. to build our data model. In AS8, we pick and choose STL queue and stack to build a postfix calculator. In this assignment you are to Design your own linked-list, a series of integer nodes. The private attributes of this IntLinked Queue including a integer node struct and private control variables and methods: private: struct Node { int data; Node *next; }; Node...

  • Am Specification For this assignment, you will write a multi-file C program to define, implement ...

    Must be written and C, and compile with MinGW. Thank you! am Specification For this assignment, you will write a multi-file C program to define, implement and use a dynamic linked lists. Please refer to Lab 07 for the definition of a basic linked list. In this assignment you will need to use the basic ideas of a node and of a linked list of nodes to implement a suit of functions which can be used to create and maintain...

  • C++ problem to use dynamic memory allocation (of arrays) and pointer manipulation in order to implement...

    C++ problem to use dynamic memory allocation (of arrays) and pointer manipulation in order to implement the Inner and Outer classes (Circular Buffer of circular buffers using Queues). No need of any classes from the Standard Template Library (STL), not even vector. Add the member functions of those classes by following the codes of InnerCB.h and CBofCB.h below: // file: InnerCB.h // Header file for Inner Circular Buffer. // See project description for details. // #ifndef _INNERCB_H_ #define _INNERCB_H_ class...

  • Instructions Download the files towardsHashTables.cpp, and Queue.h. This is the incomplete example from class last week,...

    Instructions Download the files towardsHashTables.cpp, and Queue.h. This is the incomplete example from class last week, where we started implementing a hash table as a vector of queues. In order for this example to work, the Queue struct needs 3 more functions to be implemented. A find function, which tells us it a given value appears in the queue, without being destructive. A function called print, which prints out the contents of the queue, again without being destructive. Finally, there...

  • This is the incomplete example from class last week, where we started implementing a hash table...

    This is the incomplete example from class last week, where we started implementing a hash table as a vector of queues. In order for this example to work, the Queue struct needs 3 more functions to be implemented. A find function, which tells us it a given value appears in the queue, without being destructive. A function called print, which prints out the contents of the queue, again without being destructive. Finally, there is a need for a destructor. To...

  • Please answer in C++ Derive a class called Queue from the linked list described in Assignment...

    Please answer in C++ Derive a class called Queue from the linked list described in Assignment 2 (list of Dates). This means the Queue class will inherit all the properties (data and functions) of the linked list. But, since a queue allows pushing only at the back and popping at the front of the list, you will need to prevent the addition in the front and removal at the back. To do this, you must derive the Queue class in...

  • Introduction In this lab, you are supposed to implement a graph class with the data structure...

    Introduction In this lab, you are supposed to implement a graph class with the data structure implemented before like linked list and queue. graph The class graph contains three member variables: linkedList *adjacentVertices; //an array of linked list. For a vertice i, adjacentVertices[i] stores the linked list that contains all other vertices connected to vertice i. int numVertices; //The number of vertices in the graph. int maxNumVertices; //The maximum number of vertices the graph can hold. Following public methods are...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT