Linked list is a perfect data structure for the implementation of stack and queue. Especially for the queue, it avoids the headaches of wrapping-around manipulation for a queue implemented by an array. In this project, you are requested to implement both stack and queue by using a linked list (not an array).
In this project, you have an option to find one partner to form a team. However, at most two persons are allowed in each team to complete this project. In a team setting, remember to provide the names of both team members in the document when you submit the completed project to Canvas.
Question 1
Implement a template class Stack as defined by the following skeleton:
template class Stack { private: NodeType* topPtr; // It points to a singly-linked list public: Stack( ); // default constructor: Stack is created and empty Stack(const Stack &x); // copy constructor: implicitly called for a // deep copy void MakeEmpty(); // Stack is made empty; you should deallocate all the // the nodes of the linked list bool IsEmpty( ); // test if the stack is empty bool IsFull( ); // test if the stack is full; assume MAXITEM=5 int length( ); // return the number of elements in the stack void Print( ); // print the value of all elements in the stack in the sequence // from the top to bottom void Push(ItemType x); // insert x onto the stack
void Pop(ItemType &x); // delete the top element from the stack // Precondition: the stack is not empty ~Stack(); // Destructor: memory for nodes needs to be deallocated };
template struct NodeType { ItemType info; NodeType* next; };
In you main( ) routine, you need to test your class in the following cases:
Stack IntStack; int x; IntStack.Pop(x); IntStack.Push(11); IntStack.Push(22); cout IntStack2(IntStack); cout Stack StringStack; string y; StringStack.Pop(y); StringStack.Push(bob); cout StringStack2 = StringStack; cout Stack FloatStack; float z; FloatStack.Pop(z); FloatStack.Push(3.1); cout FloatStack2 = FloatStack; cout Linked list is a perfect data structure for the implementation of stack and queue Especially for the queue, it avoids the headaches of wrapping-around manipulation for a queue implemented by an array. In this project, you are requested to implement both stack and queue by using a linked list (not an array) In this project, you have an option to find one partner to form a team. However, at most two persons are allowed in each team to complete this project. In a team setting, remember to provide the names of both team members in the document when you submit the completed project to Canvas. Question 1 Implement a template class Stack as defined by the following skeleton: template class Stack private: NodeType ItemType topPtr; // It points to a singly-linked list public: Stack(); // default constructor: Stack is created and empty Stack(const Stack &); // copy constructor: implicitly called for a // deep copy void MakeEmpty); // Stack is made empty; you should deallocate all the // the nodes of the linked list bool IsEmpty); // test if the stack is empty bool IsFull( ); // test if the stack is full, assume MAXITEM=5 int length(); // return the number of elements in the stack void Print(); // print the value of all elements in the stack in the sequence // from the top to bottom void Push(ItemType x); / insert x onto the stack void Pop(ItemType &); // delete the top element from the stack -Stack); // Destructor: memory for nodes needs to be deallocated // Precondition: the stack is not empty template