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

Stack

The document provides an overview of the stack data structure, emphasizing its LIFO (Last In First Out) principle and basic operations such as push, pop, and peek. It discusses stack implementation using arrays and linked lists, detailing the steps for each operation and their time complexities. Additionally, it covers the application of stacks in evaluating arithmetic expressions, including the conversion of infix notation to prefix and postfix notations.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Stack

The document provides an overview of the stack data structure, emphasizing its LIFO (Last In First Out) principle and basic operations such as push, pop, and peek. It discusses stack implementation using arrays and linked lists, detailing the steps for each operation and their time complexities. Additionally, it covers the application of stacks in evaluating arithmetic expressions, including the conversion of infix notation to prefix and postfix notations.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 61

STACK

STACK
 The stack data structure is a linear data structure
that accompanies a principle known as LIFO (Last In
First Out) or FILO (First In Last Out).
 Real-life examples of a stack are a deck of cards, piles
of books, piles of money, and many more.
STACK
 This example allows you to perform operations from
one end only, like when you insert and remove new
books from the top of the stack. It means insertion and
deletion in the stack data structure can be done only
from the top of the stack. You can access only the top
of the stack at any given point in time.
 Inserting a new element in the stack is termed a push
operation.
 Removing or deleting elements from the stack is termed
pop operation.
STACK REPRESENTATION
WORKING OF STACK
 Now, assume that you have a stack of books.
 You can only see the top, i.e., the top-most book,
namely 40, which is kept top of the stack.
 If you want to insert a new book first, namely 50,
you must update the top and then insert a new
text.
 And if you want to access any other book other
than the topmost book that is 40, you first
remove the topmost book from the stack, and
then the top will point to the next topmost book
WORKING OF STACK
 After working on the representation of stacks in
data structures, you will see some basic
operations performed on the stacks in data
structures.
WORKING OF STACK
BASIC OPERATIONS ON STACK:PUSH
 Push Operation
 Push operation involves inserting new elements in
the stack. Since you have only one end to insert a
unique element on top of the stack, it inserts the new
element at the top of the stack.
BASIC OPERATIONS ON STACK:PUSH
 Push operation includes various steps, which are
as follows :
 Step 1: First, check whether or not the stack is
full
 Step 2: If the stack is complete, then exit

 Step 3: If not, increment the top by one

 Step 4: Insert a new element where the top is


pointing
 Step 5: Success
BASIC OPERATIONS ON STACK:PUSH
 begin
 if top = n then stack full
 top = top + 1
 stack (top) : = item;
 end
BASIC OPERATIONS ON STACK:POP
 Pop Operation
 Pop operation refers to removing the element from
the stack again since you have only one end to do all
top of the stack. So removing an element from the top
of the stack is termed pop operation.
BASIC OPERATIONS ON STACK:POP
 Step 1: First, check whether or not the stack is
empty
 Step 2: If the stack is empty, then exit

 Step 3: If not, access the topmost data element

 Step 4: Decrement the top by one

 Step 5: Success
BASIC OPERATIONS ON STACK:POP
 begin
 if top = 0 then stack empty;
 item := stack(top);
 top = top - 1;
 end;
BASIC OPERATIONS ON STACK:PEEK
 Peek Operation
 Peek operation refers to retrieving the topmost
element in the stack without removing it from the
collections of data elements.

Begin
if top = -1 then stack empty
item = stack[top]
return item
End
BASIC OPERATIONS ON STACK:ISFULL()
 isFull()
 isFull function is used to check whether or not a
stack is empty.

begin
if
top equals to maxsize
return true
else
return false
else if
end
BASIC OPERATIONS ON STACK:ISEMPTY
 isEmpty()

 Empty function is used to check whether or not a


stack is empty.

begin
if
top less than 1
return true
else
return false
else if
end
IMPLEMENTATION OF STACK
 You can perform the implementation of stacks in
data structures using two data structures that
are an array and a linked list.
1. Array: In array implementation, the stack is
formed using an array. All the operations are
performed using arrays. You will see how all
operations can be implemented on the stack in
data structures using an arraydata structure.
IMPLEMENTATION OF STACK USING ARRAY
IMPLEMENTATION OF STACK USING ARRAY
IMPLEMENTATION OF STACK USING ARRAY
IMPLEMENTATION OF STACK USING ARRAY
IMPLEMENTATION OF STACK USING ARRAY
IMPLEMENTATION OF STACK USING ARRAY
IMPLEMENTATION OF STACK USING ARRAY
IMPLEMENTATION OF STACK USING ARRAY
IMPLEMENTATION OF STACK USING ARRAY
IMPLEMENTATION OF STACK
2. Linked-List: Every new element is inserted as a
top element in the linked list implementation of
stacks in data structures. That means every
newly inserted element is pointed to the top.
Whenever you want to remove an element from
the stack, remove the node indicated by the top,
by moving the top to its previous node in the
list.
IMPLEMENTATION OF STACK USING
LINKED LIST
IMPLEMENTATION OF STACK USING
LINKED LIST

 isEmpty function is used to check whether or not a stack


is empty.

