SlideShare a Scribd company logo
1
2
Name Syed Husnain Naqvi
Roll no 05
Presented By
Presentation on the topic stack
University of Kotli Azad Jammu and Kashmir
Subject Data structures and Algorithms
Department Data Science
Session 2022-26
Faculty Computing and Engineering
Stack
3
Introduction to stack
Stack is a linear data structure with a pre-defined capacity, which means that it can store the elements of a
limited size. It is a data structure that follows some order to insert and delete the elements, and that order
can be LIFO or FILO.
• Stack is a data structure in which data is added and removed at only one end called the top.
• To add (push) an item to the stack, it must be placed on the top of the stack.
• To remove (pop) an item from the stack, it must be removed from the top of the stack too.
• Thus, the last element that is pushed into the stack, is the first element to be popped out of the stack.
4
An Example of Stack
2
8
1
7
2
7
2
1
7
2
1
7
2
8
1
7
2
8
1
7
2
top
top
top
top
top
top
Push(8) Push(2)
pop()
pop(8)
pop(1)
5
Example of stack
6
Stack ADT (abstract data type)
A stack is an abstract data type that serves as a collection of
elements with different operations.
• Push, which adds an element to the collection.
• Pop, which removes the element.
• full/empty, which determine the stack is full or empty.
7
Applications of Stacks:
•Function calls: Stacks are used to keep track of the return addresses of
function calls, allowing the program to return to the correct location after a
function has finished executing.
•Recursion: Stacks are used to store the local variables and return addresses of
recursive function calls, allowing the program to keep track of the current state of
the recursion.
•Expression evaluation: Stacks are used to evaluate expressions in postfix
notation (Reverse Polish Notation).
•Syntax parsing: Stacks are used to check the validity of syntax in programming
languages and other formal languages.
•Memory management: Stacks are used to allocate and manage memory in
some operating systems and programming languages.
8
Advantages of Stacks
•Simplicity: Stacks are a simple and easy-to-understand data structure,
making them suitable for a wide range of applications.
•Efficiency: Push and pop operations on a stack can be performed in
constant time (O(1)), providing efficient access to data.
•Last-in, First-out (LIFO): Stacks follow the LIFO principle, ensuring that the
last element added to the stack is the first one removed. This behavior is
useful in many scenarios, such as function calls and expression evaluation.
•Limited memory usage: Stacks only need to store the elements that have
been pushed onto them, making them memory-efficient compared to other
data structures.
9
Disadvantages of Stacks
•Limited access: Elements in a stack can only be accessed from the
top, making it difficult to retrieve or modify elements in the middle of the
stack.
•Potential for overflow: If more elements are pushed onto a stack
than it can hold, an overflow error will occur, resulting in a loss of data
•.
•Not suitable for random access: Stacks do not allow for random
access to elements, making them unsuitable for applications where
elements need to be accessed in a specific order.
•Limited capacity: Stacks have a fixed capacity, which can be a
limitation if the number of elements that need to be stored is unknown
or highly variable.
10
Implementation of stack
Two implementations in stack is:
• Stack As Array
• Stack As Linked List
11
Representation of a Stack as a array
We are given a stack of elements: 12 , 08 , 21 , 33 , 18 , 40.
12
Representation of a Stack as a array
We are given a stack of elements: 12 , 08 , 21 , 33 , 18 , 40.
13
In an array-based implementation.
The push operation is implemented by incrementing the index of the top
element and storing the new element at that index.
public void push(Object object){
if (count == array.length)
throw new ContainerFullException();
else
array[count++] = object;
}
Push() method adds an element at the top the stack.
It takes as argument an Object to be pushed.
It first checks if there is room left in the stack. If no room is left, it throws a
ContainerFullException exception. Otherwise, it puts the object into the array, and
then increments count variable by one.
14
In an array-based implementation.
The pop operation is implemented by storing the element at the top, decrementing
the index of the top element and returning the value stored.
public Object pop(){
if(count == 0)
throw new ContainerEmptyException();
else {
Object result = array[--count];
array[count] = null;
return result;
}
}
The pop method removes an item from the stack and returns that item.
The pop method first checks if the stack is empty. If the stack is empty, it throws a
ContainerEmptyException. Otherwise, it simply decreases count by one and returns
the item found at the top of the stack
15
getTop() method first checks if the stack is empty.
getTop() method is a stack accessor which returns the top item in the stack
without removing that item. If the stack is empty, it throws a
ContainerEmptyException. Otherwise, it returns the top item found at
position count-1.
public Object getTop(){
if(count == 0)
throw new ContainerEmptyException();
else
return array[count – 1];
}
In an array-based implementation.
16
Representation of a Stack as a Linked List
A Linked List is a Data Structure which
consists of two parts:
•The data/value part.
•The pointer which gives the location of the
next node in the list.
17
18
Difference between array and link list
19
20
Thank you
21

