Question

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

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

// List.h

#include <iostream>

using namespace std;

struct time{

int date,month,year;

};

class List {

private:

class Node {

private:

               struct time m_data;

               Node* m_Next;

public:

Node(void): m_Next(0) {

               m_data.date = 0;

               m_data.month = 0;

               m_data.year = 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(struct time 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 = navigator->getData();

cout<<t.date<<"/"<<t.month<<"/"<<t.year<<endl;

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++;

}

int getCount(void) {

               return m_count;

}

};

class Stack: protected List

{

public:

               void push(struct time data);

               struct time pop();

               void print();

};

void Stack::push(struct time data)

{

               List::push(data);

}

struct time Stack::pop()

{

               return List::pop();

}

void Stack::print()

{

               List::print();

}

//end of List.h

// main.cpp

#include <iostream>

#include "List.h"

using namespace std;

int main() {

               Stack stack;

               int choice= 1;

               char ch;

               while(choice!=4){

               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;

               cout<<"4. Quit"<<endl;

               std::cin >> choice;

               if(choice == 1){

               cout << "Stack: "<<endl;;

               stack.print();

               cout << endl;

               }

               else if (choice == 2) {

               struct time t;

               cout << "Input date in format dd/mm/yy:";

               cin>>t.date>>ch>>t.month>>ch>>t.year;

               stack.push(t);

               }

               else if (choice == 3){

               struct time t=stack.pop();

               cout<<t.date<<"/"<<t.month<<"/"<<t.year<<" was popped from list"<<endl;

               //LinkedList.print();

               cout << endl;

               }else if(choice != 4)

                              cout<<"Invalid choice"<<endl;

               }

               system("pause");

               return 0;

}

//end of main.cpp

Output:

Add a comment
Know the answer?
Add Answer to:
Please answer in C++. Derive a class called Stack 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 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...

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

  • in c++ please include all of the following " template class, template function, singly linked list,...

    in c++ please include all of the following " template class, template function, singly linked list, the ADT stack, copy constructor, operator overloading, "try catch"and pointers Modify the code named "Stack using a Singly Linked List" to make the ADT Stack that is a template class has the code of the destructor in which each node is directly deleted without using any member function. As each node is deleted, the destructor displays the address of the node that is being...

  • 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 fill in this code to reverse a linked list: (written in C/C++) #include #include #include...

    Please fill in this code to reverse a linked list: (written in C/C++) #include #include #include using namespace std; /* Link list node */ struct Node { int data; // your code here }; /* Function to reverse the linked list */ static void reverse(struct Node** head_ref) { // your code here } /* Function to push a node */ void push(struct Node** head_ref, int new_data) { // your code here } /* Function to print linked list */ void...

  • Implement the stack queue data structure with a linked list implementation to get the given test...

    Implement the stack queue data structure with a linked list implementation to get the given test code in driver.cpp to work properly: driver.cpp code: #include <iostream> #include "stackLL.h" using namespace std; int main() { /////////////Test code for stack /////////////// stackLL stk; stk.push(5); stk.push(13); stk.push(7); stk.push(3); stk.push(2); stk.push(11); cout << "Popping: " << stk.pop() << endl; cout << "Popping: " << stk.pop() << endl; stk.push(17); stk.push(19); stk.push(23); while( ! stk.empty() ) { cout << "Popping: " << stk.pop() << endl; }...

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

  • Need help. write a C program stack-ptr.c that implements a stack using a link list. Below...

    Need help. write a C program stack-ptr.c that implements a stack using a link list. Below is a skeleton code to start with.Jjust edit to make thread friendly. examplpe: push(5, &top); push(10, &top); push(15, &top); int value = pop(&top); value = pop(&top); value = pop(&top); this program currently has a race condition. use Pthread mutex locks to fix the race conditions. test you now thread safe stack by creating 200 concurrent threads in main() that push and pop values. -use...

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

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