Question

C++ Help Create a node class/struct. Create a queue class/struct. Members: Node - a node that...

C++ Help
Create a node class/struct.
Create a queue class/struct.
Members:
Node - a node that tracks the front of the queue.
Node - a node that tracks the end of the queue.
Count - indicates how many items are on the queue.
Methods:
En-queue
- Accepts a number and adds to the end of the queue.
De-queue
- Returns a number from the front of the queue.
- If the queueis empty, emit an error indicating the queueis empty.
IsEmpty
- Returns a boolean indicating if the queue is empty.

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

#include <iostream>

using namespace std;


class Node    //class that represent the node
{

public:

int data;
    //data part of node
Node* next; //this is link part pointing to next node
};


class queue
{

private:

   Node* front;//node indicating front of queue
   Node* end;
//node indicating end of queue
int count; //count indicating no.of elements in queue


public:

queue()
//constructor of queue class intializing front,end to null and count to zero as the queue is empty intially
   {

       front = NULL;

       end = NULL;

       count = 0;

   }
void Enqueue(int element)//this method is used to add elements to queue
    {

       Node* tmp = new Node();
       // Creating a new node
to add the element in it
        tmp->data = element;

       tmp->next = NULL;


       if ( isEmpty() ) //checking if the queue is empty
         {

  
           front = end = tmp;//add the first elemnt and since it is the one and only element it is both front and end
       }

       else {

         
           end->next = tmp;//add this element at end
           end = tmp;
//make this as end
       }


       count++;//increment count as the element is added
   }
int Dequeue()//this method removes the element from front of the queue
   {
     if ( isEmpty() )//checks if queue is empty
         {

           cout<<"Queue is Empty";

           return -1;

         }

       int num = front->data;

       Node* tmp = front;


     

       front = front->next;// Move the front pointer to next node

       count--;//decrement count as a number is deleted from queue
       return num;

   }
    




   bool isEmpty()//function to check wether the queue is empty
   {

     if (count==0)//if count is zero it returns true indicating queue is empty else returns false
      return true;

    else

      return false;

   }

};


int main()

{

   queue q;//create an object to queue class
   int ch;

   int d;

do

   {

     cout<<"\nEnter your choice\n1:Add an element to queue\n2:delete an element from queue\n3:exit\n";

     cin>>ch;
   
switch(ch)//basing on given chice switch to the cases
     
{
       
case 1:

//if choice is 1 ask for the number and call Enqueue function
          cout<<"enter the number you want to add to queue:";

        int n;

          cin>>n;

         q.Enqueue(n);

          cout<<"element added to queue\n";

         break;
      
case 2:
//if choice is 2 call deque function and store the return value in d
           d=q.Dequeue();

           if(d==-1)

            cout<<"queue is empty";

        else
          

          cout<<"the element removed from queue is:",d;
         

           break;
      
case 3:
  
       break;
      
default :
         
cout<<"invalid choice, please choose correct number\n";
     
}

}while(ch!=3);
//this loop ends when ch==3
return 0;


}


Sample Output:

Enter your choice
1:Add an element to queue
2:delete an element from queue
3:exit
1
enter the number you want to add to queue:20
element added to queue

Enter your choice
1:Add an element to queue
2:delete an element from queue
3:exit
1
enter the number you want to add to queue:45
element added to queue

Enter your choice
1:Add an element to queue
2:delete an element from queue
3:exit
1
enter the number you want to add to queue:89
element added to queue

Enter your choice
1:Add an element to queue
2:delete an element from queue
3:exit
2
the element removed from queue is:20
Enter your choice
1:Add an element to queue
2:delete an element from queue
3:exit
3

