Question

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

Previous

------------------------------------Linked list below ----------------------assignment 2 below does not need to be done------------------

Define a structure called Date with only month, day and year as its integer private members and to be used as the node type for a linked list.

Define a class called LinkedList with a Date pointer called head as its private member to be used to point to the first (head) node of a linked list.

Define a constructor to initialize head to NULL, destructor to free all dynamically allocated nodes, a copy constructor, overloaded assignment operator and a display() member function for displaying all the nodes' data (all the dates).

Also, include a push_front() public member function for adding a new node at the front of the list, a remove_front()public member function for removing the node at the front, a pop_front() public member function for displaying the front node, a push_back(), a remove_back() and a pop_back() public member function for inserting a new node at the back, for removing the node at the back and for displaying the node at the back, respectively, a search() member function for searching the list for a given date:

int LinkedList::search(const Date& date)

which will return the node number (starting from 0) where the date was found or will return -1 if not found.

Write a main function to test the class, which will first instantiate a LinkedList and then keep asking the user if he or she wants to add a new node (Date) to the list. If the answer is 'y' or 'Y', it will allocate dynamic memory for a new Date structure and push it at the front of the list, until the user answers with a 'n' or 'N' to creating a new node.

Then, when finished with creating the list, the program must continuously present a menu to the user with options to:

1. Display all elements of the list (using display function)

2. Insert new node - which will ask to front or back (using push functions)

3. Remove node - which will ask from front or back (using remove functions)

4. Find a node - which will ask for a date to search for (using search function)

5. Display a node - which will ask if the front or the back node (using pop functions)

6. Quit

Submit separate cpp files for main program and function definitions and the header file for defining the class and the structure.

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

#include <iostream>
using namespace std;

class stack:private LinkedList{
   void push(Date d){
       push_back(d);
   }
   Date pop(){
       Date d = pop_back();
       remove_back();
       return d;
   }
   int searchDate(Date d){
       return search(d);

   }
   void DisplayDates(){
       display();
   }
}
int main(){
   bool y=true;
   int choice;
   stack s;
   do{
       cout<<"1. Add a date."<<endl;
       cout<<"2. Remove a date."<<endl;
       cout<<"3. Search a date."<<endl;
       cout<<"4. Display all dates."<<endl;
       cout<<"5. Quit"<<endl;
       cin>>choice;
       switch(choice){
           case 1: cout<<"Enter date: ";
                   int month,date,year;
                   cin>>month>>date>>year;
                   Date d=Date(month,date,year);
                   s.push(d);
                   break;
           case 2: s.pop();
                   break;
           case 3: cout<<"Enter date: ";
                   int month,date,year;
                   cin>>month>>date>>year;
                   Date d=Date(month,date,year);
                   if(s.searchDate()!=-1)
                       cout<<"Date found at "<<s.searchDate()<<endl;
                   else
                       cout<<"Date not found"<<endl;
                   break;
           case 4: s.DisplayDates();
                   break;
           case 5: y=false;
                   break;
       }
   }while(y);
return 0;
}

The question has been answered while assuming that the class LinkedList is already declared according to the given specifications.

Add a comment
Know the answer?
Add Answer to:
Derive a class called Stack from the linked list described in Assignment 2 (list of Dates)....
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...

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

  • // Java Queue LinkedList Assignment // A queue is implemented using a singly linked list. //...

    // Java Queue LinkedList Assignment // A queue is implemented using a singly linked list. // the variable: back "points" at the first node in the linked list // new elements ( enqueued) are added at the back // the variable: front "points" at the last node in the linked list. // elements are removed (dequeued) from the front // // Several queue instance methods are provided for you; do not change these // Other instance methods are left for...

  • Design and implement your own linked list class to hold a sorted list of integers in ascending order. The class should h...

    Design and implement your own linked list class to hold a sorted list of integers in ascending order. The class should have member function for inserting an item in the list, deleting an item from the list, and searching the list for an item. Note: the search function should return the position of the item in the list (first item at position 0) and -1 if not found. In addition, it should member functions to display the list, check if...

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

  • C++ functions using linked list A templated stack class that stores type T with the following...

    C++ functions using linked list A templated stack class that stores type T with the following public functions: template <class T> class Stack{ - void Push(T t) - adds t to the top of the stack - T Pop() - asserts that the stack is not empty then removes and returns the item at the top of the stack. };

  • Using Java You are given a Node class and a List class: public class Node {...

    Using Java You are given a Node class and a List class: public class Node {    int   data;     Node next; } public class List {     Node first; } You are also given a Stack class. The following functions are available for use: public class Stack { public boolean isEmpty(){}; public void push(int n){}; public int pop(){};} Write a Java method snglyRevToStck that pushes the data found in a linked list t in reverse order into the stack...

  • // Header code for stack // requesting to create source code using C++ #ifndef Stack_h #define...

    // Header code for stack // requesting to create source code using C++ #ifndef Stack_h #define Stack_h #include <stdio.h> #include <string> #include <iostream> using namespace std; class Stack { public: Stack(); ~Stack(); bool empty(); string top(); void push(const string &val); void pop(); void display(ostream &out); private: class Node { public: string word; Node *next; }; Node *tos; }; #endif Header file provided above. PLS create source code for functions declared in the header. If changes are need no make in...

  • **HELP** Write a function that takes a linked list of items and deletes all repetitions from the ...

    **HELP** Write a function that takes a linked list of items and deletes all repetitions from the list. In your implementation, assume that items can be compared for equality using ==, that you used in the lab. The prototype may look like: void delete_repetitions(LinkedList& list); ** Node.h ** #ifndef Node_h #define Node_h class Node { public: // TYPEDEF typedef double value_type; // CONSTRUCTOR Node(const value_type& init_data = value_type( ), Node* init_link = NULL) { data_field = init_data; link_field = init_link;...

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