The document discusses stacks in C++. It defines a stack as a data structure that follows LIFO (Last In First Out) principle where the last element added is the first to be removed. Stacks can be implemented using arrays or linked lists. The key operations on a stack are push which adds an element and pop which removes an element. Example applications of stacks include function call stacks, converting infix to postfix notation, and reversing arrays.
The document discusses stacks and their implementation using linked lists. It defines a stack as a last-in, first-out data structure that allows insertion and deletion of elements only at one end, called the top. Key stack operations like push, pop and peek are described along with their algorithms. Linked list and array implementations of stacks are presented. Common applications of stacks like balancing symbols, postfix notation calculators, and infix to postfix conversion are also covered.
A stack is a data structure where items can only be inserted and removed from one end. The last item inserted is the first item removed (LIFO). Common examples include stacks of books, plates, or bank transactions. Key stack operations are push to insert, pop to remove, and functions to check if the stack is empty or full. Stacks can be used to implement operations like reversing a string, converting infix to postfix notation, and evaluating arithmetic expressions.
The document discusses stacks and their applications. It defines a stack as a Last In First Out (LIFO) data structure. Key stack operations are described such as push, pop, and peek. An array implementation of a stack is presented and applications like checking balanced brackets, converting infix to postfix notation, and postfix calculators are covered. Example code and diagrams are provided to illustrate stack concepts and operations.
The document describes implementing a queue using an array. It provides algorithms for enQueue() and deQueue() operations. EnQueue() inserts elements at the rear by incrementing rear and checking for full. DeQueue() deletes elements from the front by incrementing front and checking for empty. The queue uses front and rear pointers to manage insertion and deletion of elements based on FIFO principle using an underlying fixed-size array.
This document discusses stacks as a data structure. It defines a stack as a Last In First Out (LIFO) structure where newly added items are placed on top. The core stack operations of push, pop, and peek are described. An array implementation of a stack is presented, along with applications like checking for balanced braces, converting infix to postfix notation, and a postfix calculator. Pseudocode provides an algorithm for infix to postfix conversion using a stack.
A stack is a last-in, first-out data structure where elements can only be inserted or removed from one end. Stacks follow the LIFO principle. The document discusses stacks and their use in evaluating arithmetic expressions. Expressions can be written in infix, prefix, or postfix notation, and converting an infix expression to postfix notation allows it to be evaluated using a stack, with operators and operands pushed and popped off the stack from left to right.
STACK AND ITS OPERATIONS IN DATA STRUCTURES.pptxKALPANAC20
This document discusses stacks as a data structure. It defines a stack as a Last In First Out (LIFO) structure where the last item inserted is the first out. Key stack operations like push, pop, peek and isEmpty are described. Examples of stack applications include checking for balanced brackets, converting infix to postfix notation, implementing an undo/redo feature and recursion. The document also provides pseudocode for converting an infix expression to postfix using a stack and describes how a postfix calculator evaluates expressions.
This document provides information about stacks as a data structure. It defines a stack as a linear data structure that only allows additions or deletions at one end, following the last-in, first-out (LIFO) principle. The key operations on a stack are push, which adds an element to the top, and pop, which removes an element from the top. The document also discusses stack terminology, different types of notations for expressions (infix, prefix, postfix), algorithms for converting between notations using a stack, an example C program for implementing a stack using an array, and summarizes the main characteristics of stacks.
The document discusses different implementations of stacks and queues using linked lists and arrays. It describes how stacks can be implemented using a linked list, with push and pop operations adding and removing nodes from the head of the list. Queues can also be implemented with linked lists or arrays, but arrays require additional logic to maintain first-in, first-out order efficiently. Common applications of stacks and queues include evaluating expressions, checking for balanced brackets, and managing tasks in operating systems and server requests.
The document discusses stacks and their implementation and operations. It defines a stack as a linear data structure that follows the LIFO principle. Stacks can be implemented using arrays or linked lists. The two main operations on a stack are push and pop. Push adds an element to the top of the stack, while pop removes an element from the top. The document provides examples of these operations and discusses applications of stacks such as converting infix to postfix notation.
An abstract data type (ADT) is a mathematical model for data structures that defines the type solely by the operations that can be performed on it. For example, a stack ADT can be defined by two operations: push, which inserts an item into the stack, and pop, which removes the top item. Abstract data types are theoretical entities used to simplify algorithm descriptions and formally describe programming language type systems. A stack is a linear data structure that follows the last-in, first-out principle, where items can only be inserted or removed from one end of the stack. Stacks have common applications in arithmetic expression evaluation, backtracking, and function call processing.
The document discusses stacks and their applications. It provides 3 key points:
1. A stack is an abstract data type that follows LIFO (last-in, first-out) principles with push and pop operations. Functions like stack_full and stack_empty are used to implement stacks.
2. Stacks have applications in converting infix notation to postfix notation and evaluating postfix expressions. The algorithms pop and push operators and operands to convert between notations and perform calculations.
3. Examples show converting the infix expression (A * B + (C - D / E)) to postfix and evaluating the postfix expression 50, 60, +, 20, 10, -, *.
The document discusses stacks and their applications. It provides 3 key points:
1. A stack is an abstract data type that follows LIFO (last-in, first-out) principles with push and pop operations. Functions like stack_full and stack_empty are used to implement stacks.
2. Stacks have applications in converting infix notation to postfix notation and evaluating postfix expressions. The algorithms pop and push operators and operands to produce the postfix form or calculate values.
3. Examples show converting the infix expression (A * B + (C - D / E)) to postfix AB*C(D-F/)++ and evaluating the postfix form of True, False, NOT, AND,
Stacks are linear data structures that follow the LIFO (last in, first out) principle. Elements are added and removed from the top of the stack. Stacks have two main operations - Push, which adds an element to the top of the stack, and Pop, which removes an element from the top. Stacks can be implemented using arrays or linked lists. Compilers use stacks to convert infix arithmetic expressions to postfix notation and then evaluate the postfix expression. This process involves pushing and popping operators from the stack.
The document discusses stacks and queues. It begins by defining a stack as a collection of homogeneous data elements where insertion and deletion occurs at one end, known as the top. The stack follows the LIFO (last in, first out) principle. Common stack operations like push and pop are introduced. Implementing stacks using both arrays and linked lists is covered. Applications of stacks like balancing symbols, recursion, undo operations are mentioned. The document then moves to discussing queues, their applications and implementations. Priority queues and their applications are also briefly covered.
The document discusses stacks and stack operations in C. It defines a stack as a linear data structure that follows the LIFO principle, where elements are added and removed from one end called the top. The key stack operations are PUSH to add an element, POP to remove an element, and DISPLAY to output the stack. It provides algorithms for implementing PUSH and POP and handling exceptions like stack overflow. The document also covers postfix notation, where operators follow operands, and the postfix evaluation algorithm using a stack.
In computer science, a stack is an abstract data type that serves as a collection of elements, with two main principal operations: push, which adds an element to the collection, and pop, which removes the most recently added element that was not yet removed.
Data structures like stacks, queues, and priority queues can be implemented in Python using built-in data types and modules. Stacks follow LIFO order, with new items added to the top and removed from the top. Queues follow FIFO order, with new items added to the back and removed from the front. Priority queues order items by priority, so the highest or lowest priority item is removed first depending on the implementation.
A stack is a linear data structure that follows the LIFO (last in, first out) principle. Elements are inserted and removed from the top of the stack. Common stack operations include push to add an element and pop to remove the top element. Stacks can be implemented using arrays or linked lists. Stacks are useful for operations like converting infix expressions to postfix and evaluating postfix expressions using a stack to hold operands. Queues follow the FIFO (first in, first out) principle with elements added to the rear and removed from the front. Common queue operations are enqueue to add and dequeue to remove elements. Queues can also be implemented using arrays or linked lists. Linked lists store elements in nodes with each node
The document discusses converting infix expressions to postfix expressions using a stack. It defines infix and postfix expressions, provides examples of each, and presents an algorithm that uses a stack to scan an infix expression from left to right and output an equivalent postfix expression. Key steps include pushing operators to the stack based on precedence and popping operators to the output when encountering operands and parentheses.
The document discusses stacks, which are linear data structures that follow the LIFO (last in, first out) principle. Stacks can be implemented using arrays or linked lists. Elements are inserted and removed only from one end, called the top of the stack. Insertion is called pushing and removal is called popping. Stacks are used extensively in computer systems, for example in operating system function calls and interrupt handling. The Java programming language contains a Stack class that can be used by programmers.
The document discusses stacks and their implementation and use. It begins by defining a stack as a collection of items that can be inserted or deleted from one end, following the LIFO principle. Common stack operations like push, pop and top are introduced. Stacks can be implemented using arrays or linked lists. The use of stacks to evaluate expressions in prefix, infix and postfix notations is covered, including the precedence of operators and the process to convert infix to postfix notation using a stack. Parentheses are also discussed in the conversion process.
This document discusses stacks and their use as a data structure. It begins by defining what a stack is, namely a data structure that follows the last-in, first-out (LIFO) principle. Common stack operations like push, pop, and peek are introduced. Examples are given of how stacks are used in applications like undo mechanisms, expression evaluation, and validating parentheses in expressions. The key operations and algorithms for implementing a stack are described, including pseudocode for push, pop, and display functions. Finally, examples are provided of how stacks can be used to reverse strings, validate parentheses, and convert infix expressions to postfix form.
A stack is a last-in, first-out data structure where elements can only be inserted or removed from one end. Stacks follow the LIFO principle. The document discusses stacks and their use in evaluating arithmetic expressions. Expressions can be written in infix, prefix, or postfix notation, and converting an infix expression to postfix notation allows it to be evaluated using a stack, with operators and operands pushed and popped off the stack from left to right.
STACK AND ITS OPERATIONS IN DATA STRUCTURES.pptxKALPANAC20
This document discusses stacks as a data structure. It defines a stack as a Last In First Out (LIFO) structure where the last item inserted is the first out. Key stack operations like push, pop, peek and isEmpty are described. Examples of stack applications include checking for balanced brackets, converting infix to postfix notation, implementing an undo/redo feature and recursion. The document also provides pseudocode for converting an infix expression to postfix using a stack and describes how a postfix calculator evaluates expressions.
This document provides information about stacks as a data structure. It defines a stack as a linear data structure that only allows additions or deletions at one end, following the last-in, first-out (LIFO) principle. The key operations on a stack are push, which adds an element to the top, and pop, which removes an element from the top. The document also discusses stack terminology, different types of notations for expressions (infix, prefix, postfix), algorithms for converting between notations using a stack, an example C program for implementing a stack using an array, and summarizes the main characteristics of stacks.
The document discusses different implementations of stacks and queues using linked lists and arrays. It describes how stacks can be implemented using a linked list, with push and pop operations adding and removing nodes from the head of the list. Queues can also be implemented with linked lists or arrays, but arrays require additional logic to maintain first-in, first-out order efficiently. Common applications of stacks and queues include evaluating expressions, checking for balanced brackets, and managing tasks in operating systems and server requests.
The document discusses stacks and their implementation and operations. It defines a stack as a linear data structure that follows the LIFO principle. Stacks can be implemented using arrays or linked lists. The two main operations on a stack are push and pop. Push adds an element to the top of the stack, while pop removes an element from the top. The document provides examples of these operations and discusses applications of stacks such as converting infix to postfix notation.
An abstract data type (ADT) is a mathematical model for data structures that defines the type solely by the operations that can be performed on it. For example, a stack ADT can be defined by two operations: push, which inserts an item into the stack, and pop, which removes the top item. Abstract data types are theoretical entities used to simplify algorithm descriptions and formally describe programming language type systems. A stack is a linear data structure that follows the last-in, first-out principle, where items can only be inserted or removed from one end of the stack. Stacks have common applications in arithmetic expression evaluation, backtracking, and function call processing.
The document discusses stacks and their applications. It provides 3 key points:
1. A stack is an abstract data type that follows LIFO (last-in, first-out) principles with push and pop operations. Functions like stack_full and stack_empty are used to implement stacks.
2. Stacks have applications in converting infix notation to postfix notation and evaluating postfix expressions. The algorithms pop and push operators and operands to convert between notations and perform calculations.
3. Examples show converting the infix expression (A * B + (C - D / E)) to postfix and evaluating the postfix expression 50, 60, +, 20, 10, -, *.
The document discusses stacks and their applications. It provides 3 key points:
1. A stack is an abstract data type that follows LIFO (last-in, first-out) principles with push and pop operations. Functions like stack_full and stack_empty are used to implement stacks.
2. Stacks have applications in converting infix notation to postfix notation and evaluating postfix expressions. The algorithms pop and push operators and operands to produce the postfix form or calculate values.
3. Examples show converting the infix expression (A * B + (C - D / E)) to postfix AB*C(D-F/)++ and evaluating the postfix form of True, False, NOT, AND,
Stacks are linear data structures that follow the LIFO (last in, first out) principle. Elements are added and removed from the top of the stack. Stacks have two main operations - Push, which adds an element to the top of the stack, and Pop, which removes an element from the top. Stacks can be implemented using arrays or linked lists. Compilers use stacks to convert infix arithmetic expressions to postfix notation and then evaluate the postfix expression. This process involves pushing and popping operators from the stack.
The document discusses stacks and queues. It begins by defining a stack as a collection of homogeneous data elements where insertion and deletion occurs at one end, known as the top. The stack follows the LIFO (last in, first out) principle. Common stack operations like push and pop are introduced. Implementing stacks using both arrays and linked lists is covered. Applications of stacks like balancing symbols, recursion, undo operations are mentioned. The document then moves to discussing queues, their applications and implementations. Priority queues and their applications are also briefly covered.
The document discusses stacks and stack operations in C. It defines a stack as a linear data structure that follows the LIFO principle, where elements are added and removed from one end called the top. The key stack operations are PUSH to add an element, POP to remove an element, and DISPLAY to output the stack. It provides algorithms for implementing PUSH and POP and handling exceptions like stack overflow. The document also covers postfix notation, where operators follow operands, and the postfix evaluation algorithm using a stack.
In computer science, a stack is an abstract data type that serves as a collection of elements, with two main principal operations: push, which adds an element to the collection, and pop, which removes the most recently added element that was not yet removed.
Data structures like stacks, queues, and priority queues can be implemented in Python using built-in data types and modules. Stacks follow LIFO order, with new items added to the top and removed from the top. Queues follow FIFO order, with new items added to the back and removed from the front. Priority queues order items by priority, so the highest or lowest priority item is removed first depending on the implementation.
A stack is a linear data structure that follows the LIFO (last in, first out) principle. Elements are inserted and removed from the top of the stack. Common stack operations include push to add an element and pop to remove the top element. Stacks can be implemented using arrays or linked lists. Stacks are useful for operations like converting infix expressions to postfix and evaluating postfix expressions using a stack to hold operands. Queues follow the FIFO (first in, first out) principle with elements added to the rear and removed from the front. Common queue operations are enqueue to add and dequeue to remove elements. Queues can also be implemented using arrays or linked lists. Linked lists store elements in nodes with each node
The document discusses converting infix expressions to postfix expressions using a stack. It defines infix and postfix expressions, provides examples of each, and presents an algorithm that uses a stack to scan an infix expression from left to right and output an equivalent postfix expression. Key steps include pushing operators to the stack based on precedence and popping operators to the output when encountering operands and parentheses.
The document discusses stacks, which are linear data structures that follow the LIFO (last in, first out) principle. Stacks can be implemented using arrays or linked lists. Elements are inserted and removed only from one end, called the top of the stack. Insertion is called pushing and removal is called popping. Stacks are used extensively in computer systems, for example in operating system function calls and interrupt handling. The Java programming language contains a Stack class that can be used by programmers.
The document discusses stacks and their implementation and use. It begins by defining a stack as a collection of items that can be inserted or deleted from one end, following the LIFO principle. Common stack operations like push, pop and top are introduced. Stacks can be implemented using arrays or linked lists. The use of stacks to evaluate expressions in prefix, infix and postfix notations is covered, including the precedence of operators and the process to convert infix to postfix notation using a stack. Parentheses are also discussed in the conversion process.
This document discusses stacks and their use as a data structure. It begins by defining what a stack is, namely a data structure that follows the last-in, first-out (LIFO) principle. Common stack operations like push, pop, and peek are introduced. Examples are given of how stacks are used in applications like undo mechanisms, expression evaluation, and validating parentheses in expressions. The key operations and algorithms for implementing a stack are described, including pseudocode for push, pop, and display functions. Finally, examples are provided of how stacks can be used to reverse strings, validate parentheses, and convert infix expressions to postfix form.
This document provides an overview of key concepts in English grammar, including sentences, parts of speech, punctuation, and the structure of sentences. It begins by defining what a sentence is and explaining that sentences contain subjects, verbs, and sometimes objects. It then describes the main parts of speech - nouns, pronouns, adjectives, verbs, adverbs - and supporting parts of speech like articles, conjunctions, prepositions, and interjections. Various punctuation marks and their uses are outlined. The document concludes by explaining the key parts of a sentence including the subject, verb, direct object, and indirect object.
This document provides a summary of lecture 3 on functional English. It discusses the formation of words through morphology and the different types of morphemes. It also explains prefixes, suffixes, and infixes as types of affixes. The document defines key terms like dictionary, clauses, active and passive voice, and different types of sentences. It provides examples of simple, compound and complex sentences. Overall, the document covers word formation, grammar rules, and sentence structure in the English language.
Ajanta Paintings: Study as a Source of HistoryVirag Sontakke
This Presentation is prepared for Graduate Students. A presentation that provides basic information about the topic. Students should seek further information from the recommended books and articles. This presentation is only for students and purely for academic purposes. I took/copied the pictures/maps included in the presentation are from the internet. The presenter is thankful to them and herewith courtesy is given to all. This presentation is only for academic purposes.
Rock Art As a Source of Ancient Indian HistoryVirag Sontakke
This Presentation is prepared for Graduate Students. A presentation that provides basic information about the topic. Students should seek further information from the recommended books and articles. This presentation is only for students and purely for academic purposes. I took/copied the pictures/maps included in the presentation are from the internet. The presenter is thankful to them and herewith courtesy is given to all. This presentation is only for academic purposes.
Transform tomorrow: Master benefits analysis with Gen AI today webinar
Wednesday 30 April 2025
Joint webinar from APM AI and Data Analytics Interest Network and APM Benefits and Value Interest Network
Presenter:
Rami Deen
Content description:
We stepped into the future of benefits modelling and benefits analysis with this webinar on Generative AI (Gen AI), presented on Wednesday 30 April. Designed for all roles responsible in value creation be they benefits managers, business analysts and transformation consultants. This session revealed how Gen AI can revolutionise the way you identify, quantify, model, and realised benefits from investments.
We started by discussing the key challenges in benefits analysis, such as inaccurate identification, ineffective quantification, poor modelling, and difficulties in realisation. Learnt how Gen AI can help mitigate these challenges, ensuring more robust and effective benefits analysis.
We explored current applications and future possibilities, providing attendees with practical insights and actionable recommendations from industry experts.
This webinar provided valuable insights and practical knowledge on leveraging Gen AI to enhance benefits analysis and modelling, staying ahead in the rapidly evolving field of business transformation.
Ancient Stone Sculptures of India: As a Source of Indian HistoryVirag Sontakke
This Presentation is prepared for Graduate Students. A presentation that provides basic information about the topic. Students should seek further information from the recommended books and articles. This presentation is only for students and purely for academic purposes. I took/copied the pictures/maps included in the presentation are from the internet. The presenter is thankful to them and herewith courtesy is given to all. This presentation is only for academic purposes.
Mental Health Assessment in 5th semester bsc. nursing and also used in 2nd ye...parmarjuli1412
Mental Health Assessment in 5th semester Bsc. nursing and also used in 2nd year GNM nursing. in included introduction, definition, purpose, methods of psychiatric assessment, history taking, mental status examination, psychological test and psychiatric investigation
How to Clean Your Contacts Using the Deduplication Menu in Odoo 18Celine George
In this slide, we’ll discuss on how to clean your contacts using the Deduplication Menu in Odoo 18. Maintaining a clean and organized contact database is essential for effective business operations.
What is the Philosophy of Statistics? (and how I was drawn to it)jemille6
What is the Philosophy of Statistics? (and how I was drawn to it)
Deborah G Mayo
At Dept of Philosophy, Virginia Tech
April 30, 2025
ABSTRACT: I give an introductory discussion of two key philosophical controversies in statistics in relation to today’s "replication crisis" in science: the role of probability, and the nature of evidence, in error-prone inference. I begin with a simple principle: We don’t have evidence for a claim C if little, if anything, has been done that would have found C false (or specifically flawed), even if it is. Along the way, I’ll sprinkle in some autobiographical reflections.
Slides to support presentations and the publication of my book Well-Being and Creative Careers: What Makes You Happy Can Also Make You Sick, out in September 2025 with Intellect Books in the UK and worldwide, distributed in the US by The University of Chicago Press.
In this book and presentation, I investigate the systemic issues that make creative work both exhilarating and unsustainable. Drawing on extensive research and in-depth interviews with media professionals, the hidden downsides of doing what you love get documented, analyzing how workplace structures, high workloads, and perceived injustices contribute to mental and physical distress.
All of this is not just about what’s broken; it’s about what can be done. The talk concludes with providing a roadmap for rethinking the culture of creative industries and offers strategies for balancing passion with sustainability.
With this book and presentation I hope to challenge us to imagine a healthier future for the labor of love that a creative career is.
All About the 990 Unlocking Its Mysteries and Its Power.pdfTechSoup
In this webinar, nonprofit CPA Gregg S. Bossen shares some of the mysteries of the 990, IRS requirements — which form to file (990N, 990EZ, 990PF, or 990), and what it says about your organization, and how to leverage it to make your organization shine.
2. Specifications/ADT of Stack
What is Stack?
Representation of Stacks
Operations on Stacks
Push
Pop
IsEmpty
IsFull
Usage
Evaluation of Expressions
Infix to Postfix Conversion
Limited by Array (Fixed Size)
Link List (Dynamic and Variable size)
3. What is Stack?
In QUEUES, insertion and deletion can take place
at both ends, i.e. start and end,
But if insertion and deletion are restricted from one
end, must used concept of STACK.
Stack can be defined as:
A stack is a linear data structure which
can be accessed only at one of its ends
for storing and retrieving data
In which items may be inserted or deleted only
from one end called top of the stack.
There are two ways of implementing a stack:
4. Description of Stack
Stack: A stack is an ordered collection
of items into which items may be
inserted or may be deleted from the
same end.
Last in First Out (LIFO Structure)
Conceptual data structure
Empty_stack () and top_of_stack()
methods just tell the state of stack,
never change the state of stack
5. Description of Stack
Examples
Function calls
Recursion
Expression evaluation
Infix expression is that expression in which
operator lies between operands i.e a=b+c
Postfix expression, in which operator
follows the operands
Prefix expression in which operator accedes
the operands
6. Description of Stack
A common model of a
stack is a plate or coin
stacker. Plates are
"pushed “onto to the top
and "popped" off from
the top.
Stack principle Last-In-
First-Out (LIFO)
queues.
7. Stack terminology
“Top”: where insertion and
deletion takes place
“Push”: is the term used to insert an
element into a stack
“Pop”: is the term used to delete an
element from a stack.
9. Implementation of Stacks
A stack can be implemented using an array
(static & dynamic) and by Link List (Dynamic).
A pointer variable TOP which contains the
location of the top element of the stack
Variable STKSIZE which gives the maximum
number of elements that can be held by the
stack.
10. Note that all operations need to check
array bounds
Pushing onto a full stack: Overflow
When TOP=STKSIZE
Popping an empty stack: Underflow
TOP=NULL
Stack implementation
(Constraints/Limitations)
11. Operations on Stack
A stack is generally implemented with only
two common operations:
Push(): adds an item to a stack
Pop(): extracts/delete the most recently pushed
item from the stack
Other methods such as
Top(): returns the item at the top without
removing it i.e. it tells the state
IsEmpty(): determines whether the stack has
anything in it, so its return type is TRUE/FALSE
IsFull(): determines whether the stack is full or
stacksize is reached to STKSIZE anything in it
12. How the stack routines work
empty stack; push(a); push(b); pop(b);
empty stack
top = 0
a
push(a)
top = 1
b
a
push(b)
top = 2
a
pop()
top = 1
13. Implementation of Stack
(ByUsing Array)
const int STKSIZE = 5;
class cstack
{
private:
int top; //Represents index of array
int STK[STKSIZE];
public:
cstack() {top = -1;}
void push(int x);
int pop();
int emptystack(); //returns true/false
int stacktop(); //tells top most values
};
17. Implementation for return the
top of stack value
void cstack::stacktop()
{
if(emptystack())
{
cout<<“STACK is EMPTY”;
return top;
}
retun STK[top];
}
21. Implementation of pop()
Operation
void cstack::pop()
{if(top=NULL)
{ cout<<“STACK is EMPTY”;
return 0; }
node *p;
p=top;
top=top->link;
int temp=p->info;
delete p;
return temp;}
22. Implementation for return the
top of stack value
void cstack::stacktop()
{
int temp;
temp=pop();
push(temp);
return temp;
}
23. How can you define Expressions?
Arithmetic expression is made up
Operands (Numeric Variables or
Constants)
e.g. A+B*C
Arithmetic Operators (+, -, *, /)
Power Operator (^)
Parentheses ()
The Expression is always evaluated
from left to right
24. How Expressions are executed?
Order in which the expression is evaluated is:
If the expression has parenthesis, then they are
evaluated first
Exponential (^) is given highest priority
Multiplication (*) and division (/) have the next
highest priority
Addition (+) and subtraction (-) have the lowest
priority
27. Infix to Postfix Conversion
Stack is used to convert an infix expression to postfix.
Stack is also used to store operators and operands and then
pass to the postfix expression according to their precedence.
Infix expression is converted into postfix expression according
to the following rules:
Infix expression is scanned from left to right until end of the
expression.
Operands are passed directly to the output.
Operators are first passed to the stacks.
Operand is encountered, it is added to the output.
28. Infix to Postfix Conversion
Each time an operator is read, the stack is repeatedly popped
and operands are passed to the output, until an operator is
reached that has a lower precedence than the most recently
read operator. The most recently read operator is then pushed
into the stack.
When end of the infix expression is reached, all operators
remaining in the stack are popped and passed to the output in
the same sequence.
Parentheses can be used in the infix expression but these are
not used in the postfix expression.
During conversion process, parentheses are treated as
operators that have higher precedence than any other
operator.
29. Infix to Postfix Conversion
Left parenthesis is pushed into the stack, when
encountered.
Right parenthesis is never pushed to the stack.
Left parentheses is popped only when right
parentheses is encountered.
The parentheses are not passed to the output postfix
expressions. They are discarded.
When end of expression is reached, then all
operators from stack are popped and added to the
output.
31. Example (Infix to Postfix
Conversion)
Convert infix expression A+B*C+
(D*E+F)*G into postfix
A + B * C + ( D * E + F ) * G
1. Scanned from left to right. First
operand read is A and passed to
output
Stack
Output: A
32. Example (Infix to Postfix
Conversion)
A + B * C + ( D * E + F ) * G
2. Next the ‘+’ operator is read, at
this stage, stack is empty.
Therefore no operators are
popped and ‘+’ is pushed into the
stack. Thus the stack and output
will be:
+
Stack
Output: A
33. Example (Infix to Postfix
Conversion)
A + B * C + ( D * E + F ) * G
3. Next the ‘B’ operand is read and
passed to the output. Thus the
stack and output will be:
+
Stack
Output: AB
34. Example (Infix to Postfix
Conversion)
A + B * C + ( D * E + F ) * G
4. Next the ‘*’ operator is read, The
stack has ‘+’ operator which has
lower precedence than the ‘*’
operator. Therefore no operators
are popped and ‘*’ is pushed into
the stack. Thus the stack and
output will be:
*
+
Stack
Output: AB
35. Example (Infix to Postfix
Conversion)
A + B * C + ( D * E + F ) * G
5. Next the ‘C’ operand is read and
passed to the output. Thus the
stack and output will be:
*
+
Stack
Output: ABC
36. Example (Infix to Postfix
Conversion)
A + B * C + ( D * E + F ) * G
6. Next the ‘+’ operator is read, The
stack has ‘*’ operator which has
higher precedence than the ‘+’
operator. The Stack is popped and
passed to output. Next stack has
‘+’ operator which has same
precedence than the ‘+’ operator.
The Stack is popped and passed to
output. Now stack is empty,
therefore no operators are popped
and ‘+’ is pushed into the stack.
Thus the stack and output will be:
+
Stack
Output: ABC*+
37. Example (Infix to Postfix
Conversion)
A + B * C + ( D * E + F ) * G
7. Next the left parenthesis ‘(’ is
read, Since all operators have
lower precedence than the left
parenthesis, it is pushed into the
stack. Thus the stack and output
will be:
(
+
Stack
Output: ABC*+
38. Example (Infix to Postfix
Conversion)
A + B * C + ( D * E + F ) * G
8. Next the ‘D’ operand is read and
passed to the output. Thus the
stack and output will be:
(
+
Stack
Output: ABC*+D
39. Example (Infix to Postfix
Conversion)
A + B * C + ( D * E + F ) * G
9. Next the ‘*’ operator is read.
Now, left parenthesis ‘(‘ has
higher precedence than ‘*’; it can
not be popped from the stack
until a right parenthesis ‘)’ has
been read. Thus the stack is not
popped and ‘*’ is pushed into the
stack. Thus the stack and output
will be:
*
(
+
Stack
Output: ABC*+D
40. Example (Infix to Postfix
Conversion)
A + B * C + ( D * E + F ) * G
10. Next the ‘E’ operand is read and
passed to the output. Thus the
stack and output will be:
*
(
+
Stack
Output: ABC*+DE
41. Example (Infix to Postfix
Conversion)
A + B * C + ( D * E + F ) * G
11. Next the ‘+’ operator is read, The
stack has ‘*’ operator which has
higher precedence than the ‘+’
operator. The Stack is popped
and passed to output. Next stack
has left parenthesis ‘(’ which has
not been popped and ‘+’ operator
is pushed into the stack. Thus the
stack and output will be:
+
(
+
Stack
Output: AB*+DE*
42. Example (Infix to Postfix
Conversion)
A + B * C + ( D * E + F ) * G
12. Next the ‘F’ operand is read and
passed to the output. Thus the
stack and output will be:
+
(
+
Stack
Output: ABC*+DE*F
43. Example (Infix to Postfix
Conversion)
A + B * C + ( D * E + F ) * G
13. Next the ‘)’ has encountered
now popped till ‘( ‘ and passed to
the output. Thus the stack and
output will be:
+
Stack
Output: ABC*+DE*F+
44. Example (Infix to Postfix
Conversion)
A + B * C + ( D * E + F ) * G
14. Next the ‘*’ operator is read, The
stack has ‘+’ operator which has
lower precedence than the ‘*’
operator. Therefore no operators
are popped and ‘*’ is pushed into
the stack. Thus the stack and
output will be:
*
+
Stack
Output: ABC*+DE*F+
45. Example (Infix to Postfix
Conversion)
A + B * C + ( D * E + F ) * G
15. Next the ‘G’ operand is read and
passed to the output. Thus the
stack and output will be:
*
+
Stack
Output: ABC*+DE*F+G
46. Example (Infix to Postfix
Conversion)
A + B * C + ( D * E + F ) * G
16. The end of expression is
encountered. The operators are
popped from the stacked and
passed to the output in the same
sequence in which these are
popped. Thus the stack and
output will be:
Stack
Output: ABC*+DE*F+G*+
47. Convert the following infix
expressions into postfix expressions
A$B*C-D+E/F/(G+H)
(A+B)*(C$(D-E)+F)-G
(A+B)*(C-D)
A-B/(C*D$E)
((A+B)*C-(D-E))$(F+G)
A+B*C+(D*E+F)*G
X+6*(Y+Z)^3
(((A-(B+C))*D)$(E+F)
A+(((B-C)*(D-E)+F)/G)$(H-J)
48. Algorithm (for Infix to Postfix
Conversion) Simple
Initialize a Stack for operators, output list
Split the input into a list of symbols.
for each symbol (left to right):
if it is operand: add to output
if it is '(': push onto Stack
if it is ')': pop & add till '('
if it has '+-*/':
while peek has precedence ≥ it:
pop & add
push onto Stack
pop and add the rest of the Stack.
49. Algorithm (for Infix to Postfix
Conversion) Most Efficient
stk = empty stack
symb = Read the first character
while(!end of string)
{
if(symb is an operand)
add symb to the postfix string
else
{
while((!empty stack()) && prced(stacktop(), symb))
{top symb = pop(stk);
add symb to the postfix string}
if(empty stack(), || symb!=‘)’)
push(stk, symb)
else
top symb = pop(stk);//end of else
}
symb = get the next char
}//end of while
While(!empty stack())
{top symb=pop(stk);
Add top symb to the postfix string}
50. Evaluation of Expression
Computer evaluates an expression given in infix notation by
converting it into postfix notation.
Stack is used to perform this operation
Following steps are taken to evaluate a postfix expression:
Expression is scanned from left to right until the end of the
expression
When an operand is encountered, it is pushed into stack
When an operator is encountered, then
Top two operands of stack are removed
Arithmetic operation is performed
Computed result is pushed back to the stack
When end of the expression is reached, the top value from the
stack is picked. It is the computed value of the expression
51. Example (Evaluation of Expression)
Postfix Expression
A B C * + D E * F + G * +
A=5, B=6, C=9, D=2, E=4, F=8, G=3
2. Scanned from left to right. In first,
second and third iteration, the value
of A, B and C are pushed into the
stack. Thus the stack will be: 9
6
5
Stack
52. Example (Evaluation of Expression)
Postfix Expression
A B C * + D E * F + G * +
A=5, B=6, C=9, D=2, E=4, F=8, G=3
2. In fourth iteration, the operator ‘*’ is
read. So the two values ‘9’ and ‘6’
are popped from the stack and
multiplication is perform. i.e 9*6=54.
The computed value pushed back to
the stack. Thus the stack will be:
54
5
Stack
53. Example (Evaluation of Expression)
Postfix Expression
A B C * + D E * F + G * +
A=5, B=6, C=9, D=2, E=4, F=8, G=3
3. In fifth iteration, the operator ‘+’ is
read. So the two values ‘54’ and ‘5’
are popped from the stack and
addition is perform. i.e 54+5=59.
The computed value pushed back to
the stack. Thus the stack will be:
59
Stack
54. Example (Evaluation of Expression)
Postfix Expression
A B C * + D E * F + G * +
A=5, B=6, C=9, D=2, E=4, F=8, G=3
4. In sixth and seventh iteration, the
value of D and E are pushed into the
stack. Thus the stack will be:
4
2
59
Stack
55. Example (Evaluation of Expression)
Postfix Expression
A B C * + D E * F + G * +
A=5, B=6, C=9, D=2, E=4, F=8, G=3
5. In eighth iteration, the operator ‘*’ is
read. So the two values ‘4’ and ‘2’
are popped from the stack and
multiplication is perform. i.e 2*4=8.
The computed value pushed back to
the stack. Thus the stack will be:
8
59
Stack
56. Example (Evaluation of Expression)
Postfix Expression
A B C * + D E * F + G * +
A=5, B=6, C=9, D=2, E=4, F=8, G=3
6. In ninth iteration, the value of F is
pushed into the stack. Thus the stack
will be:
8
8
59
Stack
57. Example (Evaluation of Expression)
Postfix Expression
A B C * + D E * F + G * +
A=5, B=6, C=9, D=2, E=4, F=8, G=3
7. In tenth iteration, the operator ‘+’ is
read. So the two values ‘8’ and ‘8’
are popped from the stack and
addition is perform. i.e 8+8=16. The
computed value pushed back to the
stack. Thus the stack will be:
16
59
Stack
58. Example (Evaluation of Expression)
Postfix Expression
A B C * + D E * F + G * +
A=5, B=6, C=9, D=2, E=4, F=8, G=3
8. In eleventh iteration, the value of G
is pushed into the stack. Thus the
stack will be:
3
16
59
Stack
59. Example (Evaluation of Expression)
Postfix Expression
A B C * + D E * F + G * +
A=5, B=6, C=9, D=2, E=4, F=8, G=3
9. In twelve iteration, the operator ‘*’ is
read. So the two values ‘3’ and ‘16’
are popped from the stack and
multiplication is perform. i.e
3*16=48. The computed value
pushed back to the stack. Thus the
stack will be:
48
59
Stack
60. Example (Evaluation of Expression)
Postfix Expression
A B C * + D E * F + G * +
A=5, B=6, C=9, D=2, E=4, F=8, G=3
10. In thirteen iteration, the operator ‘+’
is read. So the two values ‘48’ and
‘59’ are popped from the stack and
addition is perform. i.e 48+59=107.
The computed value pushed back to
the stack. Thus the stack will be:
107
Stack
61. Example (Evaluation of Expression)
A + B * C + ( D * E + F ) * G
A=5, B=6, C=9, D=2, E=4, F=8, G=3
= 5 + 6 * 9 + (2 * 4 + 8) * 3
= 5 + 54 + (8 + 8) * 3
= 59 + 16 * 3
= 59 + 48
=107
62. Algorithm (for evaluating the Postfix)
stk = empty stack
symb = get the first value
while(!end of string)
{
if(symb is an operand)
Push(stk, symb);
else
{
operand 2 = pop(stk());
operand 1= pop(stk());
result = apply symb to operand 1 && operand 2
push(stk, result);
} //end of else
symb = get the next value
}// end of while
return(pop(stk));
63. Infix to Prefix
For Infix to Prefix conversion
Read the input string from right to left.
OR
Convert Infix to Postfix, then reverse the
output string
Do it Yourself????????
64. Prefix to Postfix Conversion
if(symb is an operand)
push(stk, symb);
else{
operand1 = pop(stk);
operand2 = pop(stk);
strcat(operand1, operand2, symb);
push(operand);
}
return(pop(stk));