Question

You need to implement a queue based on the doubly linked list. (In C++) **PLEASE follow...

You need to implement a queue based on the doubly linked list. (In C++) **PLEASE follow the format** **Don't worry about the readme.txt** THANK YOU SO SO MUCH!

  • You have to implement the doubly linked list in DList.h and DList.cpp and the queue in the LinkedQueue.h and LinkedQueue.cpp; all as template classes.
  • You have to provide the main.cpp file as we discussed in class to allow the user to interact with your queue using enqueue, dequeue, front, back, size, empty, and display functions.
  • Your code should be well documented.
  • You have to submit a zip file including the aforementioned *.h and *.cpp files and Readme.txt file in which you provide the command for compiling and linking your project.
0 0
Add a comment Improve this question Transcribed image text
Answer #1

// For template classes, declaration and implementation is present in the same file

// DList.h

#ifndef DLIST_H_

#define DLIST_H_

#include <iostream>

using namespace std;

template <typename T>

struct node{

               T data;

               node *next;

               node *prev;

};

template <typename T>

class DList

{

               node<T> *front;

               node<T> *back;

public:

               DList();

               void addFront(T data);

               void addBack(T data);

               T getFront() const;

               T getBack() const;

               bool isEmpty() const;

               void removeFront();

               void removeBack();

               int getSize() const;

               void display();

};

template <typename T>

DList<T>::DList()

{

               front = NULL;

               back = NULL;

}

template <typename T>

void DList<T>::addFront(T data)

{

               if(isEmpty())

               {

                              front = new node<T>;

                              front->data = data;

                              front->next = NULL;

                              front->prev = NULL;

                              back = front;

               }else{

                              node<T> *curr = new node<T>;

                              curr->data = data;

                              curr->next = front;

                              curr->prev = NULL;

                              front->prev = curr;

                              front = curr;

               }

}

template <typename T>

void DList<T>::addBack(T data)

{

               if(isEmpty())

               {

                              front = new node<T>;

                              front->data = data;

                              front->next = NULL;

                              front->prev = NULL;

                              back = front;

                              }else{

                                             node<T> *curr = new node<T>;

                                             curr->data = data;

                                             curr->next = NULL;

                                             curr->prev = back;

                                             back->next = curr;

                                             back = curr;

                              }

}

template <typename T>

T DList<T>:: getFront() const

{

               if(isEmpty())

                              return NULL;

               return front->data;

}

template <typename T>

T DList<T>::getBack() const

{

               if(isEmpty())

                              return NULL;

               return back->data;

}

template <typename T>

bool DList<T>:: isEmpty() const

{

               return(front==NULL);

}

template <typename T>

void DList<T>:: removeFront()

{

               if(!isEmpty())

               {

                              node<T> *temp = front;

                              front = front->next;

                              if(front != NULL)

                                             front->prev = NULL;

                              else

                                             back = NULL;

                              delete(temp);

               }

}

template <typename T>

void DList<T>:: removeBack()

{

               if(!isEmpty())

               {

                              node<T> temp = back;

                              back = back->prev;

                              if(back == NULL)

                                             front = NULL;

                              else

                                             back->next = NULL;

                              delete(temp);

               }

}

template <typename T>

int DList<T>::getSize() const

{

               node<T> *curr = front;

               int count = 0;

               while(curr != NULL)

               {

                              count++;

                              curr = curr->next;

               }

               return count;

}

template <typename T>

void DList<T>:: display()

{

               if(isEmpty())

                              cout<<"Empty list";

               else

               {

                              node<T> *curr = front;

                              while(curr->next != NULL)

                              {

                                             cout<<curr->data<<"->";

                                             curr = curr->next;

                              }

                              cout<<curr->data;

               }

}

#endif /* DLIST_H_ */

//end of DList.h

// LinkedQueue.h

#ifndef LINKEDQUEUE_H_

#define LINKEDQUEUE_H_

