0% found this document useful (0 votes)
4 views25 pages

Dsa L7

The document provides an introduction to the stack data structure, emphasizing its LIFO (Last In, First Out) principle and essential operations such as push, pop, and peek. It discusses implementations using arrays and linked lists, along with real-world applications like function call management and expression evaluation. The conclusion highlights the importance of stacks in computing for their efficient operations.

Uploaded by

seunadepoju64
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)
4 views25 pages

Dsa L7

The document provides an introduction to the stack data structure, emphasizing its LIFO (Last In, First Out) principle and essential operations such as push, pop, and peek. It discusses implementations using arrays and linked lists, along with real-world applications like function call management and expression evaluation. The conclusion highlights the importance of stacks in computing for their efficient operations.

Uploaded by

seunadepoju64
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
You are on page 1/ 25

1

Dr. Mohammed Al-Hubaishi


Data Structures &
Algorithms

Introduction to C++ Stack

DR . MOHAMMED AL-HUBAISHI
2
Introduction to Stack Data Structure

• Stack is fundamental for many algorithms


• Choose implementation based on requirements
• Understanding stack operations is crucial
• Real-world applications are numerous
3
Stack Definition

► A stack is a linear data structure in which insertions and


deletion are allowed only at the end.
► A stack follows the LIFO (Last In, First Out) principle.

This means that the last element added to the stack is the
first element to be removed.
4
Operations on STACK
► Push
► Pop

1. Any object (either book or a coin) can be accessed only from the top.
2. Any object can be added only at the top.
Dr. Mohammed Al-Hubaishi
5
Operations on STACK

Dr. Mohammed Al-Hubaishi


6
Stack operations

Dr. Mohammed Al-Hubaishi


7
Insertion in STACK : PUSH

Dr. Mohammed Al-Hubaishi


8
Deletion in Stack: POP

Dr. Mohammed Al-Hubaishi


9
Display in Stack:

Dr. Mohammed Al-Hubaishi


10
Operations on STACK

- Push: Add element to top


- Pop: Remove element from top
- Peek/Top: View top element
- isEmpty: Check if stack is empty
• Memory allocation is contiguous (array) or
linked (linked list)
11
Implementation using Arrays

A stack can be implemented using an array.


The top of the stack is the index of the last element that was
added to the stack.
The size of the array determines the maximum number of
elements that can be stored in the stack.

https://onlinegdb.com/E4Xb9HyXv
12
Implementation using Linked Lists

A stack can also be implemented using a linked list.


In this case, the top of the stack is a pointer to the last element that was added to
the stack.
The linked list allows for dynamic resizing of the stack, which means that the
stack can grow as needed.

https://onlinegdb.com/fs7VogR9a
13
Real-world Applications of Stack

Stacks have a wide variety of applications, including:


★ Function Call Management
○ - Managing function calls and returns in program execution
★ Expression Evaluation
○ - Evaluating arithmetic expressions
○ - Converting infix to postfix/prefix
★ Undo Operations
○ - Implementing undo feature in text editors
★ Browser History
○ - Managing back/forward navigation
★ Syntax Parsing
○ - Checking balanced parentheses
○ - Syntax validation in compilers
14
Checking balanced parentheses
15
Checking balanced parentheses
example

https://onlinegdb.com/HcqwyOg1p
16
Reversing Strings using Stack

String input :
REVERSE
A simple application of stack is reversing strings.
► To reverse a string, the characters of string are pushed
onto the stack one by one as the string is read from left to
right.
E TOP
► Once all the characters of string are pushed onto stack, S
they are popped one by one. Since the character last
R
pushed in comes out first, subsequent pop operation
results in the reversal of the string. E

https://onlinegdb.com/z-xhqIJTz V

R
Dr. Mohammed Al-Hubaishi
Stack
17
sort a stack using recursion

https://onlinegdb.com/ICdXXgNno
18
Stack class

Define the stack


class and its
functions. And also
specify the top
variable and
maximum size of the
array used in the
stack class.

Dr. Mohammed Al-Hubaishi


19
Stack Functions

A constructor in C++ is a special method that is automatically


called when an object of a class is created.

isEmpty method to check if the


Stack is empty or not.

Dr. Mohammed Al-Hubaishi


20
Main function

output

Dr. Mohammed Al-Hubaishi


21
Stack class : push function

Dr. Mohammed Al-Hubaishi


22
Stack class : pop function

Dr. Mohammed Al-Hubaishi


23
Stack class : print function

Dr. Mohammed Al-Hubaishi


24
Conclusion

Stacks are fundamental in both theoretical and


practical computing, offering efficient LIFO
operations.
25
Book References

Dr. Mohammed Al-Hubaishi

You might also like