0% found this document useful (0 votes)
41 views36 pages

05 Stacks and Its Implementation

This document provides an overview of stacks and their implementation in 3 sentences or less: Stacks are linear data structures that follow the last-in, first-out (LIFO) principle, allowing insertion and deletion of elements only from one end called the top. Common stack operations include push to insert an element and pop to delete the top element. Stacks have array-based and linked list-based implementations and applications in reversing sequences, tracking function calls, expression evaluation, and more.

Uploaded by

Haseeb Muhammad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
41 views36 pages

05 Stacks and Its Implementation

This document provides an overview of stacks and their implementation in 3 sentences or less: Stacks are linear data structures that follow the last-in, first-out (LIFO) principle, allowing insertion and deletion of elements only from one end called the top. Common stack operations include push to insert an element and pop to delete the top element. Stacks have array-based and linked list-based implementations and applications in reversing sequences, tracking function calls, expression evaluation, and more.

Uploaded by

Haseeb Muhammad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 36

CS250: Data Structures and Algorithms

Stacks and its Implementation

Instructor: Dr. Sohail Iqbal

1
Recap
Introduction to data
structures & algorithms
Pointers, structures & arrays
List ADT
Array lists
Linked Lists (single,
double & circular)

Stack and its applications

Queues
2
Source: https://pic1.zhimg.com/v2-5dd21bea8b50af177e31351ed0a2ea8d_r.jpg?
source=172ae18b
Stacks

3
Today‘s lecture
 What is a Stack?
 Operations on a Stack
 Initialize a stack
 Push (insert) an element
 Pop (delete) an element
 Read the top element
 status: empty, full

 Array-based and linked-list based Implementation.

 Applications of Stack
 Reverse a sequence of number using stack
 Keep track of function calls in a program
 Testing the correctness of a mathematical expression
 Converting a mathematical expression from infix to postfix and prefix notations
 Solving a mathematical expression in postfix form

4
What is a Stack?
Definition:
Conceptually, a stack is a data structure that allows
adding and removing elements in a particular
order
More specifically, in an order such that items can be
inserted or deleted to the collection of already inserted
items from one end only, called the top of the stack

5
Stack

 Stack is a linear data structure in which input and output


operations are constrained at only at the top end.
 Insertion or removal of an item is allowed only at the top end.
 Only the element at the top end can be accessed

6
Stack
Stack is said to have “First In, Last Out" (FILO) or
“Last In, First Out” (LIFO) behaviour meaning
that the first item added to a stack will be the
last item removed from a stack

Example:
Which is the first coin to pick up from
the stack of gold coins?

7
Basics of stack
Size: The number of elements on the stack

Top: Points to the top most element on the stack.


This refers to NULL if stack is empty or size = 0
Because we think of stacks in terms of the physical analogy,
we usually draw them vertically (so the top is really on top)

8
Stack Operations
Primarily two operations performed on one end
only

Push: Add an item on the top of stack

Pop: Remove an item at the top of stack

9
Stack Operations
Push Concerns
What if when the stack is full?
If the push operation is called when a stack is already full, the
condition is called “stack overflow”
Pop Concerns
What if the stack is empty
If the pop operation is called when a stack is already empty, the
condition is called “stack underflow”

Solution:
Before any push, check if the stack is already filled or not
If it is filled then generate error message e.g., Stack Overflow

Before pop, check if stack is not already empty


If it is empty then generate error message e.g., Stack Underflow

10
Stack Operations
push(item) // Push an item onto the stack

pop( ) // Pop the top item off the


stack

isEmpty() // Return true if stack is empty

isFull() // Return true if stack is full

top( ) or peek() // Return (check) value of top11 item


Applications
Real life
• Stack of trays in cafeteria
• Piles of books in library
Computer Science
• Compilers use stacks to evaluate expressions & syntax
parsing
• Program execution stack
• Undo operations
• Expression conversion
• Infix to post-, pre-fix and vice versa
• Tower of Hanoi
• And many more … 12
Tower of Hanoi

Reading: Tower of Hanoi from Wikipedia 13


Implementation of Stack in C++

 A stack is an ordered collection of items, and


C++ already contains a data type that is an
ordered collection of items: array.
 So, it is tempting to use Array to implement
stack.
 However, there are certain limitations of array,
when used for implementing stack.
 The number of elements in array is fixed , and
is decided at declaration time.

14
Implementation of Stack in C++

 During run time, this size can not be changed.


 There is no such upperbound on the stack.
Stack during its life time continues to grow and
shrink, whereas array remains static.
 Array is not stack, but array serves as the
home/container for the stack.

15
Take a sequence: -1.5 2.3 6.7

Push onto a stack one


number at a time.

Then pop each number off the stack

16
-1.5 2.3 6.7

Push onto a stack one


number at a time.

Top

17
2.3 6.7

Push onto a stack one


number at a time.

Top -1.5

18
6.7

Push onto a stack one


number at a time.

Top 2.3
-1.5

19
Push onto a stack one
Top 6.7 number at a time.

2.3
-1.5

20
Top 6.7
2.3
-1.5
Then pop each number off the stack

21
Top 2.3
-1.5
Then pop each number off the stack

6.7 22
Top -1.5
Then pop each number off the stack

6.7 2.3 23
Top

6.7 2.3 -1.5 24


Pseudocode for sequence reverse (){

initialize the Stack


loop{
input nextNumber
if (end of input) then {exit loop}
if (Stack is not full) then {push nextNumber onto Stack}
}

loop {
if (Stack is empty) then {exit loop}
pop nextNumber off the Stack
output nextNumber
}

}
} 25
Choice of implementation
Array based: Stack Maximum stack size is known
ahead of time

Linked List based: Stack Maximum stack size


unknown

26
Array based stack
When using an array to implement a stack

The array's first element should represent the bottom of


the stack

The last occupied location in the array represents the


stack's top

This avoids shifting of elements of the array when


we push or remove elements from stack

27
Array based stack
Allocate an array of some size (pre-defined)
Maximum N elements in stack

Bottom stack element stored at element 0

Last index in the array is the top

Increment top when one element is pushed,


similarly decrement after each pop

28
Array based stack operations
int stack[size]; int Pop() {
int top = -1; int element = -1;
If (!isEmpty()) {

void Push(int element) { element = stack[top];

if (!isFull()) { stack[top--] = -1;


}
stack[++top] = element;
return element;
}
}
}

bool isEmpty() {
bool isFull() {
return top<0;
return top==size-1;
} }

29
Linked List based implementation
How can we implement Stack
using linked lists?
LIFO
Push = Insert at head\

Pop = Remove at head

IsEmpty = Empty List

Peek()/Top() = reading element at the top without popping it out.

Clear = Destroy List

30
Linked List based implementation

31
Linked List based implementation
void main ()
{
...
stack();
stack.push(5);
...
}

32
Linked List based implementation

33
Practice:
Write one of the unique application of Stack in 2 min

34
Lab Project
• Choose an Interesting topic relevant to DSA

• Make a team of 2-3 people.

• Two teams can work close to each other.

• Proposal Defense. 16 Oct 2023 [10 lab marks]

• Final Defense. 16 Dec 2023. [20 lab marks]


35
Thank You!

Any Questions?

36

You might also like