#include "DList.h"

template <class T>

class LinkedQueue : public DList<T>

{

public:

               LinkedQueue();

               void enqueue(T data);

               void dequeue();

               T front() const;

               T back() const;

               int size() const;

               bool isEmpty() const;

               void display() ;

};

//#include "LinkedQueue.h"

template <class T>

LinkedQueue<T>::LinkedQueue() : DList<T>()

{}

template <class T>

void LinkedQueue<T>::enqueue(T data)

{

               DList<T>::addBack(data);

}

template <class T>

void LinkedQueue<T>::dequeue()

{

               DList<T>::removeFront();

}

template <class T>

T LinkedQueue<T>::front() const

{

               return(DList<T>::getFront());

}

template <class T>

T LinkedQueue<T>::back() const

{

               return DList<T>::getBack();

}

template <class T>

int LinkedQueue<T>::size() const

{

               return DList<T>::getSize();

}

template <class T>

bool LinkedQueue<T>:: isEmpty() const

{

               return(DList<T>::isEmpty());

}

template <class T>

void LinkedQueue<T>:: display()

{

               DList<T>::display();

}

#endif /* LINKEDQUEUE_H_ */

//end of LinkedQueue.h

// main.cpp

#include "LinkedQueue.h"

#include <iostream>

using namespace std;

int main()

{

               LinkedQueue<int> queue;

               int choice;

               int data;

               do{

                              cout<<"1. Enqueue "<<endl;

                              cout<<"2. Dequeue"<<endl;

                              cout<<"3. Get Front"<<endl;

                              cout<<"4. Get Back"<<endl;

                              cout<<"5. Size"<<endl;

                              cout<<"6. Is Empty "<<endl;

                              cout<<"7. Display"<<endl;

                              cout<<"8. Exit"<<endl;

                              cout<<"Choice(1-8) : ";

                              cin>>choice;

                              switch(choice)

                              {

                                             case 1:

                                                            cout<<"Data : ";

                                                            cin>>data;

                                                            queue.enqueue(data);

                                                            break;

                                             case 2:

                                                            if(!queue.isEmpty())

                                                                           queue.dequeue();

                                                            else

                                                                           cout<<"Queue empty"<<endl;

                                                            break;

                                             case 3:

                                                            if(!queue.isEmpty())

                                                                           cout<<queue.front()<<endl;

                                                            else

                                                                           cout<<"Queue empty"<<endl;

                                                            break;

                                             case 4:

                                                            if(!queue.isEmpty())

                                                                           cout<<queue.back()<<endl;

                                                            else

                                                                           cout<<"Queue empty"<<endl;

                                                            break;

                                             case 5:

                                                            cout<<"Size : "<<queue.size()<<endl;

                                                            break;

                                             case 6:

                                                            if(queue.isEmpty())

                                                                           cout<<" Queue is empty"<<endl;

                                                            else

                                                                           cout<<" Queue is not empty"<<endl;

                                                            break;

                                             case 7:

                                                            cout<<"Queue : ";

                                                            queue.display();

                                                            cout<<endl;

                                                            break;

                                             case 8:

                                                            break;

                                             default:

                                                            cout<<"Invalid choice"<<endl;

                              }

               }while(choice != 8);

               return 0;

}

//end of main.cpp

