SlideShare a Scribd company logo
Chapter 4Chapter 4
staCksstaCks
03/17/18 BY MS. SHAISTA QADIR 1
PRESENTED BY
Shaista Qadir
Lecturer king khalid university
CONteNtsCONteNts
 staCk
 OperatiONs perfOrmed ON staCk
 staCk appliCatiONs
 iNfix tO pOstfix CONversiON
 iNfix tO prefix CONversiON
 pOstfix tO iNfix CONversiON
 prefix tO iNfix CONversiON
 algOrithm tO push aN elemeNt iN a staCk
 algOrithm tO pOp aN elemeNt frOm a
staCk
03/17/18 BY MS. SHAISTA QADIR 2
staCkstaCk
03/17/18 BY MS. SHAISTA QADIR 3
 A stack is a linear data structure that can be accessed only
at one of its ends for storing and retrieving data.
 Such a stack resembles a stack of trays in a cafeteria: New
trays are put on the top of the stack and taken off the top.
 The last tray put on the stack is the first tray removed from
the stack.
 For this reason, a stack is called an LIFO structure last
in/first out.
OperatiONs perfOrmed ONOperatiONs perfOrmed ON
staCkstaCk
03/17/18 BY MS. SHAISTA QADIR 4
 The operations are as follows:
◦ clear() : Clear the stack.
◦ isEmpty() : Check to see if the stack is empty.
◦ push(el) : Put the element el on the top of the
stack.
◦ pop() : Take the topmost element from the
stack.
◦ topEl() : Return the topmost element in the
stack without removing it.
03/17/18 BY MS. SHAISTA QADIR 5
Stack Overflow:
If adding an element onto the top of the stack and if that
stack is full this situation is called stack overflow.
Stack Underflow:
Removing an element from the stack and if that stack is
empty this situation is called stack underflow
OperatiONs perfOrmed ON
staCk
staCk appliCatiONsstaCk appliCatiONs
03/17/18 BY MS. SHAISTA QADIR 6
APPLICATIONS OF STACK:
◦ Reversing Data: We can use stacks to reverse data.
(example: files, strings) Very useful for finding
palindromes.
◦ To evaluate Arithmetic expressions: (Prefix, Infix, Postfix
Notations)
◦ Arithmetic expressions have
 Operands (variables or numeric constants).
 Operators
 i. Binary : +, -, *, / ,%
 ii. Unary: - (sign for negative numbers)
03/17/18 BY MS. SHAISTA QADIR 7
 Example:
Prefix Notation Infix Notation Postfix Notation
+A * B C A + B * C A B C * +
* + A B C (A+B) * C A B + C *
+ – A B C A – B + C A B – C +
– A + B C A – (B+C) A B C + –
*+AB-CD (A+B)*(C-D) AB+CD-*
 Priority convention(Rules):
