Lab 08 - Stack
Lab 08 - Stack
Outcomes
1. What is stack?
2. Stack basics operations.
3. Stack Implementation using arrays.
4. Stack implementation using linked list.
5. Lab Tasks
1. What is Stack?
A stack is used to store elements where the Last element In is the First one Out (LIFO). A
common model of a stack is a plate or coin stacker. Plates are "pushed" onto to the top and
“pooped” off the top. New elements are added or pushed onto the top of the stack. The first
element to be removed or popped is taken from the top - the last one in.
We are going to use the element at the index 1 of the array as the bottom of the stack and the last
element of the stack as the top of the stack as described in the picture given below.
Since we need to add and remove elements from the top of the stack, we are going to use a pointer
which is always going to point the top most element of the stack. It will point to the element at
index 0 when the stack is empty.
So, let’s first write a function to check whether a stack is empty or not.
IS_EMPTY(S)
if S.top == 0
return TRUE
return FALSE
We are going to consider only the elements from 1 to S.top as part of the stack. It might be
possible that there are other elements also in the array but we are not going to consider them as
stack.
To add an item to a stack (push), we just need to increment the top pointer by 1 and add the
element there.
PUSH(S, x) → Here, S is the stack and x is the item we are going to push to the stack.
Let’s suppose that S.size is the maximum size of the stack. So, if S.top+1 exceeds S.size, then the
stack is overflowed. So, we will first check for the overflowing of the stack and then accordingly
add the element to it.
PUSH(S, x)
S.top = S.top+1
if S.top > S.size
Error "Stack Overflow"
else
S[S.top] = x
Similarly to remove an item from a stack (pop), we will first check if the stack is empty or not. If
it is empty, then we will throw an error of "Stack Underflow", otherwise remove the element from
the stack and return it.
POP(S)
if IS_EMPTY(S)
Error “Stack Underflow”
else
S.top = S.top-1
return S[S.top+1]
5. Lab Tasks
Dry run the blow code and write behavior of code after each line.
#include<iostream>
using namespace std;
int main()
{
int arr[10];
read(arr, 10);
display(arr, 10);
Replace(arr, 10);
cout << endl;
display(arr, 10);
}
1. Write program to Implement a stack using
a. Singly/doubly linked list.
b. Array.
2. Write a C++ program that uses a stack to convert a decimal number to its binary representation
using an array.
Lab # 08 Marks distribution
ER1 ER6 ER8
Task 3 points 3 points 4 points
ER1 Task Completion Minimal or no program Some tasks were completed, but All tasks were completed, and
functionality was achieved. the program has errors or the program runs without
incomplete functionalities. errors.
# Qualities & Criteria 0 < Poor <= 1 1 < Satisfactory <= 2 2 < Excellent <=3
ER6 Program Output Output is inaccurate or Output is mostly accurate but may Output is clear, accurate, and
poorly presented. lack labels, captions, or well presented with labels,
formatting. captions, and proper
formatting.
# Qualities & Criteria 0 < Poor <= 1.5 1.5 < Satisfactory <= 3 3 < Excellent <= 4
ER8 Question & Answer Answers some questions but Answers most questions Answers all questions
not confidently or based on confidently and based on lab task confidently and demonstrates a
lab task knowledge. knowledge. deep understanding of the
given lab task.