Question

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 such a way that the base class (LinkedList) functions become private in the derived class (Queue). See private and protected inheritance, instead of public inheritance. Then, define Stack's own add and remove functions that call the appropriate functions for adding and removing from the Queue.

Write a main program, where you present a menu to the user and allow adding Dates (as described in Assignment 2), removing Dates, searching for a Date, displaying all Dates and quit. Keep presenting the menu until quit is selected.

The referred Assignment 2 code is below, so you can use the code and modify it based on the instructions above. Thank you!

#include <iostream>
#include "List.h"

int main() {
List LinkedList;

char input='y';
while(input=='y' || input=='Y'){
cout << "Hello User, Choose from the Below options:" << endl;
cout << "1. Print List" << endl;
cout << "2. Push to List" << endl;
cout << "3. Pop from List" << endl;
int choice;
std::cin >> choice;
if(choice == 1){
cout << "List: ";
LinkedList.print();
cout << endl;
}
else if (choice == 2) {
struct time t;
cout << "Input date in format dd/mm/yy:";
scanf("%d/%d/%d",&(t.date),&(t.month),&(t.year));
LinkedList.push(t);
}
else if (choice == 3){
struct time t=LinkedList.pop();
printf("%d/%d/%d was popped from list",t.date,t.month,t.year);
LinkedList.print();
cout << endl;
}
}
system("pause");
return 0;
}

struct time{
int date,month,year;
};

class List {
private:
class Node {
private:
Node* m_Next;
struct time m_data;
public:
Node(void):m_data(0), m_Next(0) {}
Node(struct time data):m_data(data), m_Next(0) {}

Node* getNext(void) {
return m_Next;
}
struct time getData(void) {
return m_data;
}

void setNext(Node* node) {
m_Next = node;
}
void setData(int data) {
m_data = data;
}
};

Node* m_Head;
int m_count;

public:
List(void):m_Head(0), m_count(0) {}

  
void print(void) {
Node* navigator = m_Head;
while (navigator != 0) {
struct time t = naviagtor->getData();
printf("%d/%d/%d\n",t.date,t.month,t.year);
navigator = navigator->getNext();
}
}

  
struct time pop(void) {
Node* temp = m_Head;
m_Head = m_Head->getNext();
m_count--;
return temp->getData();
}


void push(struct time data) {
Node* node = new Node(data);
Node* temp = m_Head;
m_Head = node;
m_Head->setNext(temp);
m_count++;
}

struct time getCount(void) {
return m_count;
}

};

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

#include<iostream>

using namespace std;

struct Node{
int data;
Node *next;
};

class Queue{
    public:
Node *front,*rear;
Queue(){front=rear=NULL;}

void insert(int n);
void deleteitem();
void display();
~Queue();
};

void Queue::insert(int n){
Node *temp=new Node;
if(temp==NULL){
cout<<"Overflow"<<endl;
return;
}
temp->data=n;
temp->next=NULL;

//for first node
if(front==NULL){
front=rear=temp;
}
else{
rear->next=temp;
rear=temp;
}
cout<<n<<" has been inserted successfully."<<endl;
}

void Queue::display(){
if(front==NULL){
cout<<"Underflow."<<endl;
return;
}
Node *temp=front;
//will check until NULL is not found
while(temp){
cout<<temp->data<<" ";
temp=temp->next;
}
cout<<endl;
}

void Queue :: deleteitem()
{
if (front==NULL){
cout<<"underflow"<<endl;
return;
}

cout<<front->data<<" is being deleted "<<endl;
if(front==rear)//if only one node is there
front=rear=NULL;
else
front=front->next;
}

Queue ::~Queue()
{
while(front!=NULL)
{
Node *temp=front;
front=front->next;
delete temp;
}
rear=NULL;
}


int main(){
Queue Q;
Q.display();

Q.insert(10);
Q.insert(24);
Q.insert(28);
Q.insert(32);
Q.insert(30);

Q.display();

Q.deleteitem();
Q.deleteitem();
Q.deleteitem();
Q.deleteitem();
Q.deleteitem();

return 0;
}