◦ Unary minus has Highest priority
◦ *, /, % have Medium priority
◦ +, - have Lowest priority
stack applicationsstack applications
03/17/18 BY MS. SHAISTA QADIR 8
stack applicationsstack applications
Infix Expression Prefix Expression Postfix Expression
A + B * C + D + + A * B C D A B C * + D +
(A + B) * (C + D) * + A B + C D A B + C D + *
A * B + C * D + * A B * C D A B * C D * +
A + B + C + D + + + A B C D A B + C + D +
03/17/18 BY MS. SHAISTA QADIR 9
stack applicationsstack applications
Example:
To evaluate the expression A+B*C using Postfix notation in
a
Stack
Postfix notation for A+B*C is ABC*+
PUSH A
PUSH B
PUSH C
MULTIPLY
ADD
POP
PUSH A PUSH B PUSH C MULTIPLY ADD POP
03/17/18 BY MS. SHAISTA QADIR 10
03/17/18 BY MS. SHAISTA QADIR 11
03/17/18 BY MS. SHAISTA QADIR 12
03/17/18 BY MS. SHAISTA QADIR 13
Postfix to Infix conversionPostfix to Infix conversion
Example: abc-+de-fg-h+/* to infix
03/17/18 BY MS. SHAISTA QADIR 14
Postfix to Infix conversionPostfix to Infix conversion
Example: abc-+de-fg-h+/* to infix (contd)
03/17/18 BY MS. SHAISTA QADIR 15
Postfix to Infix conversionPostfix to Infix conversion
Example: abc-+de-fg-h+/* to infix (contd)
03/17/18 BY MS. SHAISTA QADIR 16
Prefix to Infix conversionPrefix to Infix conversion
Example: *+a-bc/-de+-fgh to infix
03/17/18 BY MS. SHAISTA QADIR 17
Prefix to Infix conversionPrefix to Infix conversion
Example: *+a-bc/-de+-fgh to infix (contd)
algorithm to push analgorithm to push an
element in a stackelement in a stack
03/17/18 BY MS. SHAISTA QADIR 18
ALGORITHM:
1. If (TOP == MAX-1) Then
2. Print: Overflow
3. Else
4. Set TOP = TOP + 1
5. Set STACK[TOP] = ITEM
6. Print: ITEM inserted EndIf
7. Exit.
algorithm to push analgorithm to push an
elementelement
03/17/18 BY MS. SHAISTA QADIR 19
PROGRAM LOGIC:
public boolean isFull()
{
if(top == maxsize-1)
return true;
else
return false;
}
public void push(int a)
{
++top;
arr[top] = a;
}
algorithm to pop analgorithm to pop an
element From staCKelement From staCK
03/17/18 BY MS. SHAISTA QADIR 20
ALGORITHM:
1. If (TOP == -1) Then
2. Print: Underflow
3. Else
4. Set ITEM = STACK[TOP]
5. Set TOP = TOP - 1
6. Print: ITEM deleted EndIf
7. Exit
algorithm to pop analgorithm to pop an
element From staCKelement From staCK
03/17/18 BY MS. SHAISTA QADIR 21
PROGRAM LOGIC:
public boolean isEmpty()
{
if(top == -1)
return true;
else
return false;
}
public int pop ()
{
int e = arr[top];
top--;
return e;
}
03/17/18 BY MS. SHAISTA QADIR 22
THANK YOUTHANK YOU

More Related Content

Similar to Lecture 4

computer notes - Data Structures - 6
computer notes - Data Structures - 6computer notes - Data Structures - 6
computer notes - Data Structures - 6
ecomputernotes
 
STACK Applications in DS
STACK Applications in DSSTACK Applications in DS
STACK Applications in DS
NARESH GUMMAGUTTA
 
Topic 2_revised.pptx
Topic 2_revised.pptxTopic 2_revised.pptx
Topic 2_revised.pptx
JAYAPRIYAR7
 
Lecture 07
Lecture 07Lecture 07
Lecture 07
sohelranasweet
 
Stack - Data Structure - Notes
Stack - Data Structure - NotesStack - Data Structure - Notes
Stack - Data Structure - Notes
Omprakash Chauhan
 
Best Practices for Migrating Legacy Data Warehouses into Amazon Redshift
Best Practices for Migrating Legacy Data Warehouses into Amazon RedshiftBest Practices for Migrating Legacy Data Warehouses into Amazon Redshift
Best Practices for Migrating Legacy Data Warehouses into Amazon Redshift
Amazon Web Services
 
01 stack 20160908_jintaek_seo
01 stack 20160908_jintaek_seo01 stack 20160908_jintaek_seo
01 stack 20160908_jintaek_seo
JinTaek Seo
 
C++17 now
C++17 nowC++17 now
C++17 now
corehard_by
 
Java9を迎えた今こそ!Java本格(再)入門
Java9を迎えた今こそ!Java本格(再)入門Java9を迎えた今こそ!Java本格(再)入門
Java9を迎えた今こそ!Java本格(再)入門
Takuya Okada
 
Lecture6
Lecture6Lecture6
Lecture6
Muhammad Zubair
 
Lesson 4 stacks and queues
Lesson 4  stacks and queuesLesson 4  stacks and queues
Lesson 4 stacks and queues
MLG College of Learning, Inc
 
HTAP By Accident: Getting More From PostgreSQL Using Hardware Acceleration
HTAP By Accident: Getting More From PostgreSQL Using Hardware AccelerationHTAP By Accident: Getting More From PostgreSQL Using Hardware Acceleration
HTAP By Accident: Getting More From PostgreSQL Using Hardware Acceleration
EDB
 
Introduction to Compiler Development
Introduction to Compiler DevelopmentIntroduction to Compiler Development
Introduction to Compiler Development
Logan Chien
 
Checking Wine with PVS-Studio and Clang Static Analyzer
Checking Wine with PVS-Studio and Clang Static AnalyzerChecking Wine with PVS-Studio and Clang Static Analyzer
Checking Wine with PVS-Studio and Clang Static Analyzer
Andrey Karpov
 
Impact Analysis FRAN PCT DATA DEFINITION CHANGE
Impact Analysis FRAN PCT DATA DEFINITION CHANGEImpact Analysis FRAN PCT DATA DEFINITION CHANGE
Impact Analysis FRAN PCT DATA DEFINITION CHANGE
Jon Fortman
 
Stack
StackStack
Odp
OdpOdp
lect- 3&4.ppt
lect- 3&4.pptlect- 3&4.ppt
lect- 3&4.ppt
mrizwan38
 
Instruction set of 8085
Instruction set of 8085Instruction set of 8085
Instruction set of 8085
venkateshkannat
 
Types of instruction in 8085 microprocessor
Types of instruction in 8085 microprocessorTypes of instruction in 8085 microprocessor
Types of instruction in 8085 microprocessor
samarthpawar9890
 

Similar to Lecture 4 (20)

computer notes - Data Structures - 6
computer notes - Data Structures - 6computer notes - Data Structures - 6
computer notes - Data Structures - 6
 
STACK Applications in DS
STACK Applications in DSSTACK Applications in DS
STACK Applications in DS
 
Topic 2_revised.pptx
Topic 2_revised.pptxTopic 2_revised.pptx
Topic 2_revised.pptx
 
Lecture 07
Lecture 07Lecture 07
Lecture 07
 
Stack - Data Structure - Notes
Stack - Data Structure - NotesStack - Data Structure - Notes
Stack - Data Structure - Notes
 
Best Practices for Migrating Legacy Data Warehouses into Amazon Redshift
Best Practices for Migrating Legacy Data Warehouses into Amazon RedshiftBest Practices for Migrating Legacy Data Warehouses into Amazon Redshift
Best Practices for Migrating Legacy Data Warehouses into Amazon Redshift
 
01 stack 20160908_jintaek_seo
01 stack 20160908_jintaek_seo01 stack 20160908_jintaek_seo
01 stack 20160908_jintaek_seo
 
C++17 now
C++17 nowC++17 now
C++17 now
 
Java9を迎えた今こそ!Java本格(再)入門
Java9を迎えた今こそ!Java本格(再)入門Java9を迎えた今こそ!Java本格(再)入門
Java9を迎えた今こそ!Java本格(再)入門
 
Lecture6
Lecture6Lecture6
Lecture6
 
Lesson 4 stacks and queues
Lesson 4  stacks and queuesLesson 4  stacks and queues
Lesson 4 stacks and queues
 
HTAP By Accident: Getting More From PostgreSQL Using Hardware Acceleration
HTAP By Accident: Getting More From PostgreSQL Using Hardware AccelerationHTAP By Accident: Getting More From PostgreSQL Using Hardware Acceleration
HTAP By Accident: Getting More From PostgreSQL Using Hardware Acceleration
 
Introduction to Compiler Development
Introduction to Compiler DevelopmentIntroduction to Compiler Development
Introduction to Compiler Development
 
Checking Wine with PVS-Studio and Clang Static Analyzer
Checking Wine with PVS-Studio and Clang Static AnalyzerChecking Wine with PVS-Studio and Clang Static Analyzer
Checking Wine with PVS-Studio and Clang Static Analyzer
 
Impact Analysis FRAN PCT DATA DEFINITION CHANGE
Impact Analysis FRAN PCT DATA DEFINITION CHANGEImpact Analysis FRAN PCT DATA DEFINITION CHANGE
Impact Analysis FRAN PCT DATA DEFINITION CHANGE
 
Stack
StackStack
Stack
 
Odp
OdpOdp
Odp
 
lect- 3&4.ppt
lect- 3&4.pptlect- 3&4.ppt
lect- 3&4.ppt
 
Instruction set of 8085
Instruction set of 8085Instruction set of 8085
Instruction set of 8085
 
Types of instruction in 8085 microprocessor
Types of instruction in 8085 microprocessorTypes of instruction in 8085 microprocessor
Types of instruction in 8085 microprocessor
 

More from Shaista Qadir

Lecture 1
Lecture 1Lecture 1
Lecture 1
Shaista Qadir
 
Sorting
SortingSorting
Sorting
Shaista Qadir
 
Searching
SearchingSearching
Searching
Shaista Qadir
 
Queue
QueueQueue
linked list
linked listlinked list
linked list
Shaista Qadir
 
Complexity Analysis
Complexity Analysis Complexity Analysis
Complexity Analysis
Shaista Qadir
 

More from Shaista Qadir (6)

Lecture 1
Lecture 1Lecture 1
Lecture 1
 
Sorting
SortingSorting
Sorting
 
Searching
SearchingSearching
Searching
 
Queue
QueueQueue
Queue
 
linked list
linked listlinked list
linked list
 
Complexity Analysis
Complexity Analysis Complexity Analysis
Complexity Analysis
 

Recently uploaded

Video Streaming: Then, Now, and in the Future
Video Streaming: Then, Now, and in the FutureVideo Streaming: Then, Now, and in the Future
Video Streaming: Then, Now, and in the Future
Alpen-Adria-Universität
 
GraphRAG for Life Science to increase LLM accuracy
GraphRAG for Life Science to increase LLM accuracyGraphRAG for Life Science to increase LLM accuracy
GraphRAG for Life Science to increase LLM accuracy
Tomaz Bratanic
 
Infrastructure Challenges in Scaling RAG with Custom AI models
Infrastructure Challenges in Scaling RAG with Custom AI modelsInfrastructure Challenges in Scaling RAG with Custom AI models
Infrastructure Challenges in Scaling RAG with Custom AI models
Zilliz
 
Choosing The Best AWS Service For Your Website + API.pptx
Choosing The Best AWS Service For Your Website + API.pptxChoosing The Best AWS Service For Your Website + API.pptx
Choosing The Best AWS Service For Your Website + API.pptx
Brandon Minnick, MBA
 
Climate Impact of Software Testing at Nordic Testing Days
Climate Impact of Software Testing at Nordic Testing DaysClimate Impact of Software Testing at Nordic Testing Days
Climate Impact of Software Testing at Nordic Testing Days
Kari Kakkonen
 
Ocean lotus Threat actors project by John Sitima 2024 (1).pptx
Ocean lotus Threat actors project by John Sitima 2024 (1).pptxOcean lotus Threat actors project by John Sitima 2024 (1).pptx
Ocean lotus Threat actors project by John Sitima 2024 (1).pptx
SitimaJohn
 
Mariano G Tinti - Decoding SpaceX
Mariano G Tinti - Decoding SpaceXMariano G Tinti - Decoding SpaceX
Mariano G Tinti - Decoding SpaceX
Mariano Tinti
 
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
名前 です男
 
Artificial Intelligence for XMLDevelopment
Artificial Intelligence for XMLDevelopmentArtificial Intelligence for XMLDevelopment
Artificial Intelligence for XMLDevelopment
Octavian Nadolu
 
“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...
“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...
“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...
Edge AI and Vision Alliance
 
20240609 QFM020 Irresponsible AI Reading List May 2024
20240609 QFM020 Irresponsible AI Reading List May 202420240609 QFM020 Irresponsible AI Reading List May 2024
20240609 QFM020 Irresponsible AI Reading List May 2024
Matthew Sinclair
 
AI-Powered Food Delivery Transforming App Development in Saudi Arabia.pdf
AI-Powered Food Delivery Transforming App Development in Saudi Arabia.pdfAI-Powered Food Delivery Transforming App Development in Saudi Arabia.pdf
AI-Powered Food Delivery Transforming App Development in Saudi Arabia.pdf
Techgropse Pvt.Ltd.
 
Uni Systems Copilot event_05062024_C.Vlachos.pdf
Uni Systems Copilot event_05062024_C.Vlachos.pdfUni Systems Copilot event_05062024_C.Vlachos.pdf
Uni Systems Copilot event_05062024_C.Vlachos.pdf
Uni Systems S.M.S.A.
 
Columbus Data & Analytics Wednesdays - June 2024
Columbus Data & Analytics Wednesdays - June 2024Columbus Data & Analytics Wednesdays - June 2024
Columbus Data & Analytics Wednesdays - June 2024
Jason Packer
 
Presentation of the OECD Artificial Intelligence Review of Germany
Presentation of the OECD Artificial Intelligence Review of GermanyPresentation of the OECD Artificial Intelligence Review of Germany
Presentation of the OECD Artificial Intelligence Review of Germany
innovationoecd
 
OpenID AuthZEN Interop Read Out - Authorization
OpenID AuthZEN Interop Read Out - AuthorizationOpenID AuthZEN Interop Read Out - Authorization
OpenID AuthZEN Interop Read Out - Authorization
David Brossard
 
Essentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FMEEssentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FME
Safe Software
 
June Patch Tuesday
June Patch TuesdayJune Patch Tuesday
June Patch Tuesday
Ivanti
 
“I’m still / I’m still / Chaining from the Block”
“I’m still / I’m still / Chaining from the Block”“I’m still / I’m still / Chaining from the Block”
“I’m still / I’m still / Chaining from the Block”
Claudio Di Ciccio
 
TrustArc Webinar - 2024 Global Privacy Survey
TrustArc Webinar - 2024 Global Privacy SurveyTrustArc Webinar - 2024 Global Privacy Survey
TrustArc Webinar - 2024 Global Privacy Survey
TrustArc
 

Recently uploaded (20)

Video Streaming: Then, Now, and in the Future
Video Streaming: Then, Now, and in the FutureVideo Streaming: Then, Now, and in the Future
Video Streaming: Then, Now, and in the Future
 
GraphRAG for Life Science to increase LLM accuracy
GraphRAG for Life Science to increase LLM accuracyGraphRAG for Life Science to increase LLM accuracy
GraphRAG for Life Science to increase LLM accuracy
 
Infrastructure Challenges in Scaling RAG with Custom AI models
Infrastructure Challenges in Scaling RAG with Custom AI modelsInfrastructure Challenges in Scaling RAG with Custom AI models
Infrastructure Challenges in Scaling RAG with Custom AI models
 
Choosing The Best AWS Service For Your Website + API.pptx
Choosing The Best AWS Service For Your Website + API.pptxChoosing The Best AWS Service For Your Website + API.pptx
Choosing The Best AWS Service For Your Website + API.pptx
 
Climate Impact of Software Testing at Nordic Testing Days
Climate Impact of Software Testing at Nordic Testing DaysClimate Impact of Software Testing at Nordic Testing Days
Climate Impact of Software Testing at Nordic Testing Days
 
Ocean lotus Threat actors project by John Sitima 2024 (1).pptx
Ocean lotus Threat actors project by John Sitima 2024 (1).pptxOcean lotus Threat actors project by John Sitima 2024 (1).pptx
Ocean lotus Threat actors project by John Sitima 2024 (1).pptx
 
Mariano G Tinti - Decoding SpaceX
Mariano G Tinti - Decoding SpaceXMariano G Tinti - Decoding SpaceX
Mariano G Tinti - Decoding SpaceX
 
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
 
Artificial Intelligence for XMLDevelopment
Artificial Intelligence for XMLDevelopmentArtificial Intelligence for XMLDevelopment
Artificial Intelligence for XMLDevelopment
 
“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...
“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...
“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...
 
20240609 QFM020 Irresponsible AI Reading List May 2024
20240609 QFM020 Irresponsible AI Reading List May 202420240609 QFM020 Irresponsible AI Reading List May 2024
20240609 QFM020 Irresponsible AI Reading List May 2024
 
AI-Powered Food Delivery Transforming App Development in Saudi Arabia.pdf
AI-Powered Food Delivery Transforming App Development in Saudi Arabia.pdfAI-Powered Food Delivery Transforming App Development in Saudi Arabia.pdf
AI-Powered Food Delivery Transforming App Development in Saudi Arabia.pdf
 
Uni Systems Copilot event_05062024_C.Vlachos.pdf
Uni Systems Copilot event_05062024_C.Vlachos.pdfUni Systems Copilot event_05062024_C.Vlachos.pdf
Uni Systems Copilot event_05062024_C.Vlachos.pdf
 
Columbus Data & Analytics Wednesdays - June 2024
Columbus Data & Analytics Wednesdays - June 2024Columbus Data & Analytics Wednesdays - June 2024
Columbus Data & Analytics Wednesdays - June 2024
 
Presentation of the OECD Artificial Intelligence Review of Germany
Presentation of the OECD Artificial Intelligence Review of GermanyPresentation of the OECD Artificial Intelligence Review of Germany
Presentation of the OECD Artificial Intelligence Review of Germany
 
OpenID AuthZEN Interop Read Out - Authorization
OpenID AuthZEN Interop Read Out - AuthorizationOpenID AuthZEN Interop Read Out - Authorization
OpenID AuthZEN Interop Read Out - Authorization
 
Essentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FMEEssentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FME
 
June Patch Tuesday
June Patch TuesdayJune Patch Tuesday
June Patch Tuesday
 
“I’m still / I’m still / Chaining from the Block”
“I’m still / I’m still / Chaining from the Block”“I’m still / I’m still / Chaining from the Block”
“I’m still / I’m still / Chaining from the Block”
 
TrustArc Webinar - 2024 Global Privacy Survey
TrustArc Webinar - 2024 Global Privacy SurveyTrustArc Webinar - 2024 Global Privacy Survey
TrustArc Webinar - 2024 Global Privacy Survey
 

Lecture 4

  • 1. Chapter 4Chapter 4 staCksstaCks 03/17/18 BY MS. SHAISTA QADIR 1 PRESENTED BY Shaista Qadir Lecturer king khalid university
  • 2. CONteNtsCONteNts  staCk  OperatiONs perfOrmed ON staCk  staCk appliCatiONs  iNfix tO pOstfix CONversiON  iNfix tO prefix CONversiON  pOstfix tO iNfix CONversiON  prefix tO iNfix CONversiON  algOrithm tO push aN elemeNt iN a staCk  algOrithm tO pOp aN elemeNt frOm a staCk 03/17/18 BY MS. SHAISTA QADIR 2
  • 3. staCkstaCk 03/17/18 BY MS. SHAISTA QADIR 3  A stack is a linear data structure that can be accessed only at one of its ends for storing and retrieving data.  Such a stack resembles a stack of trays in a cafeteria: New trays are put on the top of the stack and taken off the top.  The last tray put on the stack is the first tray removed from the stack.  For this reason, a stack is called an LIFO structure last in/first out.
  • 4. OperatiONs perfOrmed ONOperatiONs perfOrmed ON staCkstaCk 03/17/18 BY MS. SHAISTA QADIR 4  The operations are as follows: ◦ clear() : Clear the stack. ◦ isEmpty() : Check to see if the stack is empty. ◦ push(el) : Put the element el on the top of the stack. ◦ pop() : Take the topmost element from the stack. ◦ topEl() : Return the topmost element in the stack without removing it.
  • 5. 03/17/18 BY MS. SHAISTA QADIR 5 Stack Overflow: If adding an element onto the top of the stack and if that stack is full this situation is called stack overflow. Stack Underflow: Removing an element from the stack and if that stack is empty this situation is called stack underflow OperatiONs perfOrmed ON staCk
  • 6. staCk appliCatiONsstaCk appliCatiONs 03/17/18 BY MS. SHAISTA QADIR 6 APPLICATIONS OF STACK: ◦ Reversing Data: We can use stacks to reverse data. (example: files, strings) Very useful for finding palindromes. ◦ To evaluate Arithmetic expressions: (Prefix, Infix, Postfix Notations) ◦ Arithmetic expressions have  Operands (variables or numeric constants).  Operators  i. Binary : +, -, *, / ,%  ii. Unary: - (sign for negative numbers)
  • 7. 03/17/18 BY MS. SHAISTA QADIR 7  Example: Prefix Notation Infix Notation Postfix Notation +A * B C A + B * C A B C * + * + A B C (A+B) * C A B + C * + – A B C A – B + C A B – C + – A + B C A – (B+C) A B C + – *+AB-CD (A+B)*(C-D) AB+CD-*  Priority convention(Rules): ◦ Unary minus has Highest priority ◦ *, /, % have Medium priority ◦ +, - have Lowest priority stack applicationsstack applications
  • 8. 03/17/18 BY MS. SHAISTA QADIR 8 stack applicationsstack applications Infix Expression Prefix Expression Postfix Expression A + B * C + D + + A * B C D A B C * + D + (A + B) * (C + D) * + A B + C D A B + C D + * A * B + C * D + * A B * C D A B * C D * + A + B + C + D + + + A B C D A B + C + D +
  • 9. 03/17/18 BY MS. SHAISTA QADIR 9 stack applicationsstack applications Example: To evaluate the expression A+B*C using Postfix notation in a Stack Postfix notation for A+B*C is ABC*+ PUSH A PUSH B PUSH C MULTIPLY ADD POP PUSH A PUSH B PUSH C MULTIPLY ADD POP
  • 10. 03/17/18 BY MS. SHAISTA QADIR 10
  • 11. 03/17/18 BY MS. SHAISTA QADIR 11
  • 12. 03/17/18 BY MS. SHAISTA QADIR 12
  • 13. 03/17/18 BY MS. SHAISTA QADIR 13 Postfix to Infix conversionPostfix to Infix conversion Example: abc-+de-fg-h+/* to infix
  • 14. 03/17/18 BY MS. SHAISTA QADIR 14 Postfix to Infix conversionPostfix to Infix conversion Example: abc-+de-fg-h+/* to infix (contd)
  • 15. 03/17/18 BY MS. SHAISTA QADIR 15 Postfix to Infix conversionPostfix to Infix conversion Example: abc-+de-fg-h+/* to infix (contd)
  • 16. 03/17/18 BY MS. SHAISTA QADIR 16 Prefix to Infix conversionPrefix to Infix conversion Example: *+a-bc/-de+-fgh to infix
  • 17. 03/17/18 BY MS. SHAISTA QADIR 17 Prefix to Infix conversionPrefix to Infix conversion Example: *+a-bc/-de+-fgh to infix (contd)
  • 18. algorithm to push analgorithm to push an element in a stackelement in a stack 03/17/18 BY MS. SHAISTA QADIR 18 ALGORITHM: 1. If (TOP == MAX-1) Then 2. Print: Overflow 3. Else 4. Set TOP = TOP + 1 5. Set STACK[TOP] = ITEM 6. Print: ITEM inserted EndIf 7. Exit.
  • 19. algorithm to push analgorithm to push an elementelement 03/17/18 BY MS. SHAISTA QADIR 19 PROGRAM LOGIC: public boolean isFull() { if(top == maxsize-1) return true; else return false; } public void push(int a) { ++top; arr[top] = a; }
  • 20. algorithm to pop analgorithm to pop an element From staCKelement From staCK 03/17/18 BY MS. SHAISTA QADIR 20 ALGORITHM: 1. If (TOP == -1) Then 2. Print: Underflow 3. Else 4. Set ITEM = STACK[TOP] 5. Set TOP = TOP - 1 6. Print: ITEM deleted EndIf 7. Exit
  • 21. algorithm to pop analgorithm to pop an element From staCKelement From staCK 03/17/18 BY MS. SHAISTA QADIR 21 PROGRAM LOGIC: public boolean isEmpty() { if(top == -1) return true; else return false; } public int pop () { int e = arr[top]; top--; return e; }
  • 22. 03/17/18 BY MS. SHAISTA QADIR 22 THANK YOUTHANK YOU