int isEmpty()
{
if(top==NULL)
return 1;
else
return 0;
}
IMPLEMENTATION OF STACK USING
LINKED LIST

Peek operation refers to retrieving the topmost element


in the stack without removing it from the collections of
data elements.
int peek()
{
if(isEmpty())
{
printf("Stack underflow...");
exit(1);
}
return top->data;
}
IMPLEMENTATION OF STACK USING
LINKED LIST

 In linked list implementation of stack, the


nodes are maintained non-contiguously in the
memory. Each node contains a pointer
to its immediate successor node in the stack. Stack
is said to be overflown if the space left in the
memory heap is not enough to create a node.
 The top most node in the stack always contains
null in its address field. Lets discuss the way in
which, each operation is performed in linked list
implementation of stack.
IMPLEMENTATION OF STACK USING
LINKED LIST

Adding a node to the stack (Push operation)

Adding a node to the stack is referred to


as push operation. Pushing an element to a stack
in linked list implementation is different from that
of an array implementation. In order to push an
element onto the stack, the following steps are
involved.
IMPLEMENTATION OF STACK USING
LINKED LIST

1. Create a node first and allocate memory to it.


2. If the list is empty then the item is to be pushed
as the start node of the list. This includes
assigning value to the data part of the node and
assign null to the address part of the node.
3. If there are some nodes in the list already, then
we have to add the new element in the
beginning of the list (to not violate the property
of the stack). For this purpose, assign the
address of the starting element to the address
field of the new node and make the new node,
the starting node of the list.
Time Complexity : o(1)
IMPLEMENTATION OF STACK USING
LINKED LIST
IMPLEMENTATION OF STACK USING
LINKED LIST

Deleting a node from the stack (POP


operation)
 Deleting a node from the top of stack is referred
to as pop operation. Deleting a node from the
linked list implementation of stack is different
from that in the array implementation. In order
to pop an element from the stack, we need to
follow the following steps :
IMPLEMENTATION OF STACK USING
LINKED LIST

Deleting a node from the stack (POP


operation)
 Check for the underflow condition: The
underflow condition occurs when we try to pop from
an already empty stack. The stack will be empty if
the head pointer of the list points to null.
 Adjust the head pointer accordingly: In stack,
the elements are popped only from one end, therefore,
the value stored in the head pointer must be deleted
and the node must be freed. The next node of the
head node now becomes the head node.

Time Complexity : o(n)


IMPLEMENTATION OF STACK USING
LINKED LIST

Display the nodes (Traversing)


 Displaying all the nodes of a stack needs
traversing all the nodes of the linked list
organized in the form of stack. For this purpose,
we need to follow the following steps.
 Copy the head pointer into a temporary pointer.
 Move the temporary pointer through all the nodes of
the list and print the value field attached to every
node.
 Time Complexity : o(n)
IMPLEMENTATION OF STACK USING
LINKED LIST
IMPLEMENTATION OF STACK USING
LINKED LIST
IMPLEMENTATION OF STACK USING
LINKED LIST
IMPLEMENTATION OF STACK USING
LINKED LIST
IMPLEMENTATION OF STACK USING
LINKED LIST
IMPLEMENTATION OF STACK USING
LINKED LIST
IMPLEMENTATION OF STACK USING
LINKED LIST
IMPLEMENTATION OF STACK USING
LINKED LIST
STACK APPLICATION-ARITHMETIC
EXPRESSION EVALUATION

 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)
STACK APPLICATION-ARITHMETIC
EXPRESSION EVALUATION

 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, /division Left to right Highest followed by +


addition and -
subtraction

+ addition, - subtraction Left to right lowest


STACK APPLICATION-ARITHMETIC
EXPRESSION EVALUATION

Evaluation of Arithmetic Expression requires two


steps:
 First, convert the given expression into special
notation.
 Evaluate the expression in this new notation.
STACK APPLICATION-ARITHMETIC
EXPRESSION EVALUATION

Notations for Arithmetic Expression


There are three notations to represent an
arithmetic expression:
 Infix Notation

 Prefix Notation

 Postfix Notation
STACK APPLICATION-ARITHMETIC
EXPRESSION EVALUATION

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.
STACK APPLICATION-ARITHMETIC
EXPRESSION EVALUATION

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.
STACK APPLICATION-ARITHMETIC
EXPRESSION EVALUATION

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.
STACK APPLICATION-ARITHMETIC
EXPRESSION EVALUATION

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-+
ALGORITHM TO CONVERT INFIX TO
POSTFIX EXPRESSION
ALGORITHM TO CONVERT INFIX TO POSTFIX
EXPRESSION
ALGORITHM TO CONVERT INFIX TO POSTFIX
EXPRESSION
ALGORITHM TO CONVERT INFIX TO POSTFIX
EXPRESSION
ALGORITHM TO CONVERT INFIX TO PREFIX
EXPRESSION
ALGORITHM TO CONVERT INFIX TO PREFIX
EXPRESSION
ALGORITHM TO CONVERT INFIX TO PREFIX
EXPRESSION
ALGORITHM TO CONVERT INFIX TO PREFIX
EXPRESSION

You might also like