More Related Content

Similar to stack_presentaton_HUSNAIN[2].pojklklklptx (20)

PPT
stack presentation
Shivalik college of engineering
 
PPT
2 a stacks
Nguync91368
 
PPTX
Data Structure Stack operation in python
deepalishinkar1
 
PPTX
Stack in Sata Structure
Muhazzab Chouhadry
 
PPTX
CD3291 2.5 stack.pptx
mareeswari15
 
PPTX
DSEC1.pptx stack exchange communities that I am not đźš­ hi nahi
ganesh209832
 
PPTX
DATA STRUCTURE AND COMPUTER ALGORITHMS LECTURE 2
emathemathematics
 
PPTX
Data structure , stack , queue
Rajkiran Nadar
 
PPTX
Stacks and Queue,Concept of Stack,LIFO,Fifo,
shaikhdaniyal8603
 
PPT
An Introduction to Stack Data Structures
berggold2024
 
PPT
introduction stacks in data structures and algorithms
sneham64878
 
PPT
01-intro_stacks.ppt
soniya555961
 
PDF
What is Stack, Its Operations, Queue, Circular Queue, Priority Queue
Balwant Gorad
 
PDF
stack 1.pdf
hafsa40
 
PPTX
Stack and Queue
Selvaraj Seerangan
 
DOCX
Stacks
Acad
 
PPT
Data structures
Manaswi Sharma
 
PPTX
Stack and Queue by M.Gomathi Lecturer
gomathi chlm
 
PPTX
VCE Unit 03vv.pptx
skilljiolms
 
stack presentation
Shivalik college of engineering
 
2 a stacks
Nguync91368
 
Data Structure Stack operation in python
deepalishinkar1
 
Stack in Sata Structure
Muhazzab Chouhadry
 
CD3291 2.5 stack.pptx
mareeswari15
 
DSEC1.pptx stack exchange communities that I am not đźš­ hi nahi
ganesh209832
 
DATA STRUCTURE AND COMPUTER ALGORITHMS LECTURE 2
emathemathematics
 
Data structure , stack , queue
Rajkiran Nadar
 
Stacks and Queue,Concept of Stack,LIFO,Fifo,
shaikhdaniyal8603
 
An Introduction to Stack Data Structures
berggold2024
 
introduction stacks in data structures and algorithms
sneham64878
 
01-intro_stacks.ppt
soniya555961
 
What is Stack, Its Operations, Queue, Circular Queue, Priority Queue
Balwant Gorad
 
stack 1.pdf
hafsa40
 
Stack and Queue
Selvaraj Seerangan
 
Stacks
Acad
 
Data structures
Manaswi Sharma
 
Stack and Queue by M.Gomathi Lecturer
gomathi chlm
 
VCE Unit 03vv.pptx
skilljiolms
 

Recently uploaded (20)

PPTX
ENGLISH 8 WEEK 3 Q1 - Analyzing the linguistic, historical, andor biographica...
OliverOllet
 
PPTX
Applied-Statistics-1.pptx hardiba zalaaa
hardizala899
 
PDF
Module 2: Public Health History [Tutorial Slides]
JonathanHallett4
 
PDF
Virat Kohli- the Pride of Indian cricket
kushpar147
 
PPTX
PROTIEN ENERGY MALNUTRITION: NURSING MANAGEMENT.pptx
PRADEEP ABOTHU
 
PDF
Tips for Writing the Research Title with Examples
Thelma Villaflores
 
PPTX
Introduction to pediatric nursing in 5th Sem..pptx
AneetaSharma15
 
