Data Structures and Algorithms — Lab 5
Topic
To Understand the concept of Stack (Last In, First Out) through some basic applications.
Objectives
• Understand the purpose of Stack and how it follows the Last in, First Out Approach.
• Explore the use of Push and Pop Operations.
• Practice the implementation of expression evaluations.
• Develop and manipulate Stack data structures with advanced functionalities.
• Gain familiarity with practical applications of Stack.
Outcomes
1. Develop a comprehensive understanding of creating and using Stack to enforce specific behaviors.
2. Gain experience implementing Push and Pop Operations to define the essential working of the stack
with the help of LIFO.
3. Build practical skills in implementing a Stack data structure with advanced functionality, such as
custom insertion and inserting elements at the Top.
4. Enhance problem-solving skills by designing functions that meet specific operational requirements,
such as Expressions Evaluation and Stack Reverse.
Content
The following sections will be covered during this lab session:
1. Implementation of Basic Stack Operations
• Push: Add an element to the top of the stack.
• Pop: Remove the element from the top of the stack.
• Top: Retrieve the top element without removing it.
• Empty: Check if the stack is empty.
• Full: Check if the stack is full or not.
2. Advanced Stack Manipulation
• Extend Stack operations with specialized scenarios:
➢ Stack reverse
➢ Brackets matching
Task Instructions
Students are required to complete the following tasks during lab time. Create a private repository on your
GitHub accounts. The name of the repository should be Lab-5-DSA. All files should be uploaded to this
repository, including the header and cpp files. Please note that the name of class-related files should be the
same as the class name. If there is only 1 task, we can name the main file Task_1.cpp or Task.cpp. We
recommend you work in .h files for classes only since we must work on templates.
Page 1 of 4
Task 1- Basic Stack’s Implementation using ADT
1. Create an Abstract Class AbstractStack<T> (Generic Template Class):
This class will define the interface (pure virtual functions) for stack operations.
The following pure virtual functions must be defined:
template <typename T>
class AbstractStack {
public:
virtual void push(T value) = 0;
virtual T pop() = 0;
virtual T top() const = 0;
virtual bool isEmpty() const = 0;
virtual bool isFull() const = 0;
virtual ~AbstractStack() {}
};
2. Create a Child Class myStack<T> (Generic Template Class):
This class will inherit from AbstractStack<T> and implement all the pure virtual
functions.
• void push(T value): Adds an element to the top of the stack.
• T pop(): Removes and returns the top element.
• T top() const: Returns the top element without removing it.
• bool isEmpty() const: Returns true if the stack is empty.
• bool isFull() const: Returns true if the stack is full.
3. Additional Function:
• void display() const: Prints all elements in the stack from top to bottom.
4. Implementation Requirements:
• Use a dynamically allocated array for stack storage.
• Define the maximum size of the stack via constructor.
5. Main Function:
In the main() function:
• Create an object of myStack<int> (you can use other data types as well for testing).
• Display a menu to the user to perform stack operations interactively.
Page 2 of 4
Task 2- Enhanced Stack with Minimum Element Tracker
Use the same AbstractStack and myStack structure from Question 1, but enhance the stack to
support an additional feature:
• Track the minimum element in the stack at any time, and retrieve it in O(1) time.
Task Details:
• Implement an additional function in myStack class:
➢ T getMin() const;
This function should return the current minimum element of the stack.
• Modify push() and pop() functions to maintain the minimum element properly whenever
elements are added or removed.
• You are allowed to use an additional stack internally to keep track of the minimum
elements.
• Keep the existing functions:
➢ push(), pop(), top(), isEmpty(), isFull(), display()
• Design the menu in main() to include an option for:
➢ "Show Minimum Element"
Example Menu for Main Function:
1. Push element
2. Pop element
3. Show top element
4. Check if stack is empty
5. Check if stack is full
6. Display stack elements
7. Show minimum element
8. Exit
Important Notes:
➢ Ensure to follow proper class structure and access specifiers.
➢ Make sure to handle edge cases (like popping from an empty stack).
➢ Maintain code readability and add comments where necessary for clarity.
Page 3 of 4
Task 3- Parking Lot Management System using Stack
Design Parking Lot Management System using stack data structure. In a small parking lot, cars
are parked in a straight line (like stack). The last car that entered will be the first to leave LIFO
system.
Details:
1. Use myCarStack to manage parked cars (store car numbers or license plates).
2. Set a limit of 8-Cars in line.
3. When a car arrives, push it into the stack.
4. When a car leaves:
➢ If it’s not the top car, remove cars on top temporarily (store them in a temporary stack).
➢ Take out the desired car.
➢ Put back the temporarily removed cars.
5. Allow viewing of:
➢ Car currently parked.
➢ Total cars parked.
➢ Park a new car.
➢ Remove a car by car number.
6. Search for a car:
➢ Check if a specific car is present in the parking lot by entering the car number.
Task 4- Text Editor History with Stack
Design a basic text editor where the user can type, delete, and undo or redo actions using
stack data structures. The text editor will allow users to type characters, delete characters, and
perform undo/redo operations. Each action taken by the user, whether typing or deleting a
character, will be stored in a stack. When a user types a character or deletes one, the
corresponding action will be pushed onto the undo stack. If the user wishes to undo an action, the
program will pop the last action from the stack and revert the text to the previous state. This
provides a simple but effective undo functionality, where the user can easily undo their most
recent changes. Additionally, to handle the redo functionality, another stack will be used. After
an action is undone, it can be pushed onto the redo stack, and the user can restore that action by
performing a redo operation. This design simulates a very basic text editor history, allowing
users to navigate back and forth between different versions of the text they’ve edited. The
program will maintain the current state of the text and allow the user to perform actions such as
adding or removing text and managing the history of changes with the undo and redo features.
Page 4 of 4