SlideShare a Scribd company logo
1 of 21
Definition & Diagram of Stack
A stack is an ordered collection of items into which new item can be inserted one end, and can be removed from
same end called as a top of the stack.
This implies that stack is different than array as it is ordered collection of elements having property last in pass Out.
Hence stack also uses different terminologies for operations, which are referred as Push and Pop instead of Store
and Retrieve in array.
4
3
2
1
0
4
3
2
1
0
4
3
2
1
0
4
3
2
1
0
4
3
2
1
0
4
3
2
1
0
4
3
2
1
0
4
3
2
1
0
4
3
2
1
0
4
3
2
1
0
4
3
2
1
0
4
3
2
1
0
Stack empty
Stack full
PUSH Stack full
Stack emptyPOP
Top
Top
Top
Top
Top
Top
Top
Top
Top
Top
Top
Top
10
20
10
30
20
10
40
30
20
10
50
40
30
20
10
50
40
30
20
10
40
30
20
10
30
20
10
20
10 10
1
Stack as an ADT & Operations on Stack
2
Stack S is a finite collection of elements having same data type and all operations are carried out at one
end called a top
Operations on Stack
1. Push (item x, Stack S) -> Stack S’;
If stack is not full then,
This function inserts an item x into top of the stack and returns new stack S’ with Top pointing to the position
of x.
2. Pop (Stack S) -> Stack S’;
if stack is not empty then,
This function deletes the item x pointed by the top of the stack and returns new Stack S’ with Top pointing to
the item down to the deleted item.
3. isEmpaty (Stack S) -. TRUE / FALSE;
This function returns TRUE when S is empty else FALSE.
4. isFull (Stack S) -. TRUE / FALSE;
This function returns TRUE when S is full else FALSE.
Arrays & Stack (Difference)
3
SR Array Stack
i We can insert an element at any location in
an array.
A new element can be added only at the top of a
stack.
ii Any element of an array can be accessed. Only the topmost element can be accessed.
iii An array essentially contains homogenous
elements.
A stack may contain diverse elements.
iv An array is a static data structure i.e. Its size
remains constant.
A stack is a dynamic data structure i.e. its size grows
and shrinks as elements are pushed and popped.
v There is an upper limit on the size of the
array, which is specified during declaration.
Logically, a stack can grow to any size.
Declaring a Stack
4
We can use typedef to create STACK as a user defined type.
#define MAX 100
Typedef struct
{
int top;
int data [MAX];
} STACK;
1. Creating a Stack: To create a stack, we should create a variable of the abc structure.
Example
STACK s;
This will create a stack named s.
2. Initializing a Stack: When a stack is created, top has to be initialized to indicate that the stack is empty at the
beginning.
Since we are using an array, the first element will be stored at position 0. Hence to indicate an empty stack,
top should be initialized to -1. This can be done as follows:
s.top = -1;
Declaring a Stack
5
3. The Push Operation: To push an element “num” into the stack, we should first increment top and then store
the element in the array at position top. The steps are shown bellows:
s.top++;
s.data[s.top] = num;
This can also be written as:
s.data [++s.top] + num;
4. The Pop Operation: To pop the topmost element from the stack, we should first access the internet at the top
position and then decrement top. The steps are shown below:
num = s.data [s.top];
s.top--;
This can also be written as:
num = s.data [s.top--];
Declaring a Stack
6
5. The Isempty Operation: An element can be popped only if the stack is not empty. To whether the stack is
empty, we can chek if top has-1.
if (s.top == -1)
6. The Isfull Operation: If the value of top reaches the maximum array index i.e. MAX-1, no more elements can
be pushed into the stack. This indicates a full stack.
if (s.top == MAX-1)
We can define all the operations in the form of functions. However, since the function will modify the stack
variable, we should pass the address of al the stack variable to the function. Hence, the function will use a pointer to
STACK to access the stack members.
Static implementation of Stack Algorithm
7
1. Start
2. Top = -1
3. Accept choice “Push” or “Pop”
4. If choice == “Push” then
if top == max-1 then
printf ”stack overflow”
else
top = top+1
stack [top] = num
5. If choice == “Pop” then
if top ==-1 then
printf “stack underflow”
else
top = top-1
6. Stop
Static implementation of Stack (Example)
8
We can illustrate the concepts above with the help of diagrams. We will accept of size 3.
1. Initially, the stack is empty
0 1 2-1
0 1 2
0 1 2
0 1 2
-1
-1
-1
top
top
top
top
10 20
10
10
2. Push 10
top = top+1=-1+1 = 0
3. Push 20
top = 0+1 = 1
4. Pop
top = top-1 = 1-1 = 0
5. Pop
top = top-1 = 0-1 = -1
Application of Stack
9
1. Reversing a list.
2. Polish Notations.
3. Conversion of an infix to postfix expression.
4. Evaluation of postfix expression.
5. Conversion of an infix to prefix expression.
6. Evaluation of prefix expression.
7. Recursion.
Infix Notations & Prefix Notations
10
(i) Infix Notations: When the operators exist between two operands then the expression is called infix expression.
Example: The expression to add two numbers A and B is written in infix notation as:
A+B
(ii) Prefix Notations: When the operators are written before their operands, than the expression is called the prefix
notation or prefix polish notation.
Example: The expression to add two numbers A and B is written in prefix notation as:
+AB
(iii) Postfix Notations: When the operators are written after the operands, it is called postfix or suffix notations. It
is also known as reverse polish notation.
Example: The expression to add two numbers A and B is written in postfix notation as:
AB+
Notations
11
Infix Expression Prefix Expression Postfix Expression
A+B +AB AB+
(A -C)*B *- ACD AC-*B
(A +B)/(C - D) /+ AB - CD AB + CD -/
A + (C*D) +A*CD ACD*+
Operator Precedence
12
Operator Symbol Precedence
Exponential $ Highest
Multiplication / Division *,/ Next Highest
Addition / Subtraction +,- Lowest
Converting Infix to Postfix Expression Algorithm
13
1. Start
2. opstk is an empty stack.
3. ch is the next character from the infix string.
4. If ch is an operand
add ch to postfix string.
5. If ch is ‘(’
push ch into opstk
6. If ch is an operator
while priority (top-of-stack) > = priority (ch) pop form opstk and add to postfix string.
push ch into opstk
7. If ch is ‘)’
ch1 = pop from opstk
while (ch1 is not ‘(’ and opstk is not empty)
add ch1 to postfix string
ch1 = pop (opstk)
8. If infix string has not ended
go to Step 3
9. While opstk is not empty.
pop ch and add to postfix string
10. Display postfix string.
11. stop
Evaluation of Postfix Expression
14
SR Symbol scanned Stack Expression
1 ( ( ~
2 ( (( ~
3 ( ((( ~
4 A ((( A
5 + (((+ A
6 B (((+ AB
7 ) (( AB+
8 * ((* AB+
9 D ((* AB + D
10 ) ( AB + D*
11 ( AB + D*
12 ( ( ( AB + D*
13 E ( ( AB + D * E
14 - ( (- AB + D * E
15 F ( (- AB + D * EF
16 ) ( AB + D * EF-
17 ) AB + D * EF-
Given Expression: ((A + B) * D) (E - F)
15
SR Symbol scanned Stack Expression
1 ( ( ~
2 A ( A
3 * ( * A
4 B ( * AB
5 - ( - AB*
6 C ( - AB*C
7 ( - AB*C
8 D ( - AB*CD
9 + ( - + AB*CD
10 E ( - + AB*CD E
11 / ( - +/ AB*CD E
12 F ( - +/ AB*CD EF
13 ) AB*CD EF/ + -
Given Expression: (A * B – C D + E/F)
16
SR Symbol scanned Stack
1 6 6
2 2 6,2
3 3 6,2,3
4 + 6,5
5 - 1
6 3 1,3
7 8 1,3,8
8 2 1,2,8,2
9 / 1,3,4
10 + 1,7
11 * 7
12 2 7,2
13 49
14 3 49,3
15 + 52
Given Expression: 6,2,3,+,-,3,8,2,/,+,*,2, ,3,+
17
SR Symbol scanned Stack Expression
1 ) ) ~
2 ) )) ~
3 D )) D
4 - ))- D
5 C ))- CD
6 ( ) -CD
7 * )* -CD
8 ) )*) -CD
9 B )*) B – CD
10 + )*)+ B – CD
11 A )*)+ AB – CD
12 ( )* +AB – CD
13 ( - *+AB – CD
Example: Convert infix string ((A+B) * (C - D) into
prefix string with stack operations show the content of
stack in each step.
18
SR Symbol scanned Stack Expression
1 ) ) ~
2 ) )) ~
3 F )) F
4 - ))- F
5 E ))- EF
6 * ))-* EF
7 D ))-* DEF
8 ( ) -*DEF
9 * )* -*DEF
10 ) )*) -*DEF
11 C )*) C - *DEF
12 / )*)/ C - *DEF
13 B (*)/ BC - *DEF
14 - )*)- /BC - *DEF
15 A )*)- A/BC - *DEF
16 ( )* -A/BC - *DEF
17 ( *-A/BC - *DEF
Example: Convert the given string to prefix
exprssio0n and shows the details of stack at each step.
(A- B/C) * (D * E - F)
Recursion (Definition)
19
Recursion is a process of expressing a fraction. In them of itself in recursion.
It is essential for a fraction to call itself, otherwise recursion will not into replace only user
depend function can be involved in the recursion library function cannot be involved in
recursion became their source code cannot be viewed.
Recursion (Example)
20
Write a program to find factorial of number using recursion.
/* program to find factorial of given number */
#include <studio.h>
#include <conio.h>
Voide main()
{
int fact(int);
int num;
clrscr();
printf(“Enter the numbern”);
scanf(“%d”, & num);
printf(“The factorial of %d=%d”,num,fact(num));
getch();
}
int fact(int n)
{
if(n==0)
return(1);
return(n * fact(n-1));
}
Tower of Hanoi (Diagram)
21
A B C

More Related Content

What's hot (20)

Linked list
Linked listLinked list
Linked list
 
Stacks
StacksStacks
Stacks
 
Queues
QueuesQueues
Queues
 
single linked list
single linked listsingle linked list
single linked list
 
Stack a Data Structure
Stack a Data StructureStack a Data Structure
Stack a Data Structure
 
Double ended queue
Double ended queueDouble ended queue
Double ended queue
 
Linked list
Linked listLinked list
Linked list
 
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
 
Single linked list
Single linked listSingle linked list
Single linked list
 
DATA STRUCTURES
DATA STRUCTURESDATA STRUCTURES
DATA STRUCTURES
 
Linked list
Linked listLinked list
Linked list
 
Stack and Queue
Stack and Queue Stack and Queue
Stack and Queue
 
sorting and its types
sorting and its typessorting and its types
sorting and its types
 
Stacks in DATA STRUCTURE
Stacks in DATA STRUCTUREStacks in DATA STRUCTURE
Stacks in DATA STRUCTURE
 
Linked lists
Linked listsLinked lists
Linked lists
 
Arrays
ArraysArrays
Arrays
 
STACK ( LIFO STRUCTURE) - Data Structure
STACK ( LIFO STRUCTURE) - Data StructureSTACK ( LIFO STRUCTURE) - Data Structure
STACK ( LIFO STRUCTURE) - Data Structure
 
Linked list
Linked list Linked list
Linked list
 
Queue Data Structure
Queue Data StructureQueue Data Structure
Queue Data Structure
 
heap Sort Algorithm
heap  Sort Algorithmheap  Sort Algorithm
heap Sort Algorithm
 

Similar to Stack - Data Structure - Notes

Data structure lab manual
Data structure lab manualData structure lab manual
Data structure lab manualnikshaikh786
 
Data structure lecture7
Data structure lecture7Data structure lecture7
Data structure lecture7Kumar
 
Applicationofstack by Ali F.RAshid
Applicationofstack  by Ali F.RAshid Applicationofstack  by Ali F.RAshid
Applicationofstack by Ali F.RAshid ali rashid
 
Application of Stack - Yadraj Meena
Application of Stack - Yadraj MeenaApplication of Stack - Yadraj Meena
Application of Stack - Yadraj MeenaDipayan Sarkar
 
5 chapter3 list_stackqueuepart2
5 chapter3 list_stackqueuepart25 chapter3 list_stackqueuepart2
5 chapter3 list_stackqueuepart2SSE_AndyLi
 
Lec5-Stack-bukc-28022024-112316am (1) .pptx
Lec5-Stack-bukc-28022024-112316am (1) .pptxLec5-Stack-bukc-28022024-112316am (1) .pptx
Lec5-Stack-bukc-28022024-112316am (1) .pptxhaaamin01
 
C++ Program It is only 1 rotation. Up-rotation of a stack. Write a.pdf
C++ Program It is only 1 rotation. Up-rotation of a stack.  Write a.pdfC++ Program It is only 1 rotation. Up-rotation of a stack.  Write a.pdf
C++ Program It is only 1 rotation. Up-rotation of a stack. Write a.pdfpallavi953613
 
STACKS IN DATASTRUCTURE
STACKS IN DATASTRUCTURESTACKS IN DATASTRUCTURE
STACKS IN DATASTRUCTUREArchie Jamwal
 

Similar to Stack - Data Structure - Notes (20)

Stack
StackStack
Stack
 
Data structure lab manual
Data structure lab manualData structure lab manual
Data structure lab manual
 
Stack
StackStack
Stack
 
stacks and queues
stacks and queuesstacks and queues
stacks and queues
 
Stacks in c++
Stacks in c++Stacks in c++
Stacks in c++
 
stack
stackstack
stack
 
Data structure lecture7
Data structure lecture7Data structure lecture7
Data structure lecture7
 
Stack
StackStack
Stack
 
Stacks.ppt
Stacks.pptStacks.ppt
Stacks.ppt
 
Stacks.ppt
Stacks.pptStacks.ppt
Stacks.ppt
 
Applicationofstack by Ali F.RAshid
Applicationofstack  by Ali F.RAshid Applicationofstack  by Ali F.RAshid
Applicationofstack by Ali F.RAshid
 
Application of Stack - Yadraj Meena
Application of Stack - Yadraj MeenaApplication of Stack - Yadraj Meena
Application of Stack - Yadraj Meena
 
Stack
StackStack
Stack
 
Stack
StackStack
Stack
 
Unit 3 stack
Unit   3 stackUnit   3 stack
Unit 3 stack
 
5 chapter3 list_stackqueuepart2
5 chapter3 list_stackqueuepart25 chapter3 list_stackqueuepart2
5 chapter3 list_stackqueuepart2
 
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
 
C++ Program It is only 1 rotation. Up-rotation of a stack. Write a.pdf
C++ Program It is only 1 rotation. Up-rotation of a stack.  Write a.pdfC++ Program It is only 1 rotation. Up-rotation of a stack.  Write a.pdf
C++ Program It is only 1 rotation. Up-rotation of a stack. Write a.pdf
 
STACKS IN DATASTRUCTURE
STACKS IN DATASTRUCTURESTACKS IN DATASTRUCTURE
STACKS IN DATASTRUCTURE
 
Stack
StackStack
Stack
 

More from Omprakash Chauhan

Sorting and Searching - Data Structure - Notes
Sorting and Searching - Data Structure - NotesSorting and Searching - Data Structure - Notes
Sorting and Searching - Data Structure - NotesOmprakash Chauhan
 
Basic of Data Structure - Data Structure - Notes
Basic of Data Structure - Data Structure - NotesBasic of Data Structure - Data Structure - Notes
Basic of Data Structure - Data Structure - NotesOmprakash Chauhan
 
Polygons - Computer Graphics - Notes
Polygons - Computer Graphics - NotesPolygons - Computer Graphics - Notes
Polygons - Computer Graphics - NotesOmprakash Chauhan
 
Line Drawing Algorithms - Computer Graphics - Notes
Line Drawing Algorithms - Computer Graphics - NotesLine Drawing Algorithms - Computer Graphics - Notes
Line Drawing Algorithms - Computer Graphics - NotesOmprakash Chauhan
 
Basic of computer graphic - Computer Graphic - Notes
Basic of computer graphic - Computer Graphic - NotesBasic of computer graphic - Computer Graphic - Notes
Basic of computer graphic - Computer Graphic - NotesOmprakash Chauhan
 
E-R Diagram of College Management Systems
E-R Diagram of College Management SystemsE-R Diagram of College Management Systems
E-R Diagram of College Management SystemsOmprakash Chauhan
 
Full Wave Rectifier (FWR) Microproject
Full Wave Rectifier (FWR) MicroprojectFull Wave Rectifier (FWR) Microproject
Full Wave Rectifier (FWR) MicroprojectOmprakash Chauhan
 
A detailed study of guidelines required for presentation skills
A detailed study of guidelines required for presentation skillsA detailed study of guidelines required for presentation skills
A detailed study of guidelines required for presentation skillsOmprakash Chauhan
 
Fractional-horsepower Motor Report
Fractional-horsepower Motor ReportFractional-horsepower Motor Report
Fractional-horsepower Motor ReportOmprakash Chauhan
 
motherboard electronic components and their functions
motherboard electronic components and their functionsmotherboard electronic components and their functions
motherboard electronic components and their functionsOmprakash Chauhan
 
How To Area of irregular shape Using Integration Method.
How To Area of irregular shape Using Integration Method.How To Area of irregular shape Using Integration Method.
How To Area of irregular shape Using Integration Method.Omprakash Chauhan
 
Welcome to the world of ionization
Welcome to the world of ionization Welcome to the world of ionization
Welcome to the world of ionization Omprakash Chauhan
 

More from Omprakash Chauhan (18)

Introduction to curve
Introduction to curveIntroduction to curve
Introduction to curve
 
Sorting and Searching - Data Structure - Notes
Sorting and Searching - Data Structure - NotesSorting and Searching - Data Structure - Notes
Sorting and Searching - Data Structure - Notes
 
Basic of Data Structure - Data Structure - Notes
Basic of Data Structure - Data Structure - NotesBasic of Data Structure - Data Structure - Notes
Basic of Data Structure - Data Structure - Notes
 
Polygons - Computer Graphics - Notes
Polygons - Computer Graphics - NotesPolygons - Computer Graphics - Notes
Polygons - Computer Graphics - Notes
 
Line Drawing Algorithms - Computer Graphics - Notes
Line Drawing Algorithms - Computer Graphics - NotesLine Drawing Algorithms - Computer Graphics - Notes
Line Drawing Algorithms - Computer Graphics - Notes
 
Basic of computer graphic - Computer Graphic - Notes
Basic of computer graphic - Computer Graphic - NotesBasic of computer graphic - Computer Graphic - Notes
Basic of computer graphic - Computer Graphic - Notes
 
E-R Diagram of College Management Systems
E-R Diagram of College Management SystemsE-R Diagram of College Management Systems
E-R Diagram of College Management Systems
 
Burglar Alarm Micro Project
Burglar Alarm Micro ProjectBurglar Alarm Micro Project
Burglar Alarm Micro Project
 
Simple Calculator Flowchart
Simple Calculator FlowchartSimple Calculator Flowchart
Simple Calculator Flowchart
 
Full Wave Rectifier (FWR) Microproject
Full Wave Rectifier (FWR) MicroprojectFull Wave Rectifier (FWR) Microproject
Full Wave Rectifier (FWR) Microproject
 
A detailed study of guidelines required for presentation skills
A detailed study of guidelines required for presentation skillsA detailed study of guidelines required for presentation skills
A detailed study of guidelines required for presentation skills
 
Fractional-horsepower Motor Report
Fractional-horsepower Motor ReportFractional-horsepower Motor Report
Fractional-horsepower Motor Report
 
motherboard electronic components and their functions
motherboard electronic components and their functionsmotherboard electronic components and their functions
motherboard electronic components and their functions
 
How To Area of irregular shape Using Integration Method.
How To Area of irregular shape Using Integration Method.How To Area of irregular shape Using Integration Method.
How To Area of irregular shape Using Integration Method.
 
Process of ionization
Process of ionizationProcess of ionization
Process of ionization
 
Welcome to the world of ionization
Welcome to the world of ionization Welcome to the world of ionization
Welcome to the world of ionization
 
Printer
PrinterPrinter
Printer
 
System of units
System of unitsSystem of units
System of units
 

Recently uploaded

Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024hassan khalil
 
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...ZTE
 
Application of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptxApplication of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptx959SahilShah
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Dr.Costas Sachpazis
 
What are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxWhat are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxwendy cai
 
Past, Present and Future of Generative AI
Past, Present and Future of Generative AIPast, Present and Future of Generative AI
Past, Present and Future of Generative AIabhishek36461
 
Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxpurnimasatapathy1234
 
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...VICTOR MAESTRE RAMIREZ
 
Current Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCLCurrent Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCLDeelipZope
 
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...srsj9000
 
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝soniya singh
 
microprocessor 8085 and its interfacing
microprocessor 8085  and its interfacingmicroprocessor 8085  and its interfacing
microprocessor 8085 and its interfacingjaychoudhary37
 
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
chaitra-1.pptx fake news detection using machine learning
chaitra-1.pptx  fake news detection using machine learningchaitra-1.pptx  fake news detection using machine learning
chaitra-1.pptx fake news detection using machine learningmisbanausheenparvam
 
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdfCCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdfAsst.prof M.Gokilavani
 
Artificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptxArtificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptxbritheesh05
 
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escortsranjana rawat
 

Recently uploaded (20)

Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024
 
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Serviceyoung call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
 
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...
 
Application of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptxApplication of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptx
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
 
What are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxWhat are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptx
 
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptxExploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
 
Past, Present and Future of Generative AI
Past, Present and Future of Generative AIPast, Present and Future of Generative AI
Past, Present and Future of Generative AI
 
Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptx
 
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
 
Current Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCLCurrent Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCL
 
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
 
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
 
microprocessor 8085 and its interfacing
microprocessor 8085  and its interfacingmicroprocessor 8085  and its interfacing
microprocessor 8085 and its interfacing
 
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
 
🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...
🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...
🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...
 
chaitra-1.pptx fake news detection using machine learning
chaitra-1.pptx  fake news detection using machine learningchaitra-1.pptx  fake news detection using machine learning
chaitra-1.pptx fake news detection using machine learning
 
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdfCCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
 
Artificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptxArtificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptx
 
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
 

Stack - Data Structure - Notes

  • 1. Definition & Diagram of Stack A stack is an ordered collection of items into which new item can be inserted one end, and can be removed from same end called as a top of the stack. This implies that stack is different than array as it is ordered collection of elements having property last in pass Out. Hence stack also uses different terminologies for operations, which are referred as Push and Pop instead of Store and Retrieve in array. 4 3 2 1 0 4 3 2 1 0 4 3 2 1 0 4 3 2 1 0 4 3 2 1 0 4 3 2 1 0 4 3 2 1 0 4 3 2 1 0 4 3 2 1 0 4 3 2 1 0 4 3 2 1 0 4 3 2 1 0 Stack empty Stack full PUSH Stack full Stack emptyPOP Top Top Top Top Top Top Top Top Top Top Top Top 10 20 10 30 20 10 40 30 20 10 50 40 30 20 10 50 40 30 20 10 40 30 20 10 30 20 10 20 10 10 1
  • 2. Stack as an ADT & Operations on Stack 2 Stack S is a finite collection of elements having same data type and all operations are carried out at one end called a top Operations on Stack 1. Push (item x, Stack S) -> Stack S’; If stack is not full then, This function inserts an item x into top of the stack and returns new stack S’ with Top pointing to the position of x. 2. Pop (Stack S) -> Stack S’; if stack is not empty then, This function deletes the item x pointed by the top of the stack and returns new Stack S’ with Top pointing to the item down to the deleted item. 3. isEmpaty (Stack S) -. TRUE / FALSE; This function returns TRUE when S is empty else FALSE. 4. isFull (Stack S) -. TRUE / FALSE; This function returns TRUE when S is full else FALSE.
  • 3. Arrays & Stack (Difference) 3 SR Array Stack i We can insert an element at any location in an array. A new element can be added only at the top of a stack. ii Any element of an array can be accessed. Only the topmost element can be accessed. iii An array essentially contains homogenous elements. A stack may contain diverse elements. iv An array is a static data structure i.e. Its size remains constant. A stack is a dynamic data structure i.e. its size grows and shrinks as elements are pushed and popped. v There is an upper limit on the size of the array, which is specified during declaration. Logically, a stack can grow to any size.
  • 4. Declaring a Stack 4 We can use typedef to create STACK as a user defined type. #define MAX 100 Typedef struct { int top; int data [MAX]; } STACK; 1. Creating a Stack: To create a stack, we should create a variable of the abc structure. Example STACK s; This will create a stack named s. 2. Initializing a Stack: When a stack is created, top has to be initialized to indicate that the stack is empty at the beginning. Since we are using an array, the first element will be stored at position 0. Hence to indicate an empty stack, top should be initialized to -1. This can be done as follows: s.top = -1;
  • 5. Declaring a Stack 5 3. The Push Operation: To push an element “num” into the stack, we should first increment top and then store the element in the array at position top. The steps are shown bellows: s.top++; s.data[s.top] = num; This can also be written as: s.data [++s.top] + num; 4. The Pop Operation: To pop the topmost element from the stack, we should first access the internet at the top position and then decrement top. The steps are shown below: num = s.data [s.top]; s.top--; This can also be written as: num = s.data [s.top--];
  • 6. Declaring a Stack 6 5. The Isempty Operation: An element can be popped only if the stack is not empty. To whether the stack is empty, we can chek if top has-1. if (s.top == -1) 6. The Isfull Operation: If the value of top reaches the maximum array index i.e. MAX-1, no more elements can be pushed into the stack. This indicates a full stack. if (s.top == MAX-1) We can define all the operations in the form of functions. However, since the function will modify the stack variable, we should pass the address of al the stack variable to the function. Hence, the function will use a pointer to STACK to access the stack members.
  • 7. Static implementation of Stack Algorithm 7 1. Start 2. Top = -1 3. Accept choice “Push” or “Pop” 4. If choice == “Push” then if top == max-1 then printf ”stack overflow” else top = top+1 stack [top] = num 5. If choice == “Pop” then if top ==-1 then printf “stack underflow” else top = top-1 6. Stop
  • 8. Static implementation of Stack (Example) 8 We can illustrate the concepts above with the help of diagrams. We will accept of size 3. 1. Initially, the stack is empty 0 1 2-1 0 1 2 0 1 2 0 1 2 -1 -1 -1 top top top top 10 20 10 10 2. Push 10 top = top+1=-1+1 = 0 3. Push 20 top = 0+1 = 1 4. Pop top = top-1 = 1-1 = 0 5. Pop top = top-1 = 0-1 = -1
  • 9. Application of Stack 9 1. Reversing a list. 2. Polish Notations. 3. Conversion of an infix to postfix expression. 4. Evaluation of postfix expression. 5. Conversion of an infix to prefix expression. 6. Evaluation of prefix expression. 7. Recursion.
  • 10. Infix Notations & Prefix Notations 10 (i) Infix Notations: When the operators exist between two operands then the expression is called infix expression. Example: The expression to add two numbers A and B is written in infix notation as: A+B (ii) Prefix Notations: When the operators are written before their operands, than the expression is called the prefix notation or prefix polish notation. Example: The expression to add two numbers A and B is written in prefix notation as: +AB (iii) Postfix Notations: When the operators are written after the operands, it is called postfix or suffix notations. It is also known as reverse polish notation. Example: The expression to add two numbers A and B is written in postfix notation as: AB+
  • 11. Notations 11 Infix Expression Prefix Expression Postfix Expression A+B +AB AB+ (A -C)*B *- ACD AC-*B (A +B)/(C - D) /+ AB - CD AB + CD -/ A + (C*D) +A*CD ACD*+
  • 12. Operator Precedence 12 Operator Symbol Precedence Exponential $ Highest Multiplication / Division *,/ Next Highest Addition / Subtraction +,- Lowest
  • 13. Converting Infix to Postfix Expression Algorithm 13 1. Start 2. opstk is an empty stack. 3. ch is the next character from the infix string. 4. If ch is an operand add ch to postfix string. 5. If ch is ‘(’ push ch into opstk 6. If ch is an operator while priority (top-of-stack) > = priority (ch) pop form opstk and add to postfix string. push ch into opstk 7. If ch is ‘)’ ch1 = pop from opstk while (ch1 is not ‘(’ and opstk is not empty) add ch1 to postfix string ch1 = pop (opstk) 8. If infix string has not ended go to Step 3 9. While opstk is not empty. pop ch and add to postfix string 10. Display postfix string. 11. stop
  • 14. Evaluation of Postfix Expression 14 SR Symbol scanned Stack Expression 1 ( ( ~ 2 ( (( ~ 3 ( ((( ~ 4 A ((( A 5 + (((+ A 6 B (((+ AB 7 ) (( AB+ 8 * ((* AB+ 9 D ((* AB + D 10 ) ( AB + D* 11 ( AB + D* 12 ( ( ( AB + D* 13 E ( ( AB + D * E 14 - ( (- AB + D * E 15 F ( (- AB + D * EF 16 ) ( AB + D * EF- 17 ) AB + D * EF- Given Expression: ((A + B) * D) (E - F)
  • 15. 15 SR Symbol scanned Stack Expression 1 ( ( ~ 2 A ( A 3 * ( * A 4 B ( * AB 5 - ( - AB* 6 C ( - AB*C 7 ( - AB*C 8 D ( - AB*CD 9 + ( - + AB*CD 10 E ( - + AB*CD E 11 / ( - +/ AB*CD E 12 F ( - +/ AB*CD EF 13 ) AB*CD EF/ + - Given Expression: (A * B – C D + E/F)
  • 16. 16 SR Symbol scanned Stack 1 6 6 2 2 6,2 3 3 6,2,3 4 + 6,5 5 - 1 6 3 1,3 7 8 1,3,8 8 2 1,2,8,2 9 / 1,3,4 10 + 1,7 11 * 7 12 2 7,2 13 49 14 3 49,3 15 + 52 Given Expression: 6,2,3,+,-,3,8,2,/,+,*,2, ,3,+
  • 17. 17 SR Symbol scanned Stack Expression 1 ) ) ~ 2 ) )) ~ 3 D )) D 4 - ))- D 5 C ))- CD 6 ( ) -CD 7 * )* -CD 8 ) )*) -CD 9 B )*) B – CD 10 + )*)+ B – CD 11 A )*)+ AB – CD 12 ( )* +AB – CD 13 ( - *+AB – CD Example: Convert infix string ((A+B) * (C - D) into prefix string with stack operations show the content of stack in each step.
  • 18. 18 SR Symbol scanned Stack Expression 1 ) ) ~ 2 ) )) ~ 3 F )) F 4 - ))- F 5 E ))- EF 6 * ))-* EF 7 D ))-* DEF 8 ( ) -*DEF 9 * )* -*DEF 10 ) )*) -*DEF 11 C )*) C - *DEF 12 / )*)/ C - *DEF 13 B (*)/ BC - *DEF 14 - )*)- /BC - *DEF 15 A )*)- A/BC - *DEF 16 ( )* -A/BC - *DEF 17 ( *-A/BC - *DEF Example: Convert the given string to prefix exprssio0n and shows the details of stack at each step. (A- B/C) * (D * E - F)
  • 19. Recursion (Definition) 19 Recursion is a process of expressing a fraction. In them of itself in recursion. It is essential for a fraction to call itself, otherwise recursion will not into replace only user depend function can be involved in the recursion library function cannot be involved in recursion became their source code cannot be viewed.
  • 20. Recursion (Example) 20 Write a program to find factorial of number using recursion. /* program to find factorial of given number */ #include <studio.h> #include <conio.h> Voide main() { int fact(int); int num; clrscr(); printf(“Enter the numbern”); scanf(“%d”, & num); printf(“The factorial of %d=%d”,num,fact(num)); getch(); } int fact(int n) { if(n==0) return(1); return(n * fact(n-1)); }
  • 21. Tower of Hanoi (Diagram) 21 A B C