0% found this document useful (0 votes)
12 views

UNIT-3 Stack

The document discusses abstract data types and describes stacks as a linear data structure that follows the LIFO principle. It provides details on stack operations like push, pop and peek, and applications of stacks such as evaluating arithmetic expressions and backtracking.

Uploaded by

ritik111819
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

UNIT-3 Stack

The document discusses abstract data types and describes stacks as a linear data structure that follows the LIFO principle. It provides details on stack operations like push, pop and peek, and applications of stacks such as evaluating arithmetic expressions and backtracking.

Uploaded by

ritik111819
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 23

STACK

ABSTRACT DATA TYPES

 An abstract data type is an abstraction of a data structure that provides only the
interface to which the data structure must adhere. The interface does not give any
specific details about something should be implemented or in what programming
language.
 In other words, we can say that abstract data types are the entities that are definitions of
data and operations but do not have implementation details. In this case, we know the
data that we are storing and the operations that can be performed on the data, but we
don't know about the implementation details. The reason for not having implementation
details is that every programming language has a different implementation strategy for
example; a C data structure is implemented using structures while a C++ data structure
is implemented using objects and classes.
STACK
 A Stack is a linear data structure that follows the LIFO (Last-In-First-Out) principle.
Stack has one end, whereas the Queue has two ends (front and rear).
 It contains only one pointer top pointer pointing to the topmost element of the stack.
Whenever an element is added in the stack, it is added on the top of the stack, and the
element can be deleted only from the stack.
 In other words, a stack can be defined as a container in which insertion and deletion can
be done from the one end known as the top of the stack.
 Some key points related to stack
• It is called as stack because it behaves like a real-world stack, piles of books, etc.
• A Stack is an abstract data type with a pre-defined capacity, which means that it can store
the elements of a limited size.
• It is a data structure that follows some order to insert and delete the elements, and that order
can be LIFO or FILO.
Working of Stack
 Stack works on the LIFO pattern. As we can observe in the below figure there are five
memory blocks in the stack; therefore, the size of the stack is 5.
 Suppose we want to store the elements in a stack and let's assume that stack is empty.
We have taken the stack of size 5 as shown below in which we are pushing the
elements one by one until the stack becomes full.
Since our stack is full as the size of the
stack is 5. In the above cases, we can
observe that it goes from the top to the
bottom when we were entering the new
element in the stack. The stack gets
filled up from the bottom to the top.
Standard Stack Operations
 The following are some common operations implemented on the stack:
• push(): When we insert an element in a stack then the operation is known as a push. If the
stack is full then the overflow condition occurs.
• pop(): When we delete an element from the stack, the operation is known as a pop. If the
stack is empty means that no element exists in the stack, this state is known as an
underflow state.
• isEmpty(): It determines whether the stack is empty or not.
• isFull(): It determines whether the stack is full or not.'
• peek(): It returns the element at the given position.
• count(): It returns the total number of elements available in a stack.
• change(): It changes the element at the given position.
• display(): It prints all the elements available in the stack.
Implementing Stack in C
Applications of Stack

 Following is the various Applications of Stack in Data Structure:


• Evaluation of Arithmetic Expressions
• Backtracking
• Delimiter Checking
• Reverse a Data
1.A Stack can be used for evaluating expressions consisting of
• Processing Function Calls operands and operators.
2.Stacks can be used for Backtracking, i.e., to check parenthesis
matching in an expression.
3.It can also be used to convert one form of expression to
another form.
4.It can be used for systematic Memory Management.
1. Evaluation of Arithmetic Expressions
 A stack is a very effective data structure for evaluating arithmetic expressions in
programming languages. An arithmetic expression consists of operands and operators.
 In addition to operands and operators, the arithmetic expression may also include
parenthesis like "left parenthesis" and "right parenthesis".
 Example: A + (B - C)
 To evaluate the expressions, one needs to be aware of the standard precedence rules for
arithmetic expression. The precedence rules for the five basic arithmetic operators are:

Operators Associativity Precedence


^ exponentiation Right to left Highest followed by
*Multiplication
and /division
*Multiplication, Left to right Highest followed by
/division + addition and -
subtraction
+ addition, - Left to right lowest
subtraction
Evaluation of Arithmetic Expression requires two steps:

• First, convert the given expression into special notation.


• Evaluate the expression in this new notation.
 Notations for Arithmetic Expression
 There are three notations to represent an arithmetic expression:
• Infix Notation
• Prefix Notation
• Postfix Notation
Infix Notation

 The infix notation is a convenient way of writing an expression in which each operator
is placed between the operands. Infix expressions can be parenthesized or
unparenthesized depending upon the problem requirement.
 Example: A + B, (C - D) etc.
 All these expressions are in infix notation because the operator comes between the
operands.
Prefix Notation

 The prefix notation places the operator before the operands. This notation was
introduced by the Polish mathematician and hence often referred to as polish notation.
 Example: + A B, -CD etc.
 All these expressions are in prefix notation because the operator comes before the
operands.
Postfix Notation

 The postfix notation places the operator after the operands. This notation is just the
reverse of Polish notation and also known as Reverse Polish notation.
 Example: AB +, CD+, etc.
 All these expressions are in postfix notation because the operator comes after the
operands.
Conversion of Arithmetic Expression into
various Notations:

Infix Notation Prefix Notation Postfix Notation

A*B *AB AB*

(A+B)/C /+ ABC AB+C/

(A*B) + (D-C) +*AB - DC AB*DC-+


In the above example, the only change from
the postfix expression is that the operator is
placed before the operands rather than
between the operands.
Evaluating Postfix expression:

 Stack is the ideal data structure to evaluate the postfix expression because the top
element is always the most recent operand. The next element on the Stack is the second
most recent operand to be operated on.
 Before evaluating the postfix expression, the following conditions must be checked. If
any one of the conditions fails, the postfix expression is invalid.
• When an operator encounters the scanning process, the Stack must contain a pair of
operands or intermediate results previously calculated.
• When an expression has been completely evaluated, the Stack must contain exactly one
value.
Example:
 Now let us consider the following infix expression 2 * (4+3) - 5.
 Its equivalent postfix expression is 2 4 3 + * 5.
 The following step illustrates how this postfix expression is evaluated.
2. Backtracking

 One of the algorithm design techniques is backtracking. For that aim, we dig into one
path; if that path is inefficient, we return to the previous state and explore alternative
options. We must save the previous state in order to return from the current state.
Stack is required for this purpose. A famous example is N-queens problems.
3. Delimiter Checking

 The common application of Stack is delimiter checking, i.e., parsing that involves
analyzing a source program syntactically.
 It is also called parenthesis checking. When the compiler translates a source program
written in some programming language such as C, C++ to a machine language, it parses
the program into multiple individual parts such as variable names, keywords, etc.
 By scanning from left to right. The main problem encountered while translating is the
unmatched delimiters.
 We make use of different types of delimiters include the parenthesis checking (,), curly
braces {,} and square brackets [,], and common delimiters /* and */.
 Every opening delimiter must match a closing delimiter, i.e., every opening parenthesis
should be followed by a matching closing parenthesis.
Valid Delimiter Invalid Delimiter

While ( i > 0) While ( i >

/* Data Structure */ /* Data Structure

{ ( a + b) - c } { ( a + b) - c

To perform a delimiter checking, the compiler makes use of a


stack. When a compiler translates a source program, it reads the
characters one at a time, and if it finds an opening delimiter it
places it on a stack. When a closing delimiter is found, it pops
up the opening delimiter from the top of the Stack and matches
it with the closing delimiter.
Example:
4. Reverse a Data

 To reverse a given set of data, we need to reorder the data so that the first and last
elements are exchanged, the second and second last element are exchanged, and so on
for all other elements.
 Example: Suppose we have a string Welcome, then on reversing it would be Emoclew.
5. Processing Function Calls:
 Stack plays an important role in programs that call several functions in succession.
Suppose we have a program containing three functions: A, B, and C. function A invokes
function B, which invokes the function C.

When we invoke function A, which contains a call to function B, then its processing will not
be completed until function B has completed its execution and returned. Similarly for
function B and C. So we observe that function A will only be completed after function B is
completed and function B will only be completed after function C is completed. Therefore,
function A is first to be started and last to be completed. To conclude, the above function
activity matches the last in first out behavior and can easily be handled using Stack.

You might also like