PDF
EXCRETION-STRUCTURE OF NEPHRON,URINE FORMATION
raviralanaresh2
 
PPTX
Electrophysiology_of_Heart. Electrophysiology studies in Cardiovascular syste...
Rajshri Ghogare
 
PPTX
Rules and Regulations of Madhya Pradesh Library Part-I
SantoshKumarKori2
 
PPTX
Digital Professionalism and Interpersonal Competence
rutvikgediya1
 
PPTX
How to Track Skills & Contracts Using Odoo 18 Employee
Celine George
 
PPTX
Gupta Art & Architecture Temple and Sculptures.pptx
Virag Sontakke
 
PPTX
HEALTH CARE DELIVERY SYSTEM - UNIT 2 - GNM 3RD YEAR.pptx
Priyanshu Anand
 
PPTX
Sonnet 130_ My Mistress’ Eyes Are Nothing Like the Sun By William Shakespear...
DhatriParmar
 
PPTX
Basics and rules of probability with real-life uses
ravatkaran694
 
PPTX
Python-Application-in-Drug-Design by R D Jawarkar.pptx
Rahul Jawarkar
 
PPTX
YSPH VMOC Special Report - Measles Outbreak Southwest US 7-20-2025.pptx
Yale School of Public Health - The Virtual Medical Operations Center (VMOC)
 
PPTX
Unlock the Power of Cursor AI: MuleSoft Integrations
Veera Pallapu
 
PPTX
Artificial Intelligence in Gastroentrology: Advancements and Future Presprec...
AyanHossain
 
ENGLISH 8 WEEK 3 Q1 - Analyzing the linguistic, historical, andor biographica...
OliverOllet
 
Applied-Statistics-1.pptx hardiba zalaaa
hardizala899
 
Module 2: Public Health History [Tutorial Slides]
JonathanHallett4
 
Virat Kohli- the Pride of Indian cricket
kushpar147
 
PROTIEN ENERGY MALNUTRITION: NURSING MANAGEMENT.pptx
PRADEEP ABOTHU
 
Tips for Writing the Research Title with Examples
Thelma Villaflores
 
Introduction to pediatric nursing in 5th Sem..pptx
AneetaSharma15
 
EXCRETION-STRUCTURE OF NEPHRON,URINE FORMATION
raviralanaresh2
 
Electrophysiology_of_Heart. Electrophysiology studies in Cardiovascular syste...
Rajshri Ghogare
 
Rules and Regulations of Madhya Pradesh Library Part-I
SantoshKumarKori2
 
Digital Professionalism and Interpersonal Competence
rutvikgediya1
 
How to Track Skills & Contracts Using Odoo 18 Employee
Celine George
 
Gupta Art & Architecture Temple and Sculptures.pptx
Virag Sontakke
 
HEALTH CARE DELIVERY SYSTEM - UNIT 2 - GNM 3RD YEAR.pptx
Priyanshu Anand
 
Sonnet 130_ My Mistress’ Eyes Are Nothing Like the Sun By William Shakespear...
DhatriParmar
 
Basics and rules of probability with real-life uses
ravatkaran694
 
Python-Application-in-Drug-Design by R D Jawarkar.pptx
Rahul Jawarkar
 
YSPH VMOC Special Report - Measles Outbreak Southwest US 7-20-2025.pptx
Yale School of Public Health - The Virtual Medical Operations Center (VMOC)
 
Unlock the Power of Cursor AI: MuleSoft Integrations
Veera Pallapu
 
Artificial Intelligence in Gastroentrology: Advancements and Future Presprec...
AyanHossain
 
Ad

