SlideShare a Scribd company logo
Stacks
MRS.SOWMYA JYOTHI, SDMCBM, MANGALORE
Reference: Data Structures with C by Seymour Lipschutz, Schaum’s Outlines
Series.
Stacks
INTRODUCTION
• The linear lists and linear arrays are used to insert and delete elements at
any place. There are certain frequent situations when one wants to restrict
insertion and deletion so that they can take place only at the beginning or
end of the list, not in the middle.
• Two of the data structures that are useful in such situations are stacks and
queues.
• A stack is a linear structure in which items may be added or removed only
at one end. It is called last-in first-out (LIFO).
• A queue is a linear list in which items may be added only at one end and
items may be removed only at the other end. It is called first-in first-out
(FIFO) list.
STACKS
• A stack is a list of elements in which an element may
be inserted or deleted only a one end, called TOP of
the stack.
• The elements are removed in reverse order of that in
which they were inserted into the stack.
Basic operations:
a) Push() is the term used to insert/add an
element into a stack.
b) Pop() is the term used to delete/remove an
element from a stack.
•Other names for stacks are piles and push-down
lists.
•There are two ways to represent Stack in
memory.
1. One is using array and
2. Other is using linked list.
Example:
• Suppose the following 6 elements are pushed, in order, onto
an empty stack:
• AAA, BBB, CCC, DDD, EEE, FFF
ARRAY REPRESENTATION OF STACKS
• Stacks are represented in the computer by a linear array.
• In the following algorithms/procedures of pushing and
popping an item from the stacks,
1. a linear array STACK,
2. a variable TOP which contains the location of the top
element of the stack;
3. and a variable MAXSTAK which gives the maximum
number of elements that can be held by the stack.
OVERFLOW AND UNDERFLOW
1. The condition TOP=0 or TOP=NULL will indicate the
stack is empty and we say UNDERFLOW.
2. The condition TOP=MAXSTK will indicate the stack
is full and we say OVERFLOW.
The operation of adding an element into the stack is called
pushing and
the operation of removing an element from the stack is
called popping.
Algorithm 6.1: PUSH(STACK, TOP, MAXSTAK, ITEM):
This procedure pushes an ITEM onto a stack.
1. [STACK already filled?]
If TOP=MAXSTAK, then: Print: OVERFLOW, and Return
2. Set TOP:=TOP+1 [Increase TOP by 1]
3. Set STACK [TOP]:=ITEM [Insert ITEM in new TOP position]
4. Return
Example:
To simulate the operation PUSH(STACK, WWW)
1. Since TOP=3, control is transferred to
Step 2.
2. TOP=3+1=4
3. STACK[TOP]=STACK[4]=WWW
4. Return
Algorithm 6.2: POP(STACK, TOP, ITEM)
This procedure deletes the top element of STACK and assigns
it to the variable ITEM.
1. [STACK has an item to be removed? Check for empty stack]
If TOP=-1, then: Print: UNDERFLOW, and Return
2. Set ITEM:=STACK[TOP] [Assigns TOP element to ITEM]
3. Set TOP=TOP-1 [Decrease TOP by 1.]
4. Return
To simulate the operation POP(STACK, ITEM)
1. Since TOP=3, control is transferred to Step 2.
2. ITEM=ZZZ
3. TOP=3-1=2
4. Return
LINKED REPRESENTATION OF STACKS
• The linked representation of stack is implemented using singly
linked list.
1. The INFO fields of the nodes hold the elements of the stack and
2. The LINK fields hold the pointers to the neighboring element in
the stack.
3. The START pointer of the linked list behaves as TOP pointer
variable of the stack and
4. The NULL pointer in the last node signals the end of the stack.
• A PUSH operation is accomplished by inserting a node into the
start of the STACK and a POP operation is undertaken by
deleting the node pointed to by the START pointer.
• The array representation of stack maintains a variable MAXSTK
which gives the maximum number of elements that can be held
by the stack.
• If the number of elements in the stack becomes equal to the
value of variable MAXSTK, it is the condition of OVERFLOW.
• The linked representation of stacks is free from this
requirement.
• There is no limitation on the capacity of the linked stack
hence it can support as many PUSH operations as free
storage list (the AVAIL list) can support.
1. However, the condition TOP=NULL may be retained in the
POP procedure to prevent deletion from an empty stack
and
2. AVAIL=NULL to check for the available space in the free
storage list.
Algorithm 6.3: PUSH_LINKSTACK(INFO, LINK, TOP, AVAIL, ITEM)
This procedure pushes an ITEM onto a stack.
1 IF AVAIL=NULL, then Write: OVERFLOW and Exit [Available Space?]
2. [Remove first node from AVAIL list]
Set NEW:=AVAIL and AVAIL:=LINK[AVAIL]
3. Set INFO[NEW]:=ITEM [ Copies the ITEM into new node]
4. Set LINK[NEW]:=TOP [New node points to the original top node in the stack]
5. Set TOP:=NEW [Reset TOP to point to the new node at the top of the stack]
6. Exit
Algorithm 6.4: POP_LINKSTACK(INFO, LINK, TOP, AVAIL, ITEM)
This procedure deletes the top element of STACK and assigns it to the
variable ITEM.
1. [STACK has an item to be removed? Check for empty stack]
If TOP=NULL , then: Write: UNDERFLOW, and Exit
2. Set ITEM:=INFO[TOP]. [Copies the top element of stack into ITEM]
3. Set TEMP:=TOP and TOP:=LINK[TOP]
[Remember the old value of TOP in TEMP and reset TOP to point to the next
element]
4. [Return deleted node to the AVAIL list]
Set LINK [TEMP]:=AVAIL and AVAIL:=TEMP
5. Exit
Example:
Consider the linked list given in
fig (a) and perform the following operations:
I) Push BBB ii) Pop iii) Pop iv) Push MMM
I) Push BBB ii) Pop iii) Pop iv) Push MMM
APPLICATION OF STACKS:
Quick Sort
• Let A be a list of n data items. Quick sort is an algorithm of the divide-and-
conquer type. That is, the problem of sorting a set is reduced to the
problem of sorting two smaller sets.
RECURSION
• Suppose P is a procedure containing either a Call statement to itself or a
Call statement to a second procedure that may eventually result in a call
statement back to the original procedure P. then P is called recursive
procedure. The program contains a recursive procedure should not run
infinitely. Hence, a recursive procedure/function must have the following
properties:
1) There must be certain criteria, called base criteria, for which the
procedure does not call itself.
2) Each time when the procedure is called, it must be closer to that criterion.
RECURSION
• Suppose P is a procedure containing either a Call statement to itself or a
Call statement to a second procedure that may eventually result in a
call statement back to the original procedure P. then P is called
recursive procedure. The program contains a recursive procedure
should not run infinitely.
• Hence, a recursive procedure/function must have the following
properties:
1) There must be certain criteria, called base criteria, for which
the procedure does not call itself.
2) Each time when the procedure is called, it must be closer to
that criterion.
Factorial Function
a) If N = 0, then N! = 1.
b) If N > 0, then N! = N*(N-1)!
Example:
5! = 5 * 4!
= 5 * 4 * 3!
= 5 * 4 * 3 * 2!
= 5 * 4 * 3 * 2 * 1!
= 5 * 4 * 3 * 2 * 1* 0!
= 5 * 4 * 3 * 2 * 1 * 1
= 120
Procedure 6.9: FACTORIAL (FACT, N)
This recursive procedure calculates N! and
returns the value in the variable FACT
1. If N = 0, then : Set FACT := 1 and
Return
2. Call FACTORIAL(FACT, N-1)
3. Set FACT := N*FACT.
Fibonacci Sequence
• The Fibonacci sequence is 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55. F0 =0 and
F1= 1.
• Each succeeding term is the sum of two preceding terms.
a) If n = 1 then Fn = 0.
b) If n = 2 then Fn = 1
c) If n > 2, then Fn = Fn-1 + Fn-2
Procedure 6.10: FIBONACCI(FIB, N)
•This recursive procedure calculates FN and returns
the value in the parameter FIB.
1. If N = 0 or N= 1 then: Set FIB:=N and Return
2. Call FIBNOCCI(FIBA, N-2)
3. Call FIBNOCCI(FIBB, N-1)
4. Set FIB := FIBA + FIBB
5. Return
ARITHMETIC EXPRESSIONS
• INFIX , POSTFIX AND PREFIX NOTATIONS:
• Let Q be an arithmetic expression involving constants and
operations.
• Besides operands and operators, Q may also contain left and right
parentheses.
• The operators in Q can consist of exponentiations ( ) multiplications
(*), divisions (/) additions (+) and subtractions (-). They have different
levels of precedence.
• Highest : Exponentiation ( )
• Next Highest : Multiplication (*) and division (/)
• Lowest : Addition (+) and subtraction (-)
Highest : Exponentiation ( )
Next Highest : Multiplication (*) and division (/)
Lowest : Addition (+) and subtraction (-)
1. Generally, expressions can be written in which the operator is
written between the operands. This is called infix notation.
For example: A + B C - D
2. Polish notation, named after the Polish mathematician Jan
Lukasiewicz, refers to the notation in which the operator symbol is
placed before its two operands. This notation frequently called prefix
notation.
For example, + A B - C D
3. Reverse Polish notation refers to the analogous notation in which
the operator symbol is placed after two operands. This notation
frequently called postfix (or suffix) notation
For example: A B + C D -
• Computers “prefer” postfix notation in which the operator is written
to the right of two operands.
• The computer usually evaluates an arithmetic
expression written in infix notation in two steps.
1. First, it converts the expression to postfix notation
and
2. Then it evaluates the postfix expression.
• In each step stack is used as main tool.
Evaluation of a Postfix Expression:
•Suppose P is an arithmetic expression written in
postfix notation.
•The following algorithm uses STACK to evaluate P.
Example:
• Consider P: 5, 6, 2, +, *, 12, 4, /, −
whose equivalent infix expression is
• Q: 5 * ( 6 + 2 ) – 12 / 4
• The elements of P are labelled from left to
right for ease of reference as follows:
•P: 5, 6, 2, +, *, 12, 4, /, −, )
(1) (2) (3) (4) (5) (6) (7) (8) (9) (10)
P: 5, 6, 2, +, *, 12, 4, /, −
P: 5, 6, 2, +, *, 12, 4, /, −, )
(1) (2) (3) (4) (5) (6) (7) (8) (9) (10)
Transforming Infix Expression into Postfix Expression:
• The following algorithm transforms the infix expression Q
into its equivalent postfix expression P.
• It uses a stack to temporary hold the operators and left
parenthesis.
• The postfix expression will be constructed from left to right
using operands from Q and operators popped from STACK.
Stacks IN DATA STRUCTURES
Stacks IN DATA STRUCTURES
Stacks IN DATA STRUCTURES
Stacks IN DATA STRUCTURES
Stacks IN DATA STRUCTURES

More Related Content

What's hot

Binary Tree Traversal
Binary Tree TraversalBinary Tree Traversal
Binary Tree Traversal
Dhrumil Panchal
 
Binary search tree(bst)
Binary search tree(bst)Binary search tree(bst)
Binary search tree(bst)
Hossain Md Shakhawat
 
Linked list
Linked listLinked list
Linked list
KalaivaniKS1
 
Graph traversals in Data Structures
Graph traversals in Data StructuresGraph traversals in Data Structures
Graph traversals in Data Structures
Anandhasilambarasan D
 
Stack
StackStack
Doubly Linked List
Doubly Linked ListDoubly Linked List
Doubly Linked List
Ninad Mankar
 
Queues
QueuesQueues
Binary tree
Binary treeBinary tree
Binary tree
Rajendran
 
Data Structures - Lecture 9 [Stack & Queue using Linked List]
 Data Structures - Lecture 9 [Stack & Queue using Linked List] Data Structures - Lecture 9 [Stack & Queue using Linked List]
Data Structures - Lecture 9 [Stack & Queue using Linked List]
Muhammad Hammad Waseem
 
Binary Tree in Data Structure
Binary Tree in Data StructureBinary Tree in Data Structure
Binary Tree in Data Structure
Meghaj Mallick
 
Queue ppt
Queue pptQueue ppt
Queue ppt
SouravKumar328
 
heap Sort Algorithm
heap  Sort Algorithmheap  Sort Algorithm
heap Sort Algorithm
Lemia Algmri
 
sorting and its types
sorting and its typessorting and its types
sorting and its types
SIVASHANKARIRAJAN
 
Arrays
ArraysArrays
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search Tree
sagar yadav
 
Linked list
Linked listLinked list
Linked list
akshat360
 
Queue
QueueQueue
Tree Traversal
Tree TraversalTree Traversal
Tree Traversal
Md. Israil Fakir
 
Quick sort
Quick sortQuick sort
Quick sort
Dhruv Sabalpara
 
sparse matrix in data structure
sparse matrix in data structuresparse matrix in data structure
sparse matrix in data structure
MAHALAKSHMI P
 

What's hot (20)

Binary Tree Traversal
Binary Tree TraversalBinary Tree Traversal
Binary Tree Traversal
 
Binary search tree(bst)
Binary search tree(bst)Binary search tree(bst)
Binary search tree(bst)
 
Linked list
Linked listLinked list
Linked list
 
Graph traversals in Data Structures
Graph traversals in Data StructuresGraph traversals in Data Structures
Graph traversals in Data Structures
 
Stack
StackStack
Stack
 
Doubly Linked List
Doubly Linked ListDoubly Linked List
Doubly Linked List
 
Queues
QueuesQueues
Queues
 
Binary tree
Binary treeBinary tree
Binary tree
 
Data Structures - Lecture 9 [Stack & Queue using Linked List]
 Data Structures - Lecture 9 [Stack & Queue using Linked List] Data Structures - Lecture 9 [Stack & Queue using Linked List]
Data Structures - Lecture 9 [Stack & Queue using Linked List]
 
Binary Tree in Data Structure
Binary Tree in Data StructureBinary Tree in Data Structure
Binary Tree in Data Structure
 
Queue ppt
Queue pptQueue ppt
Queue ppt
 
heap Sort Algorithm
heap  Sort Algorithmheap  Sort Algorithm
heap Sort Algorithm
 
sorting and its types
sorting and its typessorting and its types
sorting and its types
 
Arrays
ArraysArrays
Arrays
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search Tree
 
Linked list
Linked listLinked list
Linked list
 
Queue
QueueQueue
Queue
 
Tree Traversal
Tree TraversalTree Traversal
Tree Traversal
 
Quick sort
Quick sortQuick sort
Quick sort
 
sparse matrix in data structure
sparse matrix in data structuresparse matrix in data structure
sparse matrix in data structure
 

Similar to Stacks IN DATA STRUCTURES

Data structure lecture7
Data structure lecture7Data structure lecture7
Data structure lecture7
Kumar
 
Stacks in c++
Stacks in c++Stacks in c++
Stacks in c++
Vineeta Garg
 
Stack_Overview_Implementation_WithVode.pptx
Stack_Overview_Implementation_WithVode.pptxStack_Overview_Implementation_WithVode.pptx
Stack_Overview_Implementation_WithVode.pptx
chandankumar364348
 
Unit 3 Stacks and Queues.pptx
Unit 3 Stacks and Queues.pptxUnit 3 Stacks and Queues.pptx
Unit 3 Stacks and Queues.pptx
Yogesh Pawar
 
Stacks,queues,linked-list
Stacks,queues,linked-listStacks,queues,linked-list
Stacks,queues,linked-list
pinakspatel
 
DS-UNIT 3 FINAL.pptx
DS-UNIT 3 FINAL.pptxDS-UNIT 3 FINAL.pptx
DS-UNIT 3 FINAL.pptx
prakashvs7
 
Unit 3 stack
Unit 3   stackUnit 3   stack
Unit 3 stack
kalyanineve
 
Stacks
StacksStacks
Stacks
sweta dargad
 
Stack in Sata Structure
Stack in Sata StructureStack in Sata Structure
Stack in Sata Structure
Muhazzab Chouhadry
 
Unit II - LINEAR DATA STRUCTURES
Unit II -  LINEAR DATA STRUCTURESUnit II -  LINEAR DATA STRUCTURES
Unit II - LINEAR DATA STRUCTURES
Usha Mahalingam
 
Stack and its operations, Queue and its operations
Stack and its operations, Queue and its operationsStack and its operations, Queue and its operations
Stack and its operations, Queue and its operations
poongothai11
 
Stack and Queue.pptx university exam preparation
Stack and Queue.pptx university exam preparationStack and Queue.pptx university exam preparation
Stack and Queue.pptx university exam preparation
RAtna29
 
DS UNIT1_STACKS.pptx
DS UNIT1_STACKS.pptxDS UNIT1_STACKS.pptx
DS UNIT1_STACKS.pptx
VeerannaKotagi1
 
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
haaamin01
 
Stack data structure
Stack data structureStack data structure
Stack data structure
rogineojerio020496
 
Bca ii dfs u-2 linklist,stack,queue
Bca ii  dfs u-2 linklist,stack,queueBca ii  dfs u-2 linklist,stack,queue
Bca ii dfs u-2 linklist,stack,queue
Rai University
 
Bsc cs ii dfs u-2 linklist,stack,queue
Bsc cs ii  dfs u-2 linklist,stack,queueBsc cs ii  dfs u-2 linklist,stack,queue
Bsc cs ii dfs u-2 linklist,stack,queue
Rai University
 
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
Soumen Santra
 
Data structure lab manual
Data structure lab manualData structure lab manual
Data structure lab manual
nikshaikh786
 
STACK AND ITS OPERATIONS IN DATA STRUCTURES.pptx
STACK AND ITS OPERATIONS IN DATA STRUCTURES.pptxSTACK AND ITS OPERATIONS IN DATA STRUCTURES.pptx
STACK AND ITS OPERATIONS IN DATA STRUCTURES.pptx
KALPANAC20
 

Similar to Stacks IN DATA STRUCTURES (20)

Data structure lecture7
Data structure lecture7Data structure lecture7
Data structure lecture7
 
Stacks in c++
Stacks in c++Stacks in c++
Stacks in c++
 
Stack_Overview_Implementation_WithVode.pptx
Stack_Overview_Implementation_WithVode.pptxStack_Overview_Implementation_WithVode.pptx
Stack_Overview_Implementation_WithVode.pptx
 
Unit 3 Stacks and Queues.pptx
Unit 3 Stacks and Queues.pptxUnit 3 Stacks and Queues.pptx
Unit 3 Stacks and Queues.pptx
 
Stacks,queues,linked-list
Stacks,queues,linked-listStacks,queues,linked-list
Stacks,queues,linked-list
 
DS-UNIT 3 FINAL.pptx
DS-UNIT 3 FINAL.pptxDS-UNIT 3 FINAL.pptx
DS-UNIT 3 FINAL.pptx
 
Unit 3 stack
Unit 3   stackUnit 3   stack
Unit 3 stack
 
Stacks
StacksStacks
Stacks
 
Stack in Sata Structure
Stack in Sata StructureStack in Sata Structure
Stack in Sata Structure
 
Unit II - LINEAR DATA STRUCTURES
Unit II -  LINEAR DATA STRUCTURESUnit II -  LINEAR DATA STRUCTURES
Unit II - LINEAR DATA STRUCTURES
 
Stack and its operations, Queue and its operations
Stack and its operations, Queue and its operationsStack and its operations, Queue and its operations
Stack and its operations, Queue and its operations
 
Stack and Queue.pptx university exam preparation
Stack and Queue.pptx university exam preparationStack and Queue.pptx university exam preparation
Stack and Queue.pptx university exam preparation
 
DS UNIT1_STACKS.pptx
DS UNIT1_STACKS.pptxDS UNIT1_STACKS.pptx
DS UNIT1_STACKS.pptx
 
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 data structure
Stack data structureStack data structure
Stack data structure
 
Bca ii dfs u-2 linklist,stack,queue
Bca ii  dfs u-2 linklist,stack,queueBca ii  dfs u-2 linklist,stack,queue
Bca ii dfs u-2 linklist,stack,queue
 
Bsc cs ii dfs u-2 linklist,stack,queue
Bsc cs ii  dfs u-2 linklist,stack,queueBsc cs ii  dfs u-2 linklist,stack,queue
Bsc cs ii dfs u-2 linklist,stack,queue
 
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
 
Data structure lab manual
Data structure lab manualData structure lab manual
Data structure lab manual
 
STACK AND ITS OPERATIONS IN DATA STRUCTURES.pptx
STACK AND ITS OPERATIONS IN DATA STRUCTURES.pptxSTACK AND ITS OPERATIONS IN DATA STRUCTURES.pptx
STACK AND ITS OPERATIONS IN DATA STRUCTURES.pptx
 

More from Sowmya Jyothi

Functions in c mrs.sowmya jyothi
Functions in c mrs.sowmya jyothiFunctions in c mrs.sowmya jyothi
Functions in c mrs.sowmya jyothi
Sowmya Jyothi
 
Strings in c mrs.sowmya jyothi
Strings in c mrs.sowmya jyothiStrings in c mrs.sowmya jyothi
Strings in c mrs.sowmya jyothi
Sowmya Jyothi
 
Arrays in c unit iii chapter 1 mrs.sowmya jyothi
Arrays in c unit iii chapter 1 mrs.sowmya jyothiArrays in c unit iii chapter 1 mrs.sowmya jyothi
Arrays in c unit iii chapter 1 mrs.sowmya jyothi
Sowmya Jyothi
 
Bca data structures linked list mrs.sowmya jyothi
Bca data structures linked list mrs.sowmya jyothiBca data structures linked list mrs.sowmya jyothi
Bca data structures linked list mrs.sowmya jyothi
Sowmya Jyothi
 
BCA DATA STRUCTURES LINEAR ARRAYS MRS.SOWMYA JYOTHI
BCA DATA STRUCTURES LINEAR ARRAYS MRS.SOWMYA JYOTHIBCA DATA STRUCTURES LINEAR ARRAYS MRS.SOWMYA JYOTHI
BCA DATA STRUCTURES LINEAR ARRAYS MRS.SOWMYA JYOTHI
Sowmya Jyothi
 
BCA DATA STRUCTURES SEARCHING AND SORTING MRS.SOWMYA JYOTHI
BCA DATA STRUCTURES SEARCHING AND SORTING MRS.SOWMYA JYOTHIBCA DATA STRUCTURES SEARCHING AND SORTING MRS.SOWMYA JYOTHI
BCA DATA STRUCTURES SEARCHING AND SORTING MRS.SOWMYA JYOTHI
Sowmya Jyothi
 
BCA DATA STRUCTURES ALGORITHMS AND PRELIMINARIES MRS SOWMYA JYOTHI
BCA DATA STRUCTURES ALGORITHMS AND PRELIMINARIES MRS SOWMYA JYOTHIBCA DATA STRUCTURES ALGORITHMS AND PRELIMINARIES MRS SOWMYA JYOTHI
BCA DATA STRUCTURES ALGORITHMS AND PRELIMINARIES MRS SOWMYA JYOTHI
Sowmya Jyothi
 
BCA DATA STRUCTURES INTRODUCTION AND OVERVIEW SOWMYA JYOTHI
BCA DATA STRUCTURES INTRODUCTION AND OVERVIEW SOWMYA JYOTHIBCA DATA STRUCTURES INTRODUCTION AND OVERVIEW SOWMYA JYOTHI
BCA DATA STRUCTURES INTRODUCTION AND OVERVIEW SOWMYA JYOTHI
Sowmya Jyothi
 
HARDWARE AND PC MAINTENANCE -THE COMPLETE PC-MRS SOWMYA JYOTHI REFERENCE-MIKE...
HARDWARE AND PC MAINTENANCE -THE COMPLETE PC-MRS SOWMYA JYOTHI REFERENCE-MIKE...HARDWARE AND PC MAINTENANCE -THE COMPLETE PC-MRS SOWMYA JYOTHI REFERENCE-MIKE...
HARDWARE AND PC MAINTENANCE -THE COMPLETE PC-MRS SOWMYA JYOTHI REFERENCE-MIKE...
Sowmya Jyothi
 
Unit II chapter 4 Loops in C
Unit II chapter 4 Loops in CUnit II chapter 4 Loops in C
Unit II chapter 4 Loops in C
Sowmya Jyothi
 
Unit ii chapter 2 Decision making and Branching in C
Unit ii chapter 2 Decision making and Branching in CUnit ii chapter 2 Decision making and Branching in C
Unit ii chapter 2 Decision making and Branching in C
Sowmya Jyothi
 
Unit ii chapter 1 operator and expressions in c
Unit ii chapter 1 operator and expressions in cUnit ii chapter 1 operator and expressions in c
Unit ii chapter 1 operator and expressions in c
Sowmya Jyothi
 
Overview of C Mrs Sowmya Jyothi
Overview of C Mrs Sowmya JyothiOverview of C Mrs Sowmya Jyothi
Overview of C Mrs Sowmya Jyothi
Sowmya Jyothi
 
NETWORK AND DATABASE CONCEPTS UNIT 1 CHAPTER 2 MRS.SOWMYA JYOTHI
NETWORK AND DATABASE CONCEPTS UNIT 1 CHAPTER 2 MRS.SOWMYA JYOTHINETWORK AND DATABASE CONCEPTS UNIT 1 CHAPTER 2 MRS.SOWMYA JYOTHI
NETWORK AND DATABASE CONCEPTS UNIT 1 CHAPTER 2 MRS.SOWMYA JYOTHI
Sowmya Jyothi
 
Introduction to computers MRS. SOWMYA JYOTHI
Introduction to computers MRS. SOWMYA JYOTHIIntroduction to computers MRS. SOWMYA JYOTHI
Introduction to computers MRS. SOWMYA JYOTHI
Sowmya Jyothi
 
Introduction to graphics
Introduction to graphicsIntroduction to graphics
Introduction to graphics
Sowmya Jyothi
 
Inter Process Communication PPT
Inter Process Communication PPTInter Process Communication PPT
Inter Process Communication PPT
Sowmya Jyothi
 
Internal representation of file chapter 4 Sowmya Jyothi
Internal representation of file chapter 4 Sowmya JyothiInternal representation of file chapter 4 Sowmya Jyothi
Internal representation of file chapter 4 Sowmya Jyothi
Sowmya Jyothi
 
Buffer cache unix ppt Mrs.Sowmya Jyothi
Buffer cache unix ppt Mrs.Sowmya JyothiBuffer cache unix ppt Mrs.Sowmya Jyothi
Buffer cache unix ppt Mrs.Sowmya Jyothi
Sowmya Jyothi
 
Introduction to the Kernel Chapter 2 Mrs.Sowmya Jyothi
Introduction to the Kernel  Chapter 2 Mrs.Sowmya JyothiIntroduction to the Kernel  Chapter 2 Mrs.Sowmya Jyothi
Introduction to the Kernel Chapter 2 Mrs.Sowmya Jyothi
Sowmya Jyothi
 

More from Sowmya Jyothi (20)

Functions in c mrs.sowmya jyothi
Functions in c mrs.sowmya jyothiFunctions in c mrs.sowmya jyothi
Functions in c mrs.sowmya jyothi
 
Strings in c mrs.sowmya jyothi
Strings in c mrs.sowmya jyothiStrings in c mrs.sowmya jyothi
Strings in c mrs.sowmya jyothi
 
Arrays in c unit iii chapter 1 mrs.sowmya jyothi
Arrays in c unit iii chapter 1 mrs.sowmya jyothiArrays in c unit iii chapter 1 mrs.sowmya jyothi
Arrays in c unit iii chapter 1 mrs.sowmya jyothi
 
Bca data structures linked list mrs.sowmya jyothi
Bca data structures linked list mrs.sowmya jyothiBca data structures linked list mrs.sowmya jyothi
Bca data structures linked list mrs.sowmya jyothi
 
BCA DATA STRUCTURES LINEAR ARRAYS MRS.SOWMYA JYOTHI
BCA DATA STRUCTURES LINEAR ARRAYS MRS.SOWMYA JYOTHIBCA DATA STRUCTURES LINEAR ARRAYS MRS.SOWMYA JYOTHI
BCA DATA STRUCTURES LINEAR ARRAYS MRS.SOWMYA JYOTHI
 
BCA DATA STRUCTURES SEARCHING AND SORTING MRS.SOWMYA JYOTHI
BCA DATA STRUCTURES SEARCHING AND SORTING MRS.SOWMYA JYOTHIBCA DATA STRUCTURES SEARCHING AND SORTING MRS.SOWMYA JYOTHI
BCA DATA STRUCTURES SEARCHING AND SORTING MRS.SOWMYA JYOTHI
 
BCA DATA STRUCTURES ALGORITHMS AND PRELIMINARIES MRS SOWMYA JYOTHI
BCA DATA STRUCTURES ALGORITHMS AND PRELIMINARIES MRS SOWMYA JYOTHIBCA DATA STRUCTURES ALGORITHMS AND PRELIMINARIES MRS SOWMYA JYOTHI
BCA DATA STRUCTURES ALGORITHMS AND PRELIMINARIES MRS SOWMYA JYOTHI
 
BCA DATA STRUCTURES INTRODUCTION AND OVERVIEW SOWMYA JYOTHI
BCA DATA STRUCTURES INTRODUCTION AND OVERVIEW SOWMYA JYOTHIBCA DATA STRUCTURES INTRODUCTION AND OVERVIEW SOWMYA JYOTHI
BCA DATA STRUCTURES INTRODUCTION AND OVERVIEW SOWMYA JYOTHI
 
HARDWARE AND PC MAINTENANCE -THE COMPLETE PC-MRS SOWMYA JYOTHI REFERENCE-MIKE...
HARDWARE AND PC MAINTENANCE -THE COMPLETE PC-MRS SOWMYA JYOTHI REFERENCE-MIKE...HARDWARE AND PC MAINTENANCE -THE COMPLETE PC-MRS SOWMYA JYOTHI REFERENCE-MIKE...
HARDWARE AND PC MAINTENANCE -THE COMPLETE PC-MRS SOWMYA JYOTHI REFERENCE-MIKE...
 
Unit II chapter 4 Loops in C
Unit II chapter 4 Loops in CUnit II chapter 4 Loops in C
Unit II chapter 4 Loops in C
 
Unit ii chapter 2 Decision making and Branching in C
Unit ii chapter 2 Decision making and Branching in CUnit ii chapter 2 Decision making and Branching in C
Unit ii chapter 2 Decision making and Branching in C
 
Unit ii chapter 1 operator and expressions in c
Unit ii chapter 1 operator and expressions in cUnit ii chapter 1 operator and expressions in c
Unit ii chapter 1 operator and expressions in c
 
Overview of C Mrs Sowmya Jyothi
Overview of C Mrs Sowmya JyothiOverview of C Mrs Sowmya Jyothi
Overview of C Mrs Sowmya Jyothi
 
NETWORK AND DATABASE CONCEPTS UNIT 1 CHAPTER 2 MRS.SOWMYA JYOTHI
NETWORK AND DATABASE CONCEPTS UNIT 1 CHAPTER 2 MRS.SOWMYA JYOTHINETWORK AND DATABASE CONCEPTS UNIT 1 CHAPTER 2 MRS.SOWMYA JYOTHI
NETWORK AND DATABASE CONCEPTS UNIT 1 CHAPTER 2 MRS.SOWMYA JYOTHI
 
Introduction to computers MRS. SOWMYA JYOTHI
Introduction to computers MRS. SOWMYA JYOTHIIntroduction to computers MRS. SOWMYA JYOTHI
Introduction to computers MRS. SOWMYA JYOTHI
 
Introduction to graphics
Introduction to graphicsIntroduction to graphics
Introduction to graphics
 
Inter Process Communication PPT
Inter Process Communication PPTInter Process Communication PPT
Inter Process Communication PPT
 
Internal representation of file chapter 4 Sowmya Jyothi
Internal representation of file chapter 4 Sowmya JyothiInternal representation of file chapter 4 Sowmya Jyothi
Internal representation of file chapter 4 Sowmya Jyothi
 
Buffer cache unix ppt Mrs.Sowmya Jyothi
Buffer cache unix ppt Mrs.Sowmya JyothiBuffer cache unix ppt Mrs.Sowmya Jyothi
Buffer cache unix ppt Mrs.Sowmya Jyothi
 
Introduction to the Kernel Chapter 2 Mrs.Sowmya Jyothi
Introduction to the Kernel  Chapter 2 Mrs.Sowmya JyothiIntroduction to the Kernel  Chapter 2 Mrs.Sowmya Jyothi
Introduction to the Kernel Chapter 2 Mrs.Sowmya Jyothi
 

Recently uploaded

Digital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments UnitDigital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments Unit
chanes7
 
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptxC1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
mulvey2
 
Cognitive Development Adolescence Psychology
Cognitive Development Adolescence PsychologyCognitive Development Adolescence Psychology
Cognitive Development Adolescence Psychology
paigestewart1632
 
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptxChapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
Mohd Adib Abd Muin, Senior Lecturer at Universiti Utara Malaysia
 
PIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf IslamabadPIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf Islamabad
AyyanKhan40
 
The History of Stoke Newington Street Names
The History of Stoke Newington Street NamesThe History of Stoke Newington Street Names
The History of Stoke Newington Street Names
History of Stoke Newington
 
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama UniversityNatural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
Akanksha trivedi rama nursing college kanpur.
 
World environment day ppt For 5 June 2024
World environment day ppt For 5 June 2024World environment day ppt For 5 June 2024
World environment day ppt For 5 June 2024
ak6969907
 
Smart-Money for SMC traders good time and ICT
Smart-Money for SMC traders good time and ICTSmart-Money for SMC traders good time and ICT
Smart-Money for SMC traders good time and ICT
simonomuemu
 
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdfবাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
eBook.com.bd (প্রয়োজনীয় বাংলা বই)
 
Hindi varnamala | hindi alphabet PPT.pdf
Hindi varnamala | hindi alphabet PPT.pdfHindi varnamala | hindi alphabet PPT.pdf
Hindi varnamala | hindi alphabet PPT.pdf
Dr. Mulla Adam Ali
 
Community pharmacy- Social and preventive pharmacy UNIT 5
Community pharmacy- Social and preventive pharmacy UNIT 5Community pharmacy- Social and preventive pharmacy UNIT 5
Community pharmacy- Social and preventive pharmacy UNIT 5
sayalidalavi006
 
clinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdfclinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdf
Priyankaranawat4
 
How to Build a Module in Odoo 17 Using the Scaffold Method
How to Build a Module in Odoo 17 Using the Scaffold MethodHow to Build a Module in Odoo 17 Using the Scaffold Method
How to Build a Module in Odoo 17 Using the Scaffold Method
Celine George
 
How to Manage Your Lost Opportunities in Odoo 17 CRM
How to Manage Your Lost Opportunities in Odoo 17 CRMHow to Manage Your Lost Opportunities in Odoo 17 CRM
How to Manage Your Lost Opportunities in Odoo 17 CRM
Celine George
 
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UPLAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
RAHUL
 
Pride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School DistrictPride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School District
David Douglas School District
 
The simplified electron and muon model, Oscillating Spacetime: The Foundation...
The simplified electron and muon model, Oscillating Spacetime: The Foundation...The simplified electron and muon model, Oscillating Spacetime: The Foundation...
The simplified electron and muon model, Oscillating Spacetime: The Foundation...
RitikBhardwaj56
 
DRUGS AND ITS classification slide share
DRUGS AND ITS classification slide shareDRUGS AND ITS classification slide share
DRUGS AND ITS classification slide share
taiba qazi
 
How to Setup Warehouse & Location in Odoo 17 Inventory
How to Setup Warehouse & Location in Odoo 17 InventoryHow to Setup Warehouse & Location in Odoo 17 Inventory
How to Setup Warehouse & Location in Odoo 17 Inventory
Celine George
 

Recently uploaded (20)

Digital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments UnitDigital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments Unit
 
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptxC1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
 
Cognitive Development Adolescence Psychology
Cognitive Development Adolescence PsychologyCognitive Development Adolescence Psychology
Cognitive Development Adolescence Psychology
 
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptxChapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
 
PIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf IslamabadPIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf Islamabad
 
The History of Stoke Newington Street Names
The History of Stoke Newington Street NamesThe History of Stoke Newington Street Names
The History of Stoke Newington Street Names
 
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama UniversityNatural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
 
World environment day ppt For 5 June 2024
World environment day ppt For 5 June 2024World environment day ppt For 5 June 2024
World environment day ppt For 5 June 2024
 
Smart-Money for SMC traders good time and ICT
Smart-Money for SMC traders good time and ICTSmart-Money for SMC traders good time and ICT
Smart-Money for SMC traders good time and ICT
 
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdfবাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
 
Hindi varnamala | hindi alphabet PPT.pdf
Hindi varnamala | hindi alphabet PPT.pdfHindi varnamala | hindi alphabet PPT.pdf
Hindi varnamala | hindi alphabet PPT.pdf
 
Community pharmacy- Social and preventive pharmacy UNIT 5
Community pharmacy- Social and preventive pharmacy UNIT 5Community pharmacy- Social and preventive pharmacy UNIT 5
Community pharmacy- Social and preventive pharmacy UNIT 5
 
clinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdfclinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdf
 
How to Build a Module in Odoo 17 Using the Scaffold Method
How to Build a Module in Odoo 17 Using the Scaffold MethodHow to Build a Module in Odoo 17 Using the Scaffold Method
How to Build a Module in Odoo 17 Using the Scaffold Method
 
How to Manage Your Lost Opportunities in Odoo 17 CRM
How to Manage Your Lost Opportunities in Odoo 17 CRMHow to Manage Your Lost Opportunities in Odoo 17 CRM
How to Manage Your Lost Opportunities in Odoo 17 CRM
 
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UPLAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
 
Pride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School DistrictPride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School District
 
The simplified electron and muon model, Oscillating Spacetime: The Foundation...
The simplified electron and muon model, Oscillating Spacetime: The Foundation...The simplified electron and muon model, Oscillating Spacetime: The Foundation...
The simplified electron and muon model, Oscillating Spacetime: The Foundation...
 
DRUGS AND ITS classification slide share
DRUGS AND ITS classification slide shareDRUGS AND ITS classification slide share
DRUGS AND ITS classification slide share
 
How to Setup Warehouse & Location in Odoo 17 Inventory
How to Setup Warehouse & Location in Odoo 17 InventoryHow to Setup Warehouse & Location in Odoo 17 Inventory
How to Setup Warehouse & Location in Odoo 17 Inventory
 

Stacks IN DATA STRUCTURES

  • 1. Stacks MRS.SOWMYA JYOTHI, SDMCBM, MANGALORE Reference: Data Structures with C by Seymour Lipschutz, Schaum’s Outlines Series.
  • 2. Stacks INTRODUCTION • The linear lists and linear arrays are used to insert and delete elements at any place. There are certain frequent situations when one wants to restrict insertion and deletion so that they can take place only at the beginning or end of the list, not in the middle. • Two of the data structures that are useful in such situations are stacks and queues. • A stack is a linear structure in which items may be added or removed only at one end. It is called last-in first-out (LIFO). • A queue is a linear list in which items may be added only at one end and items may be removed only at the other end. It is called first-in first-out (FIFO) list.
  • 3. STACKS • A stack is a list of elements in which an element may be inserted or deleted only a one end, called TOP of the stack. • The elements are removed in reverse order of that in which they were inserted into the stack.
  • 4. Basic operations: a) Push() is the term used to insert/add an element into a stack. b) Pop() is the term used to delete/remove an element from a stack. •Other names for stacks are piles and push-down lists.
  • 5. •There are two ways to represent Stack in memory. 1. One is using array and 2. Other is using linked list.
  • 6. Example: • Suppose the following 6 elements are pushed, in order, onto an empty stack: • AAA, BBB, CCC, DDD, EEE, FFF
  • 7. ARRAY REPRESENTATION OF STACKS • Stacks are represented in the computer by a linear array. • In the following algorithms/procedures of pushing and popping an item from the stacks, 1. a linear array STACK, 2. a variable TOP which contains the location of the top element of the stack; 3. and a variable MAXSTAK which gives the maximum number of elements that can be held by the stack.
  • 8. OVERFLOW AND UNDERFLOW 1. The condition TOP=0 or TOP=NULL will indicate the stack is empty and we say UNDERFLOW. 2. The condition TOP=MAXSTK will indicate the stack is full and we say OVERFLOW.
  • 9. The operation of adding an element into the stack is called pushing and the operation of removing an element from the stack is called popping.
  • 10. Algorithm 6.1: PUSH(STACK, TOP, MAXSTAK, ITEM): This procedure pushes an ITEM onto a stack. 1. [STACK already filled?] If TOP=MAXSTAK, then: Print: OVERFLOW, and Return 2. Set TOP:=TOP+1 [Increase TOP by 1] 3. Set STACK [TOP]:=ITEM [Insert ITEM in new TOP position] 4. Return
  • 11. Example: To simulate the operation PUSH(STACK, WWW) 1. Since TOP=3, control is transferred to Step 2. 2. TOP=3+1=4 3. STACK[TOP]=STACK[4]=WWW 4. Return
  • 12. Algorithm 6.2: POP(STACK, TOP, ITEM) This procedure deletes the top element of STACK and assigns it to the variable ITEM. 1. [STACK has an item to be removed? Check for empty stack] If TOP=-1, then: Print: UNDERFLOW, and Return 2. Set ITEM:=STACK[TOP] [Assigns TOP element to ITEM] 3. Set TOP=TOP-1 [Decrease TOP by 1.] 4. Return
  • 13. To simulate the operation POP(STACK, ITEM) 1. Since TOP=3, control is transferred to Step 2. 2. ITEM=ZZZ 3. TOP=3-1=2 4. Return
  • 14. LINKED REPRESENTATION OF STACKS • The linked representation of stack is implemented using singly linked list. 1. The INFO fields of the nodes hold the elements of the stack and 2. The LINK fields hold the pointers to the neighboring element in the stack. 3. The START pointer of the linked list behaves as TOP pointer variable of the stack and 4. The NULL pointer in the last node signals the end of the stack.
  • 15.
  • 16. • A PUSH operation is accomplished by inserting a node into the start of the STACK and a POP operation is undertaken by deleting the node pointed to by the START pointer. • The array representation of stack maintains a variable MAXSTK which gives the maximum number of elements that can be held by the stack. • If the number of elements in the stack becomes equal to the value of variable MAXSTK, it is the condition of OVERFLOW. • The linked representation of stacks is free from this requirement.
  • 17. • There is no limitation on the capacity of the linked stack hence it can support as many PUSH operations as free storage list (the AVAIL list) can support. 1. However, the condition TOP=NULL may be retained in the POP procedure to prevent deletion from an empty stack and 2. AVAIL=NULL to check for the available space in the free storage list.
  • 18.
  • 19.
  • 20. Algorithm 6.3: PUSH_LINKSTACK(INFO, LINK, TOP, AVAIL, ITEM) This procedure pushes an ITEM onto a stack. 1 IF AVAIL=NULL, then Write: OVERFLOW and Exit [Available Space?] 2. [Remove first node from AVAIL list] Set NEW:=AVAIL and AVAIL:=LINK[AVAIL] 3. Set INFO[NEW]:=ITEM [ Copies the ITEM into new node] 4. Set LINK[NEW]:=TOP [New node points to the original top node in the stack] 5. Set TOP:=NEW [Reset TOP to point to the new node at the top of the stack] 6. Exit
  • 21. Algorithm 6.4: POP_LINKSTACK(INFO, LINK, TOP, AVAIL, ITEM) This procedure deletes the top element of STACK and assigns it to the variable ITEM. 1. [STACK has an item to be removed? Check for empty stack] If TOP=NULL , then: Write: UNDERFLOW, and Exit 2. Set ITEM:=INFO[TOP]. [Copies the top element of stack into ITEM] 3. Set TEMP:=TOP and TOP:=LINK[TOP] [Remember the old value of TOP in TEMP and reset TOP to point to the next element] 4. [Return deleted node to the AVAIL list] Set LINK [TEMP]:=AVAIL and AVAIL:=TEMP 5. Exit
  • 22. Example: Consider the linked list given in fig (a) and perform the following operations: I) Push BBB ii) Pop iii) Pop iv) Push MMM
  • 23. I) Push BBB ii) Pop iii) Pop iv) Push MMM
  • 24. APPLICATION OF STACKS: Quick Sort • Let A be a list of n data items. Quick sort is an algorithm of the divide-and- conquer type. That is, the problem of sorting a set is reduced to the problem of sorting two smaller sets. RECURSION • Suppose P is a procedure containing either a Call statement to itself or a Call statement to a second procedure that may eventually result in a call statement back to the original procedure P. then P is called recursive procedure. The program contains a recursive procedure should not run infinitely. Hence, a recursive procedure/function must have the following properties: 1) There must be certain criteria, called base criteria, for which the procedure does not call itself. 2) Each time when the procedure is called, it must be closer to that criterion.
  • 25. RECURSION • Suppose P is a procedure containing either a Call statement to itself or a Call statement to a second procedure that may eventually result in a call statement back to the original procedure P. then P is called recursive procedure. The program contains a recursive procedure should not run infinitely. • Hence, a recursive procedure/function must have the following properties: 1) There must be certain criteria, called base criteria, for which the procedure does not call itself. 2) Each time when the procedure is called, it must be closer to that criterion.
  • 26. Factorial Function a) If N = 0, then N! = 1. b) If N > 0, then N! = N*(N-1)! Example: 5! = 5 * 4! = 5 * 4 * 3! = 5 * 4 * 3 * 2! = 5 * 4 * 3 * 2 * 1! = 5 * 4 * 3 * 2 * 1* 0! = 5 * 4 * 3 * 2 * 1 * 1 = 120 Procedure 6.9: FACTORIAL (FACT, N) This recursive procedure calculates N! and returns the value in the variable FACT 1. If N = 0, then : Set FACT := 1 and Return 2. Call FACTORIAL(FACT, N-1) 3. Set FACT := N*FACT.
  • 27. Fibonacci Sequence • The Fibonacci sequence is 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55. F0 =0 and F1= 1. • Each succeeding term is the sum of two preceding terms. a) If n = 1 then Fn = 0. b) If n = 2 then Fn = 1 c) If n > 2, then Fn = Fn-1 + Fn-2
  • 28. Procedure 6.10: FIBONACCI(FIB, N) •This recursive procedure calculates FN and returns the value in the parameter FIB. 1. If N = 0 or N= 1 then: Set FIB:=N and Return 2. Call FIBNOCCI(FIBA, N-2) 3. Call FIBNOCCI(FIBB, N-1) 4. Set FIB := FIBA + FIBB 5. Return
  • 29. ARITHMETIC EXPRESSIONS • INFIX , POSTFIX AND PREFIX NOTATIONS: • Let Q be an arithmetic expression involving constants and operations. • Besides operands and operators, Q may also contain left and right parentheses. • The operators in Q can consist of exponentiations ( ) multiplications (*), divisions (/) additions (+) and subtractions (-). They have different levels of precedence. • Highest : Exponentiation ( ) • Next Highest : Multiplication (*) and division (/) • Lowest : Addition (+) and subtraction (-)
  • 30. Highest : Exponentiation ( ) Next Highest : Multiplication (*) and division (/) Lowest : Addition (+) and subtraction (-)
  • 31. 1. Generally, expressions can be written in which the operator is written between the operands. This is called infix notation. For example: A + B C - D 2. Polish notation, named after the Polish mathematician Jan Lukasiewicz, refers to the notation in which the operator symbol is placed before its two operands. This notation frequently called prefix notation. For example, + A B - C D 3. Reverse Polish notation refers to the analogous notation in which the operator symbol is placed after two operands. This notation frequently called postfix (or suffix) notation For example: A B + C D - • Computers “prefer” postfix notation in which the operator is written to the right of two operands.
  • 32. • The computer usually evaluates an arithmetic expression written in infix notation in two steps. 1. First, it converts the expression to postfix notation and 2. Then it evaluates the postfix expression. • In each step stack is used as main tool.
  • 33. Evaluation of a Postfix Expression: •Suppose P is an arithmetic expression written in postfix notation. •The following algorithm uses STACK to evaluate P.
  • 34.
  • 35.
  • 36.
  • 37. Example: • Consider P: 5, 6, 2, +, *, 12, 4, /, − whose equivalent infix expression is • Q: 5 * ( 6 + 2 ) – 12 / 4 • The elements of P are labelled from left to right for ease of reference as follows: •P: 5, 6, 2, +, *, 12, 4, /, −, ) (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)
  • 38. P: 5, 6, 2, +, *, 12, 4, /, −
  • 39. P: 5, 6, 2, +, *, 12, 4, /, −, ) (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)
  • 40. Transforming Infix Expression into Postfix Expression: • The following algorithm transforms the infix expression Q into its equivalent postfix expression P. • It uses a stack to temporary hold the operators and left parenthesis. • The postfix expression will be constructed from left to right using operands from Q and operators popped from STACK.