SlideShare a Scribd company logo
1 of 28
19CSE111 Fundamentals of Data Structures
Stack
Course Instructor
Dr. M. Rajasekhar Reddy
Stack
What is Stack?
• A stack is a collection of objects that are inserted and removed
according to the last-in, first-out (LIFO) principle
– A user may insert objects into a stack at any time, but may only access
or remove the most recently inserted object that remains at the so-
called “top” of the stack
Stack Applications
• Internet Web browsers store the addresses of recently visited
sites in a stack
Stack Applications
• Text editors usually provide an “undo” mechanism that cancels
recent editing operations and reverts to former states of a
document
– This undo operation can be accomplished by keeping text changes in a
stack
Stack Example
• Push(10)
• Push(15)
• Push(20)
• Push(25)
• Pop()
• Pop()
10
15
20
25
The Stack Abstract Data Type
• ADT: Abstract Data type is a type for objects whose behavior is
defined by a set of value and a set of operations
• Formally, a stack is an abstract data type (ADT) such that an
instance S supports the following two methods:
– S.push(e): Add element e to the top of stack S
– S.pop(): Remove and return the top element from the stack S; an error
occurs if the stack is empty
The Stack Abstract Data Type
• Additionally, let us define the following accessor methods for
convenience:
– S.top(): Return a reference to the top element of stack S, without
removing it; an error occurs if the stack is empty
– S.is_empty( ): Return True if stack S does not contain any elements
– len(S): Return the number of elements in stack S
Working of Stack
• A pointer called TOP is used to keep track of the top element
in the stack
• When initializing the stack, we set its value to -1 so that we
can check if the stack is empty by comparing TOP == -1
• On pushing an element, we increase the value of TOP and
place the new element in the position pointed to by TOP
• On popping an element, we return the element pointed to by
TOP and reduce its value.
• Before pushing, we check if the stack is already full
• Before popping, we check if the stack is already empty
Stack Example
• Push(10)
• Push(15)
• Push(20)
• Len()
• Top()
• IsEmpty()
• Push(25)
• Pop()
• Pop()
• Len()
10 TOP
15
20
= 3
= 2
25
= False
= 2
…
5
4
3
2
1
0
Stack Operations – push()
• The procedure to perform push is here:
begin procedure push: stack, data
if stack is full
return null
endif
top ← top + 1
stack[top] ← data
end procedure
void push(int data)
{
if(!isFull())
{
top = top + 1;
stack[top] = data;
}
else
{
printf("Stack is full.n");
}
}
Stack Operations – pop()
• The procedure to perform pop is here:
begin procedure pop: stack
if stack is empty
return null
endif
data ← stack[top]
top ← top - 1
return data
end procedure
int pop(int data)
{
if(!isempty())
{
data = stack[top];
top = top - 1;
return data;
}
else
{
printf("Stack is empty.n");
}
}
Stack Operations – is_empty()
• The procedure to check the emptiness of a stack is here:
begin procedure isempty
if top less than 1
return true
else
return false
endif
end procedure
int isempty()
{
if(top == -1)
return 1;
else
return 0;
}
Stack Operations – top()
• The procedure to return the reference to top element is here:
begin procedure peek
return stack[top]
end procedure
int peek()
{
return stack[top];
}
Stack Operations
Stack Applications in Computer Engineering
• Infix to Postfix Conversion
– The stack can be used to convert some infix expression into its postfix
equivalent, or prefix equivalent
– These postfix or prefix notations are used in computers to express some
expressions
• Expression Evaluation
– We also need the help of stack data structure to evaluate postfix
expressions
• Balancing Symbols
– Frequently a lack of one symbol will cause the compiler to spill out a
hundred lines of diagnostics without identifying the real error
– Stack can be used to balance the symbols such as {}, (), [] etc.
Stack Applications in Computer Engineering
• Processing Function Calls
– Stack plays an important role in programs that call several functions in
succession
• Reverse a String
– A Stack can be used to reverse the characters of a string
Infix to Postfix Conversion
• Infix Notation
– The infix notation is a convenient way of writing an expression in which each
operator is placed between the operands
• Prefix Notation
– The prefix notation places the operator before the operands (Polish notation)
• Postfix Notation
– The postfix notation places the operator after the operands (Reverse Polish
Notation)
Infix Notation Prefix Notation Postfix Notation
A * B * A B AB*
(A+B)/C /+ ABC AB+C/
(A*B) + (D-C) +*AB - DC AB*DC-+
Algorithm
• Step 1 : Scan the Infix Expression from left to right one character at a time.
• Step 2 : If the scanned character is an operand, append it to the output.
• Step 3 : Else, if it is an operator,
– Step 3.1 : If the precedence of the scanned operator is greater than that of the
operator in the stack (or the stack is empty or the stack contains a ‘(‘ or ‘[‘ or ‘{‘),
push it on stack.
– Step 3.2 : Else, Pop all the operators from the stack which are greater than or equal
to in precedence than that of the scanned operator.
• Step 4 : If the scanned character is an ‘(‘ or ‘[‘ or ‘{‘, push it onto the stack
• Step 5 : If the scanned character is an ‘)’or ‘]’ or ‘}’, pop the stack and output it
until a ‘(‘ or ‘[‘ or ‘{‘ respectively is encountered, and discard both the
parenthesis
• Step 6 : Repeat steps 2-6 until infix expression is scanned
• Step 7 : Print the output
• Step 8 : Pop and output from the stack until it is not empty
Example – Infix to Postfix Conversion
( A + B / C + D * ( E – F ) ^ G )
Expression
Stack
Output
(
A
+
B
/
C / +
+
D
*
(
E
-
F - ^
G ^ * +
Exercise Questions
1. Convert the infix expression ( AX * ( BX * ( ( ( CY + AY ) + BY ) * CX ) )
) to its postfix equivalent using stack.
2. Convert the infix expression ( ( H * ( ( ( ( A + ( ( B + C ) * D ) ) * F ) *
G ) * E ) ) + J ) to its postfix equivalent using stack.
3. Convert the infix expression a + b * ( c ^ d – e ) ^ ( f + g * h ) - i to its
postfix equivalent using stack.
Postfix Expression Evaluation
• The expressions written in postfix form are evaluated faster
compared to infix notation
• Algorithm
1. Create a stack to store operands
2. Scan the given expression and do the following for every scanned
element
a. If the element is a number, push it onto the stack
b. If the element is an operator, pop operands for the operator from the
stack. Evaluate the operator and push the result back to the stack
3. When the expression is ended, the number in the stack is the final
answer
Example – Expression Evaluation
2 2 1 * + 9 -
Expression
Stack
Calculations
2
3
1
*
1 2 = 2
2
+
2 2 = 4
4
9 -
9 4 = 5
5
Exercise Questions
• Evaluate the following postfix expressions using stack
1. 5 6 7 * + 8 3 /
2. 20 13 15 14 + 18 + 8 * * *
3. 5 6 7 8 + 3 * + 12 * 11 * 4 * * 5 +
Balancing Symbols
• Stacks can be used to check if the given expression has
balanced symbols or not
• Algorithm
1. Create a stack
2. While end of input is not reached
a) If the character read is not a symbol to be balanced, ignore it
b) If the character is an opening delimiter like ( , { or [ , PUSH it into the stack
c) If it is a closing symbol like ) , } , ] , then if the stack is empty report an error,
otherwise POP the stack
d) If the symbol POP-ed is not the corresponding delimiter, report an error
3. At the end of the input, if the stack is not empty report an error
Example – Balancing Symbols
[ { ( a – b ) * ( c – d ) } / f ]
Expression
Stack
[
{
(
(
Processing Function Calls
• Suppose we have a program containing three functions: A, B, and C.
function A invokes function B, which invokes the function C.
main()
{
………………
………………
funA();
return;
}
funA()
{
………………
………………
funB();
return;
}
funB()
{
………………
………………
funC();
return;
}
funC()
{
………………
………………
………………
return;
}
Stack
funA()
funB()
funC()
Reversing String
• Procedure
– Create an empty stack.
– One by one push all characters of string to stack.
– One by one pop all characters from stack and put them back to string
C o m p u t e r
String
Stack
C
Output
o
m
p
u
t
e
r
r e t u pm o C

More Related Content

Similar to Stack_Overview_Implementation_WithVode.pptx

Stack and its Applications : Data Structures ADT
Stack and its Applications : Data Structures ADTStack and its Applications : Data Structures ADT
Stack and its Applications : Data Structures ADTSoumen Santra
 
What is Stack, Its Operations, Queue, Circular Queue, Priority Queue
What is Stack, Its Operations, Queue, Circular Queue, Priority QueueWhat is Stack, Its Operations, Queue, Circular Queue, Priority Queue
What is Stack, Its Operations, Queue, Circular Queue, Priority QueueBalwant Gorad
 
Stack application
Stack applicationStack application
Stack applicationStudent
 
Application of Stack For Expression Evaluation by Prakash Zodge DSY 41.pptx
Application of Stack For Expression Evaluation by Prakash Zodge DSY 41.pptxApplication of Stack For Expression Evaluation by Prakash Zodge DSY 41.pptx
Application of Stack For Expression Evaluation by Prakash Zodge DSY 41.pptxPrakash Zodge
 
358 33 powerpoint-slides_9-stacks-queues_chapter-9
358 33 powerpoint-slides_9-stacks-queues_chapter-9358 33 powerpoint-slides_9-stacks-queues_chapter-9
358 33 powerpoint-slides_9-stacks-queues_chapter-9sumitbardhan
 
Stacks,queues,linked-list
Stacks,queues,linked-listStacks,queues,linked-list
Stacks,queues,linked-listpinakspatel
 
Lec5-Stack-bukc-28022024-112316am (1) .pptx
Lec5-Stack-bukc-28022024-112316am (1) .pptxLec5-Stack-bukc-28022024-112316am (1) .pptx
Lec5-Stack-bukc-28022024-112316am (1) .pptxhaaamin01
 
Stack in Data Structure
Stack in Data StructureStack in Data Structure
Stack in Data StructureUshaP15
 
Stacks IN DATA STRUCTURES
Stacks IN DATA STRUCTURESStacks IN DATA STRUCTURES
Stacks IN DATA STRUCTURESSowmya Jyothi
 
5 chapter3 list_stackqueuepart2
5 chapter3 list_stackqueuepart25 chapter3 list_stackqueuepart2
5 chapter3 list_stackqueuepart2SSE_AndyLi
 

Similar to Stack_Overview_Implementation_WithVode.pptx (20)

Unit 3 stack
Unit   3 stackUnit   3 stack
Unit 3 stack
 
DS UNIT1_STACKS.pptx
DS UNIT1_STACKS.pptxDS UNIT1_STACKS.pptx
DS UNIT1_STACKS.pptx
 
Stack and its Applications : Data Structures ADT
Stack and its Applications : Data Structures ADTStack and its Applications : Data Structures ADT
Stack and its Applications : Data Structures ADT
 
stack & queue
stack & queuestack & queue
stack & queue
 
STACK1.pptx
STACK1.pptxSTACK1.pptx
STACK1.pptx
 
Stack in Sata Structure
Stack in Sata StructureStack in Sata Structure
Stack in Sata Structure
 
What is Stack, Its Operations, Queue, Circular Queue, Priority Queue
What is Stack, Its Operations, Queue, Circular Queue, Priority QueueWhat is Stack, Its Operations, Queue, Circular Queue, Priority Queue
What is Stack, Its Operations, Queue, Circular Queue, Priority Queue
 
Stacks in c++
Stacks in c++Stacks in c++
Stacks in c++
 
Stack application
Stack applicationStack application
Stack application
 
Application of Stack For Expression Evaluation by Prakash Zodge DSY 41.pptx
Application of Stack For Expression Evaluation by Prakash Zodge DSY 41.pptxApplication of Stack For Expression Evaluation by Prakash Zodge DSY 41.pptx
Application of Stack For Expression Evaluation by Prakash Zodge DSY 41.pptx
 
358 33 powerpoint-slides_9-stacks-queues_chapter-9
358 33 powerpoint-slides_9-stacks-queues_chapter-9358 33 powerpoint-slides_9-stacks-queues_chapter-9
358 33 powerpoint-slides_9-stacks-queues_chapter-9
 
Stacks,queues,linked-list
Stacks,queues,linked-listStacks,queues,linked-list
Stacks,queues,linked-list
 
Lec5-Stack-bukc-28022024-112316am (1) .pptx
Lec5-Stack-bukc-28022024-112316am (1) .pptxLec5-Stack-bukc-28022024-112316am (1) .pptx
Lec5-Stack-bukc-28022024-112316am (1) .pptx
 
Stack in Data Structure
Stack in Data StructureStack in Data Structure
Stack in Data Structure
 
CH4.pptx
CH4.pptxCH4.pptx
CH4.pptx
 
MO 2020 DS Stacks 3 AB.ppt
MO 2020 DS Stacks 3 AB.pptMO 2020 DS Stacks 3 AB.ppt
MO 2020 DS Stacks 3 AB.ppt
 
5.-Stacks.pptx
5.-Stacks.pptx5.-Stacks.pptx
5.-Stacks.pptx
 
Stacks IN DATA STRUCTURES
Stacks IN DATA STRUCTURESStacks IN DATA STRUCTURES
Stacks IN DATA STRUCTURES
 
Introduction To Stack
Introduction To StackIntroduction To Stack
Introduction To Stack
 
5 chapter3 list_stackqueuepart2
5 chapter3 list_stackqueuepart25 chapter3 list_stackqueuepart2
5 chapter3 list_stackqueuepart2
 

Recently uploaded

Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxDecoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxJoão Esperancinha
 
main PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidmain PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidNikhilNagaraju
 
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...Soham Mondal
 
Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.eptoze12
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVRajaP95
 
power system scada applications and uses
power system scada applications and usespower system scada applications and uses
power system scada applications and usesDevarapalliHaritha
 
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escortsranjana rawat
 
Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...VICTOR MAESTRE RAMIREZ
 
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...ZTE
 
HARMONY IN THE HUMAN BEING - Unit-II UHV-2
HARMONY IN THE HUMAN BEING - Unit-II UHV-2HARMONY IN THE HUMAN BEING - Unit-II UHV-2
HARMONY IN THE HUMAN BEING - Unit-II UHV-2RajaP95
 
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...srsj9000
 
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort serviceGurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort servicejennyeacort
 
GDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSCAESB
 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130Suhani Kapoor
 
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfCCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfAsst.prof M.Gokilavani
 
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝soniya singh
 
Past, Present and Future of Generative AI
Past, Present and Future of Generative AIPast, Present and Future of Generative AI
Past, Present and Future of Generative AIabhishek36461
 
SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )Tsuyoshi Horigome
 

Recently uploaded (20)

Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxDecoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
 
main PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidmain PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfid
 
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
 
Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
 
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
 
power system scada applications and uses
power system scada applications and usespower system scada applications and uses
power system scada applications and uses
 
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Serviceyoung call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
 
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
 
Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...
 
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...
 
HARMONY IN THE HUMAN BEING - Unit-II UHV-2
HARMONY IN THE HUMAN BEING - Unit-II UHV-2HARMONY IN THE HUMAN BEING - Unit-II UHV-2
HARMONY IN THE HUMAN BEING - Unit-II UHV-2
 
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
 
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort serviceGurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
 
GDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentation
 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
 
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfCCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
 
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
 
Past, Present and Future of Generative AI
Past, Present and Future of Generative AIPast, Present and Future of Generative AI
Past, Present and Future of Generative AI
 
SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )
 

Stack_Overview_Implementation_WithVode.pptx

  • 1. 19CSE111 Fundamentals of Data Structures Stack Course Instructor Dr. M. Rajasekhar Reddy
  • 3. What is Stack? • A stack is a collection of objects that are inserted and removed according to the last-in, first-out (LIFO) principle – A user may insert objects into a stack at any time, but may only access or remove the most recently inserted object that remains at the so- called “top” of the stack
  • 4. Stack Applications • Internet Web browsers store the addresses of recently visited sites in a stack
  • 5. Stack Applications • Text editors usually provide an “undo” mechanism that cancels recent editing operations and reverts to former states of a document – This undo operation can be accomplished by keeping text changes in a stack
  • 6. Stack Example • Push(10) • Push(15) • Push(20) • Push(25) • Pop() • Pop() 10 15 20 25
  • 7. The Stack Abstract Data Type • ADT: Abstract Data type is a type for objects whose behavior is defined by a set of value and a set of operations • Formally, a stack is an abstract data type (ADT) such that an instance S supports the following two methods: – S.push(e): Add element e to the top of stack S – S.pop(): Remove and return the top element from the stack S; an error occurs if the stack is empty
  • 8. The Stack Abstract Data Type • Additionally, let us define the following accessor methods for convenience: – S.top(): Return a reference to the top element of stack S, without removing it; an error occurs if the stack is empty – S.is_empty( ): Return True if stack S does not contain any elements – len(S): Return the number of elements in stack S
  • 9. Working of Stack • A pointer called TOP is used to keep track of the top element in the stack • When initializing the stack, we set its value to -1 so that we can check if the stack is empty by comparing TOP == -1 • On pushing an element, we increase the value of TOP and place the new element in the position pointed to by TOP • On popping an element, we return the element pointed to by TOP and reduce its value. • Before pushing, we check if the stack is already full • Before popping, we check if the stack is already empty
  • 10. Stack Example • Push(10) • Push(15) • Push(20) • Len() • Top() • IsEmpty() • Push(25) • Pop() • Pop() • Len() 10 TOP 15 20 = 3 = 2 25 = False = 2 … 5 4 3 2 1 0
  • 11. Stack Operations – push() • The procedure to perform push is here: begin procedure push: stack, data if stack is full return null endif top ← top + 1 stack[top] ← data end procedure void push(int data) { if(!isFull()) { top = top + 1; stack[top] = data; } else { printf("Stack is full.n"); } }
  • 12. Stack Operations – pop() • The procedure to perform pop is here: begin procedure pop: stack if stack is empty return null endif data ← stack[top] top ← top - 1 return data end procedure int pop(int data) { if(!isempty()) { data = stack[top]; top = top - 1; return data; } else { printf("Stack is empty.n"); } }
  • 13. Stack Operations – is_empty() • The procedure to check the emptiness of a stack is here: begin procedure isempty if top less than 1 return true else return false endif end procedure int isempty() { if(top == -1) return 1; else return 0; }
  • 14. Stack Operations – top() • The procedure to return the reference to top element is here: begin procedure peek return stack[top] end procedure int peek() { return stack[top]; }
  • 16. Stack Applications in Computer Engineering • Infix to Postfix Conversion – The stack can be used to convert some infix expression into its postfix equivalent, or prefix equivalent – These postfix or prefix notations are used in computers to express some expressions • Expression Evaluation – We also need the help of stack data structure to evaluate postfix expressions • Balancing Symbols – Frequently a lack of one symbol will cause the compiler to spill out a hundred lines of diagnostics without identifying the real error – Stack can be used to balance the symbols such as {}, (), [] etc.
  • 17. Stack Applications in Computer Engineering • Processing Function Calls – Stack plays an important role in programs that call several functions in succession • Reverse a String – A Stack can be used to reverse the characters of a string
  • 18. Infix to Postfix Conversion • Infix Notation – The infix notation is a convenient way of writing an expression in which each operator is placed between the operands • Prefix Notation – The prefix notation places the operator before the operands (Polish notation) • Postfix Notation – The postfix notation places the operator after the operands (Reverse Polish Notation) Infix Notation Prefix Notation Postfix Notation A * B * A B AB* (A+B)/C /+ ABC AB+C/ (A*B) + (D-C) +*AB - DC AB*DC-+
  • 19. Algorithm • Step 1 : Scan the Infix Expression from left to right one character at a time. • Step 2 : If the scanned character is an operand, append it to the output. • Step 3 : Else, if it is an operator, – Step 3.1 : If the precedence of the scanned operator is greater than that of the operator in the stack (or the stack is empty or the stack contains a ‘(‘ or ‘[‘ or ‘{‘), push it on stack. – Step 3.2 : Else, Pop all the operators from the stack which are greater than or equal to in precedence than that of the scanned operator. • Step 4 : If the scanned character is an ‘(‘ or ‘[‘ or ‘{‘, push it onto the stack • Step 5 : If the scanned character is an ‘)’or ‘]’ or ‘}’, pop the stack and output it until a ‘(‘ or ‘[‘ or ‘{‘ respectively is encountered, and discard both the parenthesis • Step 6 : Repeat steps 2-6 until infix expression is scanned • Step 7 : Print the output • Step 8 : Pop and output from the stack until it is not empty
  • 20. Example – Infix to Postfix Conversion ( A + B / C + D * ( E – F ) ^ G ) Expression Stack Output ( A + B / C / + + D * ( E - F - ^ G ^ * +
  • 21. Exercise Questions 1. Convert the infix expression ( AX * ( BX * ( ( ( CY + AY ) + BY ) * CX ) ) ) to its postfix equivalent using stack. 2. Convert the infix expression ( ( H * ( ( ( ( A + ( ( B + C ) * D ) ) * F ) * G ) * E ) ) + J ) to its postfix equivalent using stack. 3. Convert the infix expression a + b * ( c ^ d – e ) ^ ( f + g * h ) - i to its postfix equivalent using stack.
  • 22. Postfix Expression Evaluation • The expressions written in postfix form are evaluated faster compared to infix notation • Algorithm 1. Create a stack to store operands 2. Scan the given expression and do the following for every scanned element a. If the element is a number, push it onto the stack b. If the element is an operator, pop operands for the operator from the stack. Evaluate the operator and push the result back to the stack 3. When the expression is ended, the number in the stack is the final answer
  • 23. Example – Expression Evaluation 2 2 1 * + 9 - Expression Stack Calculations 2 3 1 * 1 2 = 2 2 + 2 2 = 4 4 9 - 9 4 = 5 5
  • 24. Exercise Questions • Evaluate the following postfix expressions using stack 1. 5 6 7 * + 8 3 / 2. 20 13 15 14 + 18 + 8 * * * 3. 5 6 7 8 + 3 * + 12 * 11 * 4 * * 5 +
  • 25. Balancing Symbols • Stacks can be used to check if the given expression has balanced symbols or not • Algorithm 1. Create a stack 2. While end of input is not reached a) If the character read is not a symbol to be balanced, ignore it b) If the character is an opening delimiter like ( , { or [ , PUSH it into the stack c) If it is a closing symbol like ) , } , ] , then if the stack is empty report an error, otherwise POP the stack d) If the symbol POP-ed is not the corresponding delimiter, report an error 3. At the end of the input, if the stack is not empty report an error
  • 26. Example – Balancing Symbols [ { ( a – b ) * ( c – d ) } / f ] Expression Stack [ { ( (
  • 27. Processing Function Calls • Suppose we have a program containing three functions: A, B, and C. function A invokes function B, which invokes the function C. main() { ……………… ……………… funA(); return; } funA() { ……………… ……………… funB(); return; } funB() { ……………… ……………… funC(); return; } funC() { ……………… ……………… ……………… return; } Stack funA() funB() funC()
  • 28. Reversing String • Procedure – Create an empty stack. – One by one push all characters of string to stack. – One by one pop all characters from stack and put them back to string C o m p u t e r String Stack C Output o m p u t e r r e t u pm o C