stack_presentaton_HUSNAIN[2].pojklklklptx

  • 1. 1
  • 2. 2 Name Syed Husnain Naqvi Roll no 05 Presented By Presentation on the topic stack University of Kotli Azad Jammu and Kashmir Subject Data structures and Algorithms Department Data Science Session 2022-26 Faculty Computing and Engineering
  • 4. Introduction to stack Stack is a linear data structure with a pre-defined capacity, which means that it can store the elements of a limited size. It is a data structure that follows some order to insert and delete the elements, and that order can be LIFO or FILO. • Stack is a data structure in which data is added and removed at only one end called the top. • To add (push) an item to the stack, it must be placed on the top of the stack. • To remove (pop) an item from the stack, it must be removed from the top of the stack too. • Thus, the last element that is pushed into the stack, is the first element to be popped out of the stack. 4
  • 5. An Example of Stack 2 8 1 7 2 7 2 1 7 2 1 7 2 8 1 7 2 8 1 7 2 top top top top top top Push(8) Push(2) pop() pop(8) pop(1) 5
  • 7. Stack ADT (abstract data type) A stack is an abstract data type that serves as a collection of elements with different operations. • Push, which adds an element to the collection. • Pop, which removes the element. • full/empty, which determine the stack is full or empty. 7
  • 8. Applications of Stacks: •Function calls: Stacks are used to keep track of the return addresses of function calls, allowing the program to return to the correct location after a function has finished executing. •Recursion: Stacks are used to store the local variables and return addresses of recursive function calls, allowing the program to keep track of the current state of the recursion. •Expression evaluation: Stacks are used to evaluate expressions in postfix notation (Reverse Polish Notation). •Syntax parsing: Stacks are used to check the validity of syntax in programming languages and other formal languages. •Memory management: Stacks are used to allocate and manage memory in some operating systems and programming languages. 8
  • 9. Advantages of Stacks •Simplicity: Stacks are a simple and easy-to-understand data structure, making them suitable for a wide range of applications. •Efficiency: Push and pop operations on a stack can be performed in constant time (O(1)), providing efficient access to data. •Last-in, First-out (LIFO): Stacks follow the LIFO principle, ensuring that the last element added to the stack is the first one removed. This behavior is useful in many scenarios, such as function calls and expression evaluation. •Limited memory usage: Stacks only need to store the elements that have been pushed onto them, making them memory-efficient compared to other data structures. 9
  • 10. Disadvantages of Stacks •Limited access: Elements in a stack can only be accessed from the top, making it difficult to retrieve or modify elements in the middle of the stack. •Potential for overflow: If more elements are pushed onto a stack than it can hold, an overflow error will occur, resulting in a loss of data •. •Not suitable for random access: Stacks do not allow for random access to elements, making them unsuitable for applications where elements need to be accessed in a specific order. •Limited capacity: Stacks have a fixed capacity, which can be a limitation if the number of elements that need to be stored is unknown or highly variable. 10
  • 11. Implementation of stack Two implementations in stack is: • Stack As Array • Stack As Linked List 11
  • 12. Representation of a Stack as a array We are given a stack of elements: 12 , 08 , 21 , 33 , 18 , 40. 12
  • 13. Representation of a Stack as a array We are given a stack of elements: 12 , 08 , 21 , 33 , 18 , 40. 13
  • 14. In an array-based implementation. The push operation is implemented by incrementing the index of the top element and storing the new element at that index. public void push(Object object){ if (count == array.length) throw new ContainerFullException(); else array[count++] = object; } Push() method adds an element at the top the stack. It takes as argument an Object to be pushed. It first checks if there is room left in the stack. If no room is left, it throws a ContainerFullException exception. Otherwise, it puts the object into the array, and then increments count variable by one. 14
  • 15. In an array-based implementation. The pop operation is implemented by storing the element at the top, decrementing the index of the top element and returning the value stored. public Object pop(){ if(count == 0) throw new ContainerEmptyException(); else { Object result = array[--count]; array[count] = null; return result; } } The pop method removes an item from the stack and returns that item. The pop method first checks if the stack is empty. If the stack is empty, it throws a ContainerEmptyException. Otherwise, it simply decreases count by one and returns the item found at the top of the stack 15
  • 16. getTop() method first checks if the stack is empty. getTop() method is a stack accessor which returns the top item in the stack without removing that item. If the stack is empty, it throws a ContainerEmptyException. Otherwise, it returns the top item found at position count-1. public Object getTop(){ if(count == 0) throw new ContainerEmptyException(); else return array[count – 1]; } In an array-based implementation. 16
  • 17. Representation of a Stack as a Linked List A Linked List is a Data Structure which consists of two parts: •The data/value part. •The pointer which gives the location of the next node in the list. 17
  • 18. 18
  • 19. Difference between array and link list 19
  • 20. 20