You are on page 1of 6

CS200 Introduction to Programming

Spring 2018-19
Section 1
Monday 25 March 2019
Lab 9: Exercise
Lab Guidelines
1. Make sure you get your work graded before the lab time ends.
2. Talking to each other is not permitted. If you have a question, ask the lab assistants.
3. The object is not simply to get the job done in the way that is asked for in the lab.
4. Any cheating case will be reported to Disciplinary Committee without any delay.
5. Please submit all your work on LMS folder designated for the lab (i.e. “Lab09”) in one
zip file with your roll number i.e 21000022.zip before the time the lab ends.

Coding Conventions:

1. Constants are ALL_CAPS.


2. Variables are all_small.
3. Functions are firstWordSmallRestCapitalized.
4. User defined data types AllWordsCapitalized.
5. All curly brackets defining a block must be vertically aligned.

Learning Objective:

1. PO-02 Develop proficiency in the practice of computing.


2. CO-02 To help students analyze and solve programming problems
3. LO-02 Critical Thinking and Analysis
4. LO-03 Problem Solving
5. LO-05 Responsibility

Objectives:
- To get familiar with Linked Lists
- To use a given linked list to store, retrieve, remove, search given data

Important:
- Do not write any inline functions. All members functions shall be outside of the class.
Exercises

Task 1 [30]
Implementing A Stack using Linked Lists
A Stack is a ubiquitous data structure used for storing data. It uses LIFO (Last in First out) policy
whereby the first element you push in is the last one to be popped out and the last to be pushed in
is the first to be popped out. Much like building blocks, one element is stacked on top of another
element and to get an element from below, you first must take off the elements from the top until
you reach that block you want to extract. To give you a visual representation of the above,
consider the image below:

Your job is to implement a stack class built upon the concepts of linked lists that you’ve learnt
i.e each element in the stack shall be a node, all of which will be linked together to form a stack.
The stack size can change dynamically and will have a minimum size of 0 i.e empty stack.
Task 1 (25)

Our stack will only hold integers so make a struct Node with a value field and a pointer to the
next element in the stack. (5)

Make a Stack class with the following private variables: (5)

• Pointer to the head of the stack


• The current size of the stack.

NOTE:

What you choose as the head is up to you, whether you choose the first element
inserted into the stack as the head or the last element inserted into the stack, either
will work however you should try to have an efficient implementation.

Also make the following public functions:

• Stack(); (5)
o Initialize the Stack with a size of 0.
• ~Stack(); (5)
o Delete the Stack.
• int getSize(); (5)
o Get the current size of the stack.
Task 2 (25)

Add three more public functions.

• void push(int element);


o Insert this element into the stack. (10)
• void pop();
o Delete the last element inserted into the stack. If there are no elements then print
a message saying stack is empty (10)
• int top();
o Retrieve last element inserted on the stack without deleting it. (5)

NOTE:

• Remember to update the size of the stack after these operations.


Task 3 (25)

Overload the assignment operator (‘=’) for the Stack class such that running this code will copy
the contents of one stack onto the other (copying from s1 to s2). (15)

Stack s1, s2;


...
s2 = s1;

Write a test code to verify that the assignment operator is correctly overloaded for Stack. (10)

NOTE:

• s1 and s2 should NOT be pointing at the same address.


• Both the stacks should exist independently in the heap.
• Popping and deleting an element from one stack should not impact the other stack.
Task 4 (25)

Overload the << operator for the Stack class such that running this code will print all the
members of the stack in a column, the first one at the bottom, and the last one at the top. (15)

For example:

Stack s2;
s2.push(2);
s2.push(3);
s2.push(4);
cout << s2;

Output:
| 4 |
| 3 |
| 2 |
| _ |

Write a test code to verify that the assignment operator is correctly overloaded for Stack. (10)

You might also like