SlideShare a Scribd company logo
Unit II - Stack
Data Structures BCS301
Stack
What is a Stack?
• A stack is a data structure of ordered items such that items can be inserted
and removed only at one end.
• A stack is a LIFO (Last-In/First-Out) data structure
2
What are some applications of stacks?
Program execution
Parsing
Evaluating postfix expressions
Stack Representation & Basic Operations
Stack operations may involve initializing the stack, using it and
then de-initializing it. Apart from these basic stuffs, a stack is
used for the following two primary operations −
•push() − Pushing (storing) an element on the stack.
•pop() − Removing (accessing) an element from the stack.
To use a stack efficiently, we need to check the status of stack
as well. For the same purpose, the following functionality is
added to stacks −
•peek() − get the top data element of the stack, without
removing it.
•isFull() − check if stack is full.
•isEmpty() − check if stack is empty.
4
STACK
push
create
pop
isfull
isempty
Stack- Abstract Data Types (ADTs)
• An abstract data type (ADT) is an abstraction of a data structure. It is a
specification of a collection of data and the operations that can be
performed on it.
• An ADT specifies:
• Data stored - A finite sequence of elements.
• Operations on the data
• Create
• Push: Insert element at top
• Top: Return top element
• Pop: Remove and return top element
• IsEmpty: test for emptyness
• Error conditions associated with operations
5
Stack implementation using Array
•In the array implementation, we would:
•Declare an array of fixed size (which determines the maximum size
of the stack).
•Keep a variable which always points to the “top” of the stack.
•Contains the array index of the “top” element.
Implementing a Stack
• There are two ways we can implement a stack:
• Using an array
• Using a linked list
7
Push Operation
The process of putting a new data element onto stack is known as a Push Operation. Push
operation involves a series of steps −
•Step 1 − Checks if the stack is full.
•Step 2 − If the stack is full, produces an error and exit.
•Step 3 − If the stack is not full, increments top to point next empty space.
•Step 4 − Adds data element to the stack location, where top is pointing.
•Step 5 − Returns success.
Stack[1:n]; // Array of size n.
top = 0; // initial value of top is 0.
Push(data, top)
{
if (top = = n){
Print: “stack is full”;
}
top = top + 1;
stack[top] = data
}
Pop Operation
Accessing the content while removing it from the stack, is known as a Pop Operation. In an array
implementation of pop() operation, the data element is not actually removed, instead top is
decremented to a lower position in the stack to point to the next value. But in linked-list
implementation, pop() actually removes data element and deallocates memory space.
A Pop operation may involve the following steps −
•Step 1 − Checks if the stack is empty.
•Step 2 − If the stack is empty, produces an error and exit.
•Step 3 − If the stack is not empty, accesses the data element at which top is pointing.
•Step 4 − Decreases the value of top by 1.
•Step 5 − Returns success. Stack[1:n]; // Array of size n.
Pop(top)
{
if (top = = 0){
Print: “stack is Empty”;
}
data=stack[top];
top = top - 1;
return data;
}
Implementation by Linked Lists
• Can use a Linked List as implementation of stack
9/23/2020 CS 201
Top of Stack = Front of Linked-List
StackLL
lst
a1 a2 a3 a4
head
LinkedListItr
The presentation on stack data structure
Push Operation:
Struct node {
int data;
struct node * next;
};
Struct node *top = NULL; // top is initially null
Push(top, data)
{
struct node * temp = Getnode(); // allocate memory
if (temp = = NULL){
Print: “Overflow”;
}
temp - > data = data;
temp -> next = top;
top = temp;
}
Pop Operation:
Struct node {
int data;
struct node * next;
};
Struct node *top = NULL; // top is initially null
Pop(top)
{
int item;
if (top = = NULL){
Print: “Underflow”;
}
item = top -> data;
top = top -> next;
return item;
}
Stack Summary
• Stack Operation Complexity for Different
Implementations
Vectors 14
Array
Fixed-Size
Array
Expandable (doubling
strategy)
List
Singly-
Linked
Pop() O(1) O(1) O(1)
Push(o) O(1) O(n) Worst Case
O(1) Best Case
O(1) Average Case
O(1)

