0% found this document useful (0 votes)
6 views16 pages

Stack Data Structure Explained

Chapter 4 discusses stacks, a linear data structure that operates on the Last In, First Out (LIFO) principle. It details key operations such as push, pop, and peek, along with implementations using arrays and linked lists. The chapter also includes examples and exercises related to stack operations.

Uploaded by

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

Stack Data Structure Explained

Chapter 4 discusses stacks, a linear data structure that operates on the Last In, First Out (LIFO) principle. It details key operations such as push, pop, and peek, along with implementations using arrays and linked lists. The chapter also includes examples and exercises related to stack operations.

Uploaded by

omarkamrl1234
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

N

W E
Data S
Structure

Chapter 4: Stack
Presented By :
Dr Thanaa Mohamed Hassan
Dr Menna Mamdouh
Stack
A stack is a linear data structure that follows
the Last In, First Out (LIFO) principle. This
means that the most recently added element is
the first one to be removed.

Stacks can be
implemented using either
arrays or linked lists,
depending on the specific
requirements of the
application.
Key Features of a Stack
LIFO (Last In, First Out): The most recent element
added to the stack is the first to be removed.
Push Operation: Adding an element to the top of the
stack.
Pop Operation: Removing the top element from the
stack.
Peek/Top Operation: Viewing the top element without
removing it.
Empty: A function that checks if the stack is empty.
Common Stack Operations:
 Push(x): Insert element x onto the stack.
 Pop(): Remove the top element from the
stack.
 Peek()/Top(): Return the top element without
removing it.
 IsEmpty(): Check if the stack is empty.
• Size(): Return the number of elements in the
stack.
Stack depending on Linked List
 Data: The information or value
that the node holds.
 Pointer/Reference: A
reference (or pointer) to the next
node in the stack.
The Top of the linked list
refers to the first node, while the
Tail is a last node's pointer (in a
singly linked list) points to
nullptr or None (indicating the
end of the list).
Representation of Stack
A stack can be visualized as a vertical structure with
elements being added or removed from the top.
Top -> [5] <- Pop or Peek
[3]
[2]
[1] <- Bottom
In this stack, 5 is at the top, and it will be the first
element removed if a pop operation is called.
Representation of Stack (Puch)
Implementing Stack using
Linked List

Creating a node

Add to first
Implementing Stack using
Linked List

Remove from first

Check if empty
Implementing Stack using
Linked List

Print top element


Implementing Stack using
Linked List

Traverse

Print top element


Testing Stack using Linked List
Part 2: Stack Using Dynamic
Array
Example 1

• Question
Write a program to reverse a string (Hello)
using a stack (push each character, then pop
all).
Exercise 5 — Count elements
in a linked-list stack
Problem:
Build a stack using a singly linked list and write
stack_size(head) to count nodes.
• We represent each node as a dictionary:
{'value': x, 'next': pointer}.
• Head points to the top of the stack
.
END SLIDE

Thank You 

You might also like