0% found this document useful (0 votes)
0 views

Lab 08 - Stack

Lab 08 focuses on understanding stacks, their basic operations, and implementations using arrays and linked lists. Key operations include push, pop, and checking for underflow and overflow conditions. The lab tasks involve coding implementations of stacks and converting decimal numbers to binary using stacks.

Uploaded by

Ubaid Ahmad
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views

Lab 08 - Stack

Lab 08 focuses on understanding stacks, their basic operations, and implementations using arrays and linked lists. Key operations include push, pop, and checking for underflow and overflow conditions. The lab tasks involve coding implementations of stacks and converting decimal numbers to binary using stacks.

Uploaded by

Ubaid Ahmad
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

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.

2. Stack basic Operations


A stack is generally implemented with only two principle operations
a. Push adds an item to a stack.
b. Pop extracts the most recently pushed item from the stack.
Other methods such as
c. Top returns the item at the top without removing it.
d. isEmpty determines whether the stack has anything in it.
e. isFull checks whether the stack is full.
We also handle two errors with a stack. They are stack underflow and stack overflow. When we
try to pop an element from an empty stack, it is said that the stack underflowed. However, if the
number of elements exceeds the stated size of a stack, the stack is said to be overflowed.
3. Stack Implementation
There are two ways to implement stack
i. Static Implementation (Using arrays)
ii. Dynamic Implementation (Using linked lists)

4. Stack Implementation using Arrays


For the static implementation of stack an array will be used. This array will hold the stack
elements. The top of a stack is represented by an integer type variable which contains the index of
an array containing top element of a stack.

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;

void read(int* arr, int n)


{
for (int i = 0; i < n; i++)
{
arr[i]=2*(i+1);
}
}

void display(int* arr, int n)


{
for (int i = 0; i < n; i++)
{
cout << arr[i] << " ";
}
}

void Replace(int* arr, int n)


{
for (int i = 0; i < n; i++)
{
if (arr[i] % 4 == 0)
{
arr[i] = i;
}
}
}

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

Lab # 08 Rubric Evaluation Guideline:


# Qualities & Criteria 0 < Poor <= 1 1 < Satisfactory <= 2 2 < Excellent <=3

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.

You might also like