Add a comment
Know the answer?
Add Answer to:
Please answer in C++ Derive a class called Queue from the linked list described in Assignment...
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
  • Please answer in C++. Derive a class called Stack from the linked list described in Assignment...

    Please answer in C++. Derive a class called Stack from the linked list described in Assignment 2 (list of Dates). This means the Stack class will inherit all the properties (data and functions) of the linked list. But, since a stack only allows pushing and popping at the front of the list only, you will need to prevent the operations at the back. To do this, derive the Stack class in such a way that the base class (LinkedList) functions...

  • C++ program: Convert the classes to template classes #include <iostream> #include <string> using namespace std; class Node { private: int data; Node* next; public: Node(int...

    C++ program: Convert the classes to template classes #include <iostream> #include <string> using namespace std; class Node { private: int data; Node* next; public: Node(int data) { this->data=data; this->next = 0; } int getData(){return data;} Node* getNext(){return next;} void setNext(Node* next){this->next=next;} }; class LinkedList { private: Node* head = 0; public: int isEmpty() {return head == 0;} void print() { Node* currNode = head; while(currNode!=0) { cout << currNode->getData() << endl; currNode = currNode->getNext(); } } void append(int data) {...

  • PLEASE USE C++ Source Code Attached is a linked list with 2 nodes. You can use...

    PLEASE USE C++ Source Code Attached is a linked list with 2 nodes. You can use this or write a similar one. The assignment is to write 2 functions. One function will add another node at the end of the list. The other function will delete a node. Don't forget - No dangling pointers ! Example linked source code attached below #include<iostream> using namespace std; class Node { int data; Node *next; public: void setdata(int d) {data = d;} void...

  • C++ assignment about doubly linked list

    You are required to write the following functions using this class: class Doubly_linked_list // Use a class Doubly_linked_list to represent an object {     public:     // constructor initialize the nextPtr     Doubly_linked_list()     {         prevPtr = 0; // point to null at the beginning         nextPtr = 0; // point to null at the beginning     }     // get a number     int GetNum()     {         return number;     }     // set a number     void SetNum(int num)     {         number = num;     }     // get the prev pointer     Doubly_linked_list ...

  • // Node.h #ifndef NODE_H #define NODE_H class Node { private: int m_entry; Node *m_next; public: Node();...

    // Node.h #ifndef NODE_H #define NODE_H class Node { private: int m_entry; Node *m_next; public: Node(); Node(int entry); int getEntry() const; void setEntry(int entry); Node *getNext(); void setNext(Node *next); }; #endif // Node.cpp #include "Node.h" Node::Node() { m_entry = 0; m_next = nullptr; } Node::Node(int entry) { m_entry = entry; } int Node::getEntry() const { return m_entry; } void Node::setEntry(int entry) { m_entry = entry; } Node *Node::getNext() { return m_next; } void Node::setNext(Node *next) { m_next = next; }...

  • Q) Modify the class Linked List below to make it a Doubly Linked List. Name your...

    Q) Modify the class Linked List below to make it a Doubly Linked List. Name your class DoublyLinkedList. Add a method addEnd to add an integer at the end of the list and a method displayInReverse to print the list backwards. void addEnd(int x): create this method to add x to the end of the list. void displayInReverse(): create this method to display the list elements from the last item to the first one. Create a main() function to test...

  • Derive a class called Stack from the linked list described in Assignment 2 (list of Dates)....

    Derive a class called Stack from the linked list described in Assignment 2 (list of Dates). This means the Stack class will inherit all the properties (data and functions) of the linked list. But, since a stack only allows pushing and popping at the front of the list only, you will need to prevent the operations at the back. To do this, derive the Stack class in such a way that the base class (LinkedList) functions become private in the...

  • c++ modify the attached unsorted linked list class into a sorted linked list class #include <iostream>...

    c++ modify the attached unsorted linked list class into a sorted linked list class #include <iostream> using namespace std; template<class T> struct Node {     T data;//data field     Node * next;//link field     Node(T data) {       this->data = data;     } }; template<class T> class linked_list{ private:       Node<T> *head,*current; public:       linked_list(){//constructor, empty linked list         head = NULL;         current = NULL;       }       ~linked_list(){         current = head;         while(current != NULL) {          ...

  • C++ Assignment Project 1 - NodeList Building upon the the ListNode/List code I would like you...

    C++ Assignment Project 1 - NodeList Building upon the the ListNode/List code I would like you to extend the interface of a list to have these member functions as well. struct ListNode { int element; ListNode *next; } Write a function to concatenate two linked lists. Given lists l1 = (2, 3, 1)and l2 = (4, 5), after return from l1.concatenate(l2)the list l1should be changed to be l1 = (2, 3, 1, 4, 5). Your function should not change l2and...

  • above in the image is a linked list with 2 nodes. You can use this or...

    above in the image is a linked list with 2 nodes. You can use this or write a similar one. The assignment is to write 2 functions. One function will add another node at the end of the list. The other function will delete a node. Don't forget - No dangling pointers C++ language Make sure its corect and runs without errors and I promise to give you a thumbs up. #include iostream» using namespace std; class Node int 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