Add a comment
Know the answer?
Add Answer to:
You need to implement a queue based on the doubly linked list. (In C++) **PLEASE follow...
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
  • Write a C++ program to implement a queue using linked lists. You can use the queue...

    Write a C++ program to implement a queue using linked lists. You can use the queue data structure from the Standard Template Library (STL). The program should provide the following functionality: Enqueue data into queue Dequeue data from queue Print data at the front Print data at the back Print the entire queue Check if the queue is empty Print the number of elements in the queue Test your program using at least the following test cases (considering the queue...

  • QUESTION 1: Queue Class: Write a Queue class using doubly-linked structure and implement the following functionalities....

    QUESTION 1: Queue Class: Write a Queue class using doubly-linked structure and implement the following functionalities. enqueue (inserts element to the end) dequeue (removes the front element and provides content) isEmpty (checks whether the Queue is empty or not) makeEmpty () peek (provides the element sitting at the top/front, but does not remove) print (prints all the elements from front to the end) reversePrint(prints all the elements from end to the front with the help of back pointers inside your...

  • C++ queue data sructure ising linked list. Design and implement a Queue data structure using linked...

    C++ queue data sructure ising linked list. Design and implement a Queue data structure using linked list. Support the following usual operations: (a) default constructor (b) parameterized constructor to create a queue of user-specified capacity (c) enqueue (d) dequeue (e) is_full (f) is_empty display (h) destructor that deallocates all nodes (i) copy constructor (j) overloaded assignment operator Demonstrate using a main function.

  • 3. How can you implement a queue data structure using a doubly linked list? Do you think it is necessary to use a doubl...

    3. How can you implement a queue data structure using a doubly linked list? Do you think it is necessary to use a doubly linked list rather than a singly linked list or not?(3 marks) 3. How can you implement a queue data structure using a doubly linked list? Do you think it is necessary to use a doubly linked list rather than a singly linked list or not?(3 marks)

  • C++ I need help to create a program that executes a simple queue data type. You...

    C++ I need help to create a program that executes a simple queue data type. You must define the queue data type and use the enqueue(), dequeue(), and displayQueue() functions. (Dont use C++ STL to execute queue logic) You may use a linked list , or just use a regular C array of integers. The input file (testfile.txt) will be the list of operations to carry out on a queue. Example input file: enqueue 6 enqueue 8 dequeue enqueue 4...

  • In c++ language Design and implement a Queue data structure using linked list. Support the following...

    In c++ language Design and implement a Queue data structure using linked list. Support the following usual operations: default constructor parameterized constructor to create a queue of user-specified capacity enqueue dequeue is_full is_empty display destructor that deallocates all the nodes copy constructor overloaded assignment operator Demonstrate using a main function.

  • implement a doubly-linked list in C. Each node in the linked list should contain a string,...

    implement a doubly-linked list in C. Each node in the linked list should contain a string, a pointer to the previous node (or NULL), and a pointer to the next node (or NULL). The nodes should be sorted by their strings. struct node_t { char* str; struct node_t* prev; struct node_t* next; } To maintain the doubly-linked list, you should keep a pointer to the head node of the list (or NULL if the list is empty), and a pointer...

  • solving using C. Use a singly linked list to implement a priority queue with two operations:...

    solving using C. Use a singly linked list to implement a priority queue with two operations: enqueue and dequeue. Each node contains an integer value, a priority, and a pointer to the next node. The priority is a value between 1- 10 (where 10 is the highest priority). When a value is added to the queue, it is added with a value and priority. When a value is removed from the priority queue, the first element with the highest priority...

  • You are going to create a Queue. (alternately you can create a list and simply implement...

    You are going to create a Queue. (alternately you can create a list and simply implement enqueue and dequeue functions in the List – that will technically make it a queue). You will fill the first list with numbers consecutively numbered from 2 to n where n is entered by the user (we will call this Q1). When creating your Queue object use the correct function names for enqueue and dequeue functions. Again – sorry, cannot use an Javascript array...

  • Create a flowchart to represent the Push and Pop operations for a Stack based on a linked list data structure. Create a flowchart to represent the Enqueue and Dequeue operations for a Queue based on a...

    Create a flowchart to represent the Push and Pop operations for a Stack based on a linked list data structure. Create a flowchart to represent the Enqueue and Dequeue operations for a Queue based on a linked list data structure. Write the required Java code to implement either a Stack or a Queue data structure based on a linked list. The code should include the class constructors, the necessary properties, and methods to add and remove elements from the data...

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