Starting from:

$35

CSE030-Lab 9 Stacks Solved

(Exercise 1)  
In this part of the lab, you will be implementing your own Stack class, comprised of a class declaration (Stack.h) and class definition (Stack.cpp).  You will also create a simple main program to test your stack class (Exercise1.cpp).  This part of the Lab will use int as the type for elements in the Stack (Linked List nodes). In order to make the Stack implementation as easy as possible, your stack class will inherit the LinkedList class so that any functions available inside the LinkedList class can be used by the Stack class.  Take a look at the Inheritance_example_files from lecture to see how to use inheritance.  As you will see when you implement the Stack class, inheriting the LinkedList class will make this part of the lab very simple.  Your Stack class will be comprised of the following functions, which you need to implement (note that the Stack class does not need any variables, since those from the inherited LinkedList are all you need):

•       Default Constructor (Stack()): does nothing

•       Destructor (~Stack()): does nothing

•       void push(int value): inserts a new element (value) at the front of the stack, by calling the appropriate LinkedList function.

•       int pop(): removes the first element of the Stack, by calling the appropriate LinkedList function. It also returns the value of the element that has been popped.

•       int& top(): returns a reference to the top element of the Stack.

You also need to create your own main program (Exercise1.cpp) to test the various Stack functions:

•       Create a stack of at least 10 elements in your main program and call various member functions to manipulate the stack (as you have seen in your HW2 problem).

•       Call push to insert integer to the stack

•       Call top to return to the top of the stack

•       Check the size of the stack by calling a function inherited from Linked List.

•       Check if the stack is empty by calling a function inherited from Linked List.

•       Print the content of the stack by calling a function inherited from Linked List and verify if the member functions work correctly.

Note:

•       You do not need to create exceptions, so create a main program that does not call top or pop when the stack is empty.

•       Your Stack implementation should be very simple and short. Most, if not all, functions can be implemented using a single line of code. Remember that since you are inheriting the LinkedList class, you can call any functions/variables that were defined inside the LinkedList class.

 

 (Exercise 2)  
In this part of the lab, you will make the following changes to the code developed in Exercise 1:

•      Change your Stack and Linked List classes so that they use char type instead of int.  Name them Stack_char.h/Stack_char.cpp and LinkedList_char.h/ LinkedList_char.cpp to avoid the confusion between the classes created in Exercise 1. 

•      Create a main program (Exercise2.cpp) to produce the following output similar to what was illustrated in the lecture.   

•      You do not need to produce the table lines, but need to use tabs in between columns and newlines for each row.  Columns do not need to be aligned between rows.  

More products