SlideShare a Scribd company logo
AAllggoorriitthhmmss aanndd DDaattaa 
SSttrruuccttuurreess 
Ahmad Khan 
Lecture 4 
Oxford University Data Structures Using C++ by Dr Varsha Patil Press © 2012
Stack 
Oxford University Data Structures Using C++ by Dr Varsha Patil Press © 2012 
2
Objectives 
Understand all the aspects of a stack as data type including : 
Last In, First Out (LIFO) data access 
Push, Pop, and other stack operations 
Contiguous implementation of a stack 
Learn realization of stack using arrays(: Contiguous stack) 
Learn to choose appropriate realization suitable for practical applications 
Learn and implement the multi-stacks 
Use of stacks in expression conversion, recursion, reversing data and many 
more 
Oxford University Data Structures Using C++ by Dr Varsha Patil Press © 2012 
3
 A stack is an ordered list in which all insertions and deletions 
Oxford University Data Structures Using C++ by Dr Varsha Patil Press © 2012 
4 
are made at one end, called the top 
 In computer programming processing of function calls and 
their terminations uses stack. Stack is used to remember the 
place where the call was made; so that it can return there after 
the function is complete
Following are some examples in which where we need to use stacks 
are generally used: 
Handling function calls in programs very often restricts the access 
only at one end. In such implementation we need to use stacks. We 
can keep track of the return address to earlier function after 
furnishing/finishing a function call using stacks 
If we intend to store a group of data together in a sequential 
manner in computer’s memory, then arrays can be one of the 
possible data structures. 
Oxford University Data Structures Using C++ by Dr Varsha Patil Press © 2012 
5
Arrays 
 An array is a finite ordered collection of homogeneous data 
elements which provides direct access (or random access) to any 
of its elements. 
 An array as a data structure is defined as a set of pairs (index, 
value) such that with each index a value is associated. 
index — indicates the location of an element in an array. 
value - indicates the actual value of that data element. 
 Declaration of an array in ‘C++’: 
int Array_A[20]; 
Oxford University Data Structures Using C++ by Dr Varsha Patil Press © 2012 
6
Examples of Stacks 
Oxford University Data Structures Using C++ by Dr Varsha Patil Press © 2012 
7 
Following figure shows some examples of stack 
1.Stack of Book 
2.Stack of chairs 
3.Stack of cups
Primitive Operations 
Oxford University Data Structures Using C++ by Dr Varsha Patil Press © 2012 
8 
1) The Stack Full Condition (Stack Capacity =3)
Oxford University Data Structures Using C++ by Dr Varsha Patil Press © 2012 
9 
2) The Pop Operation
Oxford University Data Structures Using C++ by Dr Varsha Patil Press © 2012 
10 
3) The Empty Stack
Oxford University Data Structures Using C++ by Dr Varsha Patil Press © 2012 
11 
4) The Get Top Operation
Oxford University Data Structures Using C++ by Dr Varsha Patil Press © 2012 
12 
Representation Of Stack S Using Sequential 
Organization (Arrays)
Oxford University Data Structures Using C++ by Dr Varsha Patil Press © 2012 
13 
……….. C B A 
top 
 The elements are stored in the stack from the first location onwards 
 The first element is stored at 0th location of the array ‘Stack’ which means at 
STACK[0], the second element at STACK[1], ith element at Stack [i–1], and nth 
element at Stack[n–1] 
 Associated with the array will be an integer variable, top, which points to the top 
element in the stack 
 The initial value of top is −1, when the stack is empty 
 It can hold elements from index 0 and maximum it can grow up to n−1 as this is the 
static stack using arrays
14 
Implementation of Stack 
Stack Class 
class STACK 
{ 
private: 
int Stack[50]; 
int MaxCapacity; 
int top; 
public: 
STACK( ) { MaxCapacity= 50; top=-1;} 
int GetTop( ); 
int Pop( ); 
void Push( int Element); 
int Empty( ); 
int CurrSize( ); 
int IsFull (); 
}; 
Data Structures in C++ by 
Dr. Varsha Patil Oxford University Press © 2012 14
15 
Implementation of Stack 
GetTop, Pop, Empty 
int STACK :: GetTop () 
{ 
if (!Empty()) 
return (Stack[top]); 
} 
int STACK :: Pop () 
{ 
if (!Empty()) 
return (Stack[top--]); 
} 
int STACK :: Empty () 
{ 
if (top==-1) 
return 1; 
else 
return 0; 
} 
Data Structures in C++ by 
Dr. Varsha Patil Oxford University Press © 2012 15
16 
Implementation of Stack 
IsFull, CurrSize, Push 
int STACK :: IsFull () 
{ 
if (top==MaxCapacity-1) 
return 1; 
else 
return 0; 
} 
int STACK :: CurrSize() 
{ 
return(top+1); 
} 
void STACK :: Push (int Element) 
{ 
if (!IsFull()) 
Stack[++top]=Element; 
} 
Data Structures in C++ by 
Dr. Varsha Patil Oxford University Press © 2012 16
STACK S; 
S.Push(1); 
S.Push(2); 
cout<<"t Top element of stack = "<<S.GetTop()<<endl; 
cout<<"t After pop = "<<S.Pop()<<endl; 
cout<<"t After pop = "<<S.Pop()<<endl; 
getch(); 
17 
Implementation of Stack 
main 
int main( ) 
{ 
} 
Data Structures in C++ by 
Dr. Varsha Patil Oxford University Press © 2012 17