Add a comment
Know the answer?
Add Answer to:
C++ Help Create a node class/struct. Create a queue class/struct. Members: Node - a node that...
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...

  • use intellij idea main java Step 3 Develop the following class: Class Name: ImprovedQueue<T> Access Modifier:...

    use intellij idea main java Step 3 Develop the following class: Class Name: ImprovedQueue<T> Access Modifier: public Implements: QueueInterface<T> Instance variables Name: front Access modifier: private Data type: Queue Node<T> Constructors: Name: ImprovedQueue Access modifier: public Parameters: none (default constructor) Task: sets the value of front to null Methods Name: isEmpty Access modifier: public Parameters: none Return type: boolean Task: returns true if the front is equal to null; otherwise return false Name: dequeue Access modifier: public Parameters: none Return...

  • CS 373 Home Work project 11 Create a queue class by priviate inherting the unorderedArrayListType class....

    CS 373 Home Work project 11 Create a queue class by priviate inherting the unorderedArrayListType class. A queue class is a First-In-First-Out data structure. To understand a queue, think of a checkout line at a grocery store: the person at the front is served first (removed), and people are added to the line from the back end. class queue : private unorderedArrayListType { public: bool isEmpty() const; // test whether queue is empty // Post: returns true if queue is...

  • Step 3 Develop the following class: Class Name: ImprovedQueue<T> Access Modifier: public Implements: QueueInterface<T> Instance variables...

    Step 3 Develop the following class: Class Name: ImprovedQueue<T> Access Modifier: public Implements: QueueInterface<T> Instance variables Name: front Access modifier: private Data type: QueueNode<T> Constructors: Name: ImprovedQueue Access modifier: public Parameters: none (default constructor) Task: sets the value of front to null Methods Name: isEmpty Access modifier: public Parameters: none Return type: boolean Task: returns true if the front is equal to null; otherwise return false Name: dequeue                           Access modifier: public Parameters: none Return type: T (parameterized type) Task: makes...

  • Java help: Please help complete the locate method that is in bold.. public class LinkedDoubleEndedList implements...

    Java help: Please help complete the locate method that is in bold.. public class LinkedDoubleEndedList implements DoubleEndedList { private Node front; // first node in list private Node rear; // last node in list private int size; // number of elements in list ////////////////////////////////////////////////// // YOU MUST IMPLEMENT THE LOCATE METHOD BELOW // ////////////////////////////////////////////////// /** * Returns the position of the node containing the given value, where * the front node is at position zero and the rear node is...

  • //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)...

  • Step 1 Develop the following interface: Interface Name: Queue Interface<T> Access Modifier: public Methods Name: isEmpty...

    Step 1 Develop the following interface: Interface Name: Queue Interface<T> Access Modifier: public Methods Name: isEmpty Access modifier: public Parameters: none Return type: boolean Name: dequeue Access modifier: public Parameters: none Return type: T (parameterized type) Name: enqueue Access modifier: public Parameters: element (data type T, parameterized type) Return type: void Step 2 Develop the following class: Class Name: Queue Node<T> Access Modifier: public Instance variables Name: info Access modifier: private Data type: T (parameterized type) Name: link Access modifier:...

  • 2. Consider a circular array based Queue as we have discussed in the lectures (class definition...

    2. Consider a circular array based Queue as we have discussed in the lectures (class definition given below for reference) public class CircArrayQueue<E> implements Queue<E private EI Q private int front-0 indicates front of queue l indicates position after end of queue private int end-0: public CircArrayQueue( public int getSize (.. public boolean isEmpty ( public void enqueue (E e)... public E dequeue ) throws EmptyQueueException... Il constructor We are interested in implementing a Stack class based on the above...

  • JAVA you have been given the code for Node Class (that holds Strings) and the LinkedList...

    JAVA you have been given the code for Node Class (that holds Strings) and the LinkedList Class (some methods included). Remember, you will use the LinkedList Class that we developed in class not Java’s LinkedList Class. You will add the following method to the LinkedList Class: printEvenNodes – this is a void method that prints Nodes that have even indices (e.g., 0, 2, 4, etc). Create a LinkedListDemo class. Use a Scanner Class to read in city names and store...

  • PROBLEM- void CBQueue::printF2B( ) If the queue is empty, the method should print “Queue is empty”,...

    PROBLEM- void CBQueue::printF2B( ) If the queue is empty, the method should print “Queue is empty”, otherwise, this method should print the items in the queue starting at the front of the queue and proceeding to the rear of the queue. The items should be printed one per line. Now that this method is written, you can do a more thorough job of testing enqueue( ). You will want to call printF2B( ) and printB2F( ) after implementing each method...

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