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.
#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.
Derive a class called Stack from the linked list described in Assignment 2 (list of Dates)....
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 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. // 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 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...
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...
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. };
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...
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...
c++ only Program 2: Linked List Class For this problem, let us take the linked list we wrote in a functional manner in a previous assignment and convert it into a Linked List class. For extra practice with pointers we’ll expand its functionality and make it a doubly linked list with the ability to traverse in both directions. Since the list is doubly linked, each node will have the following structure: struct Node { int number; Node * nextNode;...
C++ Given this linked list class: class LinkedList { private: Node * head; public: LinkedList(); void prepend(int val); int sum(); }; Implement a sum method which will return the sum of the values stored in the linked list.