SlideShare a Scribd company logo
1 of 22
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 - 6ecomputernotes
 
Topic 2_revised.pptx
Topic 2_revised.pptxTopic 2_revised.pptx
Topic 2_revised.pptxJAYAPRIYAR7
 
Stack - Data Structure - Notes
Stack - Data Structure - NotesStack - Data Structure - Notes
Stack - Data Structure - NotesOmprakash Chauhan
 
01 stack 20160908_jintaek_seo
01 stack 20160908_jintaek_seo01 stack 20160908_jintaek_seo
01 stack 20160908_jintaek_seoJinTaek Seo
 
Java9を迎えた今こそ!Java本格(再)入門
Java9を迎えた今こそ!Java本格(再)入門Java9を迎えた今こそ!Java本格(再)入門
Java9を迎えた今こそ!Java本格(再)入門Takuya Okada
 
Introduction to Compiler Development
Introduction to Compiler DevelopmentIntroduction to Compiler Development
Introduction to Compiler DevelopmentLogan 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 AnalyzerAndrey 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 CHANGEJon Fortman
 
lect- 3&4.ppt
lect- 3&4.pptlect- 3&4.ppt
lect- 3&4.pptmrizwan38
 
Types of instruction in 8085 microprocessor
Types of instruction in 8085 microprocessorTypes of instruction in 8085 microprocessor
Types of instruction in 8085 microprocessorsamarthpawar9890
 
ADT STACK and Queues
ADT STACK and QueuesADT STACK and Queues
ADT STACK and QueuesBHARATH KUMAR
 

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
 
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
 
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
 
ADT STACK and Queues
ADT STACK and QueuesADT STACK and Queues
ADT STACK and Queues
 
8086 instruction set
8086 instruction set8086 instruction set
8086 instruction set
 

More from 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

How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024Results
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024The Digital Insurer
 
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...gurkirankumar98700
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEarley Information Science
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Allon Mureinik
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...Neo4j
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Servicegiselly40
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure servicePooja Nehwal
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
Developing An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilDeveloping An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilV3cube
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesSinan KOZAK
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityPrincipled Technologies
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 

Recently uploaded (20)

How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Developing An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilDeveloping An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of Brazil
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen Frames
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 

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