More Related Content

PDF
PDF
Chapter 5 Stack and Queue.pdf
PPTX
stack_ppt_DSA(sudipta samanta).pptx push,pop,peek operation
PPT
Stack data structures with definition and code
PDF
What is Stack, Its Operations, Queue, Circular Queue, Priority Queue
PPTX
PPTX
DSEC1.pptx stack exchange communities that I am not 🚭 hi nahi
PDF
Chapter 5 Stack and Queue.pdf
stack_ppt_DSA(sudipta samanta).pptx push,pop,peek operation
Stack data structures with definition and code
What is Stack, Its Operations, Queue, Circular Queue, Priority Queue
DSEC1.pptx stack exchange communities that I am not 🚭 hi nahi

Similar to The presentation on stack data structure (20)

PPTX
5.-Stacks.pptx
PDF
The Stack (Data Structccccccccccccccccccc
PPTX
Stack and its operations
PDF
04 stacks
PDF
STACK ( LIFO STRUCTURE) - Data Structure
PPTX
Data Structure.pptx
PDF
PPTX
Data Structures and Agorithm: DS 06 Stack.pptx
PPTX
Unit II - LINEAR DATA STRUCTURES
PDF
4-Stack --------------------------------in C++.pdf
PDF
Stack push pop
PDF
Chapter 4 stack
PDF
Stacks-and-Queues.pdf
PPTX
Stack PPT.pptx
PDF
Data structure.pdf
DOCX
ADT STACK and Queues
PPTX
CD3291 2.5 stack.pptx
PDF
Data structure week y 5
PPTX
Stack in C.pptx
PPT
week 7,8,10,11 alll files included from .ppt
5.-Stacks.pptx
The Stack (Data Structccccccccccccccccccc
Stack and its operations
04 stacks
STACK ( LIFO STRUCTURE) - Data Structure
Data Structure.pptx
Data Structures and Agorithm: DS 06 Stack.pptx
Unit II - LINEAR DATA STRUCTURES
4-Stack --------------------------------in C++.pdf
Stack push pop
Chapter 4 stack
Stacks-and-Queues.pdf
Stack PPT.pptx
Data structure.pdf
ADT STACK and Queues
CD3291 2.5 stack.pptx
Data structure week y 5
Stack in C.pptx
week 7,8,10,11 alll files included from .ppt
Ad

Recently uploaded (20)

PPTX
CYBER-CRIMES AND SECURITY A guide to understanding
PPTX
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
PDF
Embodied AI: Ushering in the Next Era of Intelligent Systems
PPTX
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
PDF
Well-logging-methods_new................
PPTX
Lesson 3_Tessellation.pptx finite Mathematics
PPTX
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
PPTX
Strings in CPP - Strings in C++ are sequences of characters used to store and...
PDF
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
PDF
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
PPTX
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
PDF
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
PPTX
OOP with Java - Java Introduction (Basics)
PPTX
web development for engineering and engineering
PDF
composite construction of structures.pdf
PDF
Arduino robotics embedded978-1-4302-3184-4.pdf
PDF
Digital Logic Computer Design lecture notes
PPT
Project quality management in manufacturing
PDF
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
PDF
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
CYBER-CRIMES AND SECURITY A guide to understanding
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
Embodied AI: Ushering in the Next Era of Intelligent Systems
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
Well-logging-methods_new................
Lesson 3_Tessellation.pptx finite Mathematics
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
Strings in CPP - Strings in C++ are sequences of characters used to store and...
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
OOP with Java - Java Introduction (Basics)
web development for engineering and engineering
composite construction of structures.pdf
Arduino robotics embedded978-1-4302-3184-4.pdf
Digital Logic Computer Design lecture notes
Project quality management in manufacturing
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
Ad

The presentation on stack data structure

  • 1. Unit II - Stack Data Structures BCS301
  • 2. Stack What is a Stack? • A stack is a data structure of ordered items such that items can be inserted and removed only at one end. • A stack is a LIFO (Last-In/First-Out) data structure 2 What are some applications of stacks? Program execution Parsing Evaluating postfix expressions
  • 3. Stack Representation & Basic Operations Stack operations may involve initializing the stack, using it and then de-initializing it. Apart from these basic stuffs, a stack is used for the following two primary operations − •push() − Pushing (storing) an element on the stack. •pop() − Removing (accessing) an element from the stack. To use a stack efficiently, we need to check the status of stack as well. For the same purpose, the following functionality is added to stacks − •peek() − get the top data element of the stack, without removing it. •isFull() − check if stack is full. •isEmpty() − check if stack is empty.
  • 5. Stack- Abstract Data Types (ADTs) • An abstract data type (ADT) is an abstraction of a data structure. It is a specification of a collection of data and the operations that can be performed on it. • An ADT specifies: • Data stored - A finite sequence of elements. • Operations on the data • Create • Push: Insert element at top • Top: Return top element • Pop: Remove and return top element • IsEmpty: test for emptyness • Error conditions associated with operations 5
  • 6. Stack implementation using Array •In the array implementation, we would: •Declare an array of fixed size (which determines the maximum size of the stack). •Keep a variable which always points to the “top” of the stack. •Contains the array index of the “top” element.
  • 7. Implementing a Stack • There are two ways we can implement a stack: • Using an array • Using a linked list 7
  • 8. Push Operation The process of putting a new data element onto stack is known as a Push Operation. Push operation involves a series of steps − •Step 1 − Checks if the stack is full. •Step 2 − If the stack is full, produces an error and exit. •Step 3 − If the stack is not full, increments top to point next empty space. •Step 4 − Adds data element to the stack location, where top is pointing. •Step 5 − Returns success. Stack[1:n]; // Array of size n. top = 0; // initial value of top is 0. Push(data, top) { if (top = = n){ Print: “stack is full”; } top = top + 1; stack[top] = data }
  • 9. Pop Operation Accessing the content while removing it from the stack, is known as a Pop Operation. In an array implementation of pop() operation, the data element is not actually removed, instead top is decremented to a lower position in the stack to point to the next value. But in linked-list implementation, pop() actually removes data element and deallocates memory space. A Pop operation may involve the following steps − •Step 1 − Checks if the stack is empty. •Step 2 − If the stack is empty, produces an error and exit. •Step 3 − If the stack is not empty, accesses the data element at which top is pointing. •Step 4 − Decreases the value of top by 1. •Step 5 − Returns success. Stack[1:n]; // Array of size n. Pop(top) { if (top = = 0){ Print: “stack is Empty”; } data=stack[top]; top = top - 1; return data; }
  • 10. Implementation by Linked Lists • Can use a Linked List as implementation of stack 9/23/2020 CS 201 Top of Stack = Front of Linked-List StackLL lst a1 a2 a3 a4 head LinkedListItr
  • 12. Push Operation: Struct node { int data; struct node * next; }; Struct node *top = NULL; // top is initially null Push(top, data) { struct node * temp = Getnode(); // allocate memory if (temp = = NULL){ Print: “Overflow”; } temp - > data = data; temp -> next = top; top = temp; }
  • 13. Pop Operation: Struct node { int data; struct node * next; }; Struct node *top = NULL; // top is initially null Pop(top) { int item; if (top = = NULL){ Print: “Underflow”; } item = top -> data; top = top -> next; return item; }
  • 14. Stack Summary • Stack Operation Complexity for Different Implementations Vectors 14 Array Fixed-Size Array Expandable (doubling strategy) List Singly- Linked Pop() O(1) O(1) O(1) Push(o) O(1) O(n) Worst Case O(1) Best Case O(1) Average Case O(1)