More Related Content

PPTX
5. Queue - Data Structures using C++ by Varsha Patil
PPS
Data Structure
PPTX
3. Stack - Data Structures using C++ by Varsha Patil
PPTX
4. Recursion - Data Structures using C++ by Varsha Patil
PPTX
1. Fundamental Concept - Data Structures using C++ by Varsha Patil
PPTX
9. Searching & Sorting - Data Structures using C++ by Varsha Patil
PPTX
10. Search Tree - Data Structures using C++ by Varsha Patil
PPTX
6. Linked list - Data Structures using C++ by Varsha Patil
5. Queue - Data Structures using C++ by Varsha Patil
Data Structure
3. Stack - Data Structures using C++ by Varsha Patil
4. Recursion - Data Structures using C++ by Varsha Patil
1. Fundamental Concept - Data Structures using C++ by Varsha Patil
9. Searching & Sorting - Data Structures using C++ by Varsha Patil
10. Search Tree - Data Structures using C++ by Varsha Patil
6. Linked list - Data Structures using C++ by Varsha Patil

What's hot (20)

PPTX
7. Tree - Data Structures using C++ by Varsha Patil
PPTX
14. Files - Data Structures using C++ by Varsha Patil
PPTX
16. Algo analysis & Design - Data Structures using C++ by Varsha Patil
PPTX
13. Indexing MTrees - Data Structures using C++ by Varsha Patil
PPT
stacks in algorithems and data structure
PPTX
8. Graph - Data Structures using C++ by Varsha Patil
PPTX
Data Structure
PDF
PDF
Ii pu cs practical viva voce questions
PDF
Week 2 - Data Structures and Algorithms
PPTX
Data structure
PPT
Introduction of data structure
PPTX
11. Hashing - Data Structures using C++ by Varsha Patil
PDF
PPTX
DATA STRUCTURE IN C LANGUAGE
PDF
Data Structures 01
PPT
Basic data-structures-v.1.1
PPT
Chapter 10: hashing data structure
PPT
Data Structure In C#
PPTX
Data Structure and Algorithms
7. Tree - Data Structures using C++ by Varsha Patil
14. Files - Data Structures using C++ by Varsha Patil
16. Algo analysis & Design - Data Structures using C++ by Varsha Patil
13. Indexing MTrees - Data Structures using C++ by Varsha Patil
stacks in algorithems and data structure
8. Graph - Data Structures using C++ by Varsha Patil
Data Structure
Ii pu cs practical viva voce questions
Week 2 - Data Structures and Algorithms
Data structure
Introduction of data structure
11. Hashing - Data Structures using C++ by Varsha Patil
DATA STRUCTURE IN C LANGUAGE
Data Structures 01
Basic data-structures-v.1.1
Chapter 10: hashing data structure
Data Structure In C#
Data Structure and Algorithms
Ad

Viewers also liked (20)

PPTX
Stack data structure
PPSX
PPTX
STACKS IN DATASTRUCTURE
PPTX
PPT
Queue and stacks
PPT
Recursion
PPSX
Stacks Implementation and Examples
PPT
មេរៀនៈ Data Structure and Algorithm in C/C++
PPT
Computer notes - Josephus Problem
PPTX
Stack using Array
PPTX
Lecture 10 data structures and algorithms
PDF
Stacks and queues
PPTX
Structured query language functions
PPTX
Classes and objects1
PPTX
Constructors and destructors
DOCX
Advanced data structures using c++ 3
PPTX
Structured query language constraints
PPS
PPTX
The Stack And Recursion
PDF
Discrete Mathematics S. Lipschutz, M. Lipson And V. H. Patil
Stack data structure
STACKS IN DATASTRUCTURE
Queue and stacks
Recursion
Stacks Implementation and Examples
មេរៀនៈ Data Structure and Algorithm in C/C++
Computer notes - Josephus Problem
Stack using Array
Lecture 10 data structures and algorithms
Stacks and queues
Structured query language functions
Classes and objects1
Constructors and destructors
Advanced data structures using c++ 3
Structured query language constraints
The Stack And Recursion
Discrete Mathematics S. Lipschutz, M. Lipson And V. H. Patil
Ad

Similar to Stacks in algorithems & data structure (20)

PPTX
Stack and Queue
PDF
PPT
2 a stacks
PPTX
Data structure , stack , queue
PPTX
Advanced data structures slide 2 2+
DOCX
Stacks in data structure
PDF
What is Stack, Its Operations, Queue, Circular Queue, Priority Queue
PPTX
STACK IN DATA STRUCTURE .pptx
PPT
introduction to stack data structures chapter 1
PPT
MODULE 01-INTRODUCTION TO STACK-PRESENTATION
PPT
stack.ppt
PDF
Fjdkkdnncmckkgkhkhkkhkhkhkhkhkhkhkhkhkhhl
PPT
introduction stacks in data structures and algorithms
PPT
An Introduction to Stack Data Structures
PPT
01-intro_stacks.ppt
PPT
03 stacks and_queues_using_arrays
PPT
Stack linked list
PPTX
Data Structures and Agorithm: DS 06 Stack.pptx
PPTX
Stack and Queue by M.Gomathi Lecturer
Stack and Queue
2 a stacks
Data structure , stack , queue
Advanced data structures slide 2 2+
Stacks in data structure
What is Stack, Its Operations, Queue, Circular Queue, Priority Queue
STACK IN DATA STRUCTURE .pptx
introduction to stack data structures chapter 1
MODULE 01-INTRODUCTION TO STACK-PRESENTATION
stack.ppt
Fjdkkdnncmckkgkhkhkkhkhkhkhkhkhkhkhkhkhhl
introduction stacks in data structures and algorithms
An Introduction to Stack Data Structures
01-intro_stacks.ppt
03 stacks and_queues_using_arrays
Stack linked list
Data Structures and Agorithm: DS 06 Stack.pptx
Stack and Queue by M.Gomathi Lecturer

Recently uploaded (20)

PPTX
Transform Your Business with a Software ERP System
PDF
Upgrade and Innovation Strategies for SAP ERP Customers
PDF
Odoo Companies in India – Driving Business Transformation.pdf
PDF
Nekopoi APK 2025 free lastest update
PDF
Digital Strategies for Manufacturing Companies
PDF
System and Network Administration Chapter 2
PPTX
Essential Infomation Tech presentation.pptx
PDF
Design an Analysis of Algorithms I-SECS-1021-03
PDF
AI in Product Development-omnex systems
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
PDF
Navsoft: AI-Powered Business Solutions & Custom Software Development
PPTX
L1 - Introduction to python Backend.pptx
PDF
Which alternative to Crystal Reports is best for small or large businesses.pdf
PDF
How Creative Agencies Leverage Project Management Software.pdf
PDF
Softaken Excel to vCard Converter Software.pdf
PDF
2025 Textile ERP Trends: SAP, Odoo & Oracle
PPTX
ai tools demonstartion for schools and inter college
PDF
System and Network Administraation Chapter 3
PPTX
Operating system designcfffgfgggggggvggggggggg
PDF
How to Migrate SBCGlobal Email to Yahoo Easily
Transform Your Business with a Software ERP System
Upgrade and Innovation Strategies for SAP ERP Customers
Odoo Companies in India – Driving Business Transformation.pdf
Nekopoi APK 2025 free lastest update
Digital Strategies for Manufacturing Companies
System and Network Administration Chapter 2
Essential Infomation Tech presentation.pptx
Design an Analysis of Algorithms I-SECS-1021-03
AI in Product Development-omnex systems
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
Navsoft: AI-Powered Business Solutions & Custom Software Development
L1 - Introduction to python Backend.pptx
Which alternative to Crystal Reports is best for small or large businesses.pdf
How Creative Agencies Leverage Project Management Software.pdf
Softaken Excel to vCard Converter Software.pdf
2025 Textile ERP Trends: SAP, Odoo & Oracle
ai tools demonstartion for schools and inter college
System and Network Administraation Chapter 3
Operating system designcfffgfgggggggvggggggggg
How to Migrate SBCGlobal Email to Yahoo Easily

Stacks in algorithems & data structure

  • 1. AAllggoorriitthhmmss aanndd DDaattaa SSttrruuccttuurreess Ahmad Khan Lecture 4 Oxford University Data Structures Using C++ by Dr Varsha Patil Press © 2012
  • 2. Stack Oxford University Data Structures Using C++ by Dr Varsha Patil Press © 2012 2
  • 3. Objectives Understand all the aspects of a stack as data type including : Last In, First Out (LIFO) data access Push, Pop, and other stack operations Contiguous implementation of a stack Learn realization of stack using arrays(: Contiguous stack) Learn to choose appropriate realization suitable for practical applications Learn and implement the multi-stacks Use of stacks in expression conversion, recursion, reversing data and many more Oxford University Data Structures Using C++ by Dr Varsha Patil Press © 2012 3
  • 4.  A stack is an ordered list in which all insertions and deletions Oxford University Data Structures Using C++ by Dr Varsha Patil Press © 2012 4 are made at one end, called the top  In computer programming processing of function calls and their terminations uses stack. Stack is used to remember the place where the call was made; so that it can return there after the function is complete
  • 5. Following are some examples in which where we need to use stacks are generally used: Handling function calls in programs very often restricts the access only at one end. In such implementation we need to use stacks. We can keep track of the return address to earlier function after furnishing/finishing a function call using stacks If we intend to store a group of data together in a sequential manner in computer’s memory, then arrays can be one of the possible data structures. Oxford University Data Structures Using C++ by Dr Varsha Patil Press © 2012 5
  • 6. Arrays  An array is a finite ordered collection of homogeneous data elements which provides direct access (or random access) to any of its elements.  An array as a data structure is defined as a set of pairs (index, value) such that with each index a value is associated. index — indicates the location of an element in an array. value - indicates the actual value of that data element.  Declaration of an array in ‘C++’: int Array_A[20]; Oxford University Data Structures Using C++ by Dr Varsha Patil Press © 2012 6
  • 7. Examples of Stacks Oxford University Data Structures Using C++ by Dr Varsha Patil Press © 2012 7 Following figure shows some examples of stack 1.Stack of Book 2.Stack of chairs 3.Stack of cups
  • 8. Primitive Operations Oxford University Data Structures Using C++ by Dr Varsha Patil Press © 2012 8 1) The Stack Full Condition (Stack Capacity =3)
  • 9. Oxford University Data Structures Using C++ by Dr Varsha Patil Press © 2012 9 2) The Pop Operation
  • 10. Oxford University Data Structures Using C++ by Dr Varsha Patil Press © 2012 10 3) The Empty Stack
  • 11. Oxford University Data Structures Using C++ by Dr Varsha Patil Press © 2012 11 4) The Get Top Operation
  • 12. Oxford University Data Structures Using C++ by Dr Varsha Patil Press © 2012 12 Representation Of Stack S Using Sequential Organization (Arrays)
  • 13. Oxford University Data Structures Using C++ by Dr Varsha Patil Press © 2012 13 ……….. C B A top  The elements are stored in the stack from the first location onwards  The first element is stored at 0th location of the array ‘Stack’ which means at STACK[0], the second element at STACK[1], ith element at Stack [i–1], and nth element at Stack[n–1]  Associated with the array will be an integer variable, top, which points to the top element in the stack  The initial value of top is −1, when the stack is empty  It can hold elements from index 0 and maximum it can grow up to n−1 as this is the static stack using arrays
  • 14. 14 Implementation of Stack Stack Class class STACK { private: int Stack[50]; int MaxCapacity; int top; public: STACK( ) { MaxCapacity= 50; top=-1;} int GetTop( ); int Pop( ); void Push( int Element); int Empty( ); int CurrSize( ); int IsFull (); }; Data Structures in C++ by Dr. Varsha Patil Oxford University Press © 2012 14
  • 15. 15 Implementation of Stack GetTop, Pop, Empty int STACK :: GetTop () { if (!Empty()) return (Stack[top]); } int STACK :: Pop () { if (!Empty()) return (Stack[top--]); } int STACK :: Empty () { if (top==-1) return 1; else return 0; } Data Structures in C++ by Dr. Varsha Patil Oxford University Press © 2012 15
  • 16. 16 Implementation of Stack IsFull, CurrSize, Push int STACK :: IsFull () { if (top==MaxCapacity-1) return 1; else return 0; } int STACK :: CurrSize() { return(top+1); } void STACK :: Push (int Element) { if (!IsFull()) Stack[++top]=Element; } Data Structures in C++ by Dr. Varsha Patil Oxford University Press © 2012 16
  • 17. STACK S; S.Push(1); S.Push(2); cout<<"t Top element of stack = "<<S.GetTop()<<endl; cout<<"t After pop = "<<S.Pop()<<endl; cout<<"t After pop = "<<S.Pop()<<endl; getch(); 17 Implementation of Stack main int main( ) { } Data Structures in C++ by Dr. Varsha Patil Oxford University Press © 2012 17