SlideShare a Scribd company logo
1 of 26
Download to read offline
A Brief Introduction to Stacks
Urjit Patel
MNNIT,Allahabad
August 30, 2015
Urjit Patel (MNNIT,Allahabad) Stacks August 30, 2015 1 / 26
Index
What is Stack?
Understanding LIFO
Basic operations used in stack
Implementation of stack using array and linked list
Advantages of stack
Application
Urjit Patel (MNNIT,Allahabad) Stacks August 30, 2015 2 / 26
What is Stack?
Figure: Basic model of stack
In computer science, a stack or LIFO (last in, first out) is an abstract data
type that serves as a collection of elements, with two principal operations:
push, which adds an element to the collection, and pop, which removes the
last element that was added.
It is an ordered group of homogeneous items of elements.
Elements are added to and removed from the top of the stack (the most
recently added items are at the top of the stack). Refer Figure:1.
Urjit Patel (MNNIT,Allahabad) Stacks August 30, 2015 3 / 26
What is Stack? (Contd.)
A stack is a restricted data structure, because only a small number of
operations are performed on it.
The nature of the pop and push operations also means that stack elements
have a natural order.
Elements are removed from the stack in the reverse order to the order of their
addition.
Now we will discuss Stack in detail but before that lets understand the term
LIFO.
Urjit Patel (MNNIT,Allahabad) Stacks August 30, 2015 4 / 26
Understanding LIFO
Stack is also know as LIFO.
The term LIFO stands for LAST IN FIRST OUT.
The basic concept can be illustrated by thinking of your LIFO set as a stack
of plates or books where you can only take the top item off the stack in order
to remove things from it.
Your cannot remove a book placed in between without removing the books
placed on top of it.this is basic principle used behind stacks.
Urjit Patel (MNNIT,Allahabad) Stacks August 30, 2015 5 / 26
Basic operations used in stack
Push
Push inserts an element at top of the stack.
Pop
Pop deletes the element placed at the top of the stack.
isEmpty
isEmpty checks whether the stack is empty or not.
isFull
isFull checks whether the stack is full or not.
Urjit Patel (MNNIT,Allahabad) Stacks August 30, 2015 6 / 26
Implementation
Since stack is a linear list, it can be implemented using arrays or linked lists.
In the next two sections we will study the two implementations of stack.
Urjit Patel (MNNIT,Allahabad) Stacks August 30, 2015 7 / 26
Using Array
Urjit Patel (MNNIT,Allahabad) Stacks August 30, 2015 8 / 26
Push (Using array)
Function-Adds an element at the top of the stack.
Precondition-Stack has been initialized and is not full.
Post-condition-newItem is at the top of the stack.
void push(int item)
{
if isFull () then
{
printf (”StackOverflow n”);
return;
}
end if
top = top + 1;
stack [top] = item;
}
Urjit Patel (MNNIT,Allahabad) Stacks August 30, 2015 9 / 26
Pop (Using array)
Function-Removes topItem from stack and returns it in item.
Precondition-Stack has been initialized and is not empty.
Post-condition-Top element has been removed from stack and item is a
copy of the removed element.
int pop()
{
intitem;
if isEmpty () then
{
printf (”StackUnderflow n”);
exit (1) ;
}
end if
item = stack [arr] ;
top = top − 1;
return item;
}
Urjit Patel (MNNIT,Allahabad) Stacks August 30, 2015 10 / 26
isEmpty (Using array)
Function-Checks whether the stack is empty or not.
Post-condition-returns true if empty or else false.
int isEmpty()
{
if (top == −1) then
return 1;
else
return 0;
end if
}
Urjit Patel (MNNIT,Allahabad) Stacks August 30, 2015 11 / 26
isFull (Using array)
Function-Checks whether the stack is full or not.
Post-condition-returns true if full or else false.
int isFull()
{
if (top == MAX − 1) then
return 1;
else
return 0;
end if
}
Urjit Patel (MNNIT,Allahabad) Stacks August 30, 2015 12 / 26
Review
Stack using array
Urjit Patel (MNNIT,Allahabad) Stacks August 30, 2015 13 / 26
Using Linked Lists
When the size of stack is not known in advance, it is better to implement
linked list.
We will take a single linked list so the structure of the node would be-
struct node {
int info;
struct node *link;
};
Urjit Patel (MNNIT,Allahabad) Stacks August 30, 2015 14 / 26
Push (Using Linked List)
Function-Adds an element at the top of the stack.
Precondition-Stack has been initialized and is not full.
Post-condition-newItem is at the top of the stack.
void push(int item)
{
struct node *tmp;
tmp= (stuct node ∗) malloc (struct node);
if tmp == NULL then
{
printf (”StackOverflow n”);
return;
}
end if
tmp → info = item;
tmp → link = top;
top = tmp;
}
Urjit Patel (MNNIT,Allahabad) Stacks August 30, 2015 15 / 26
Pop (Using Linked List)
Function-Removes topItem from stack and returns it in item.
Precondition-Stack has been initialized and is not empty.
Post-condition-Top element has been removed from stack and item is a
copy of the removed element.
int pop()
{
struct node *tmp;
if isEmpty () then
{
printf (”Stack Underflow n”);
exit (1)
}
end if
tmp = top;
item = tmp → info;
top = tmp → link;
free (tmp) ;
return item;Urjit Patel (MNNIT,Allahabad) Stacks August 30, 2015 16 / 26
isEmpty (Using Linked List)
Function-Checks whether the stack is empty or not.
Post-condition-returns true if empty or else false.
int isEmpty()
{
if (top == NULL) then
return 1;
else
return 0;
end if
}
Urjit Patel (MNNIT,Allahabad) Stacks August 30, 2015 17 / 26
Array vs Linked List
Array Linked List
Accessing
Stack elements
can be randomly
Accessed using
Subscript Variable
While In Linked List
We have to Traverse
Through the Linked
List for Accessing El-
ement.
Memory Structure
The stack elements
are stored at con-
tiguous memory.
The stack elements
are stored at dif-
ferent memory lo-
cations but joined
through pointers.
Insertion deletion
As the Array ele-
ments are stored in
Consecutive memory
Locations so While
Inserting elements
we have to create
space for Insertion.
While in linked list
one has to only
change the pointers.
Urjit Patel (MNNIT,Allahabad) Stacks August 30, 2015 18 / 26
Array vs Linked List (Contd.)
Array Linked List
Memory Allocation
Memory Should
be allocated at
Compile-Time in ar-
ray . i.e at the time
when Programmer
is Writing Program.
In Linked list mem-
ory can be allocated
at Run-Time , i.e
After executing Pro-
gram
Allocation type
Stack uses Static
Memory Allocation
While linked list uses
dynamic memory al-
location
Urjit Patel (MNNIT,Allahabad) Stacks August 30, 2015 19 / 26
Application of stack
The various application are
Reversing of string
Conversion of infix forms to postfix forms.
Check well-formed paranthesis.
Used in compilers to check the syntax whether is it correct or not.
We shall discuss some applications now.
Urjit Patel (MNNIT,Allahabad) Stacks August 30, 2015 20 / 26
Reverse a string using stack
The basic procedure is simple.
First push each character of string into the stack.
Then pop each character out of the stack.
The resultant would be the reversed string.
Urjit Patel (MNNIT,Allahabad) Stacks August 30, 2015 21 / 26
Pseudo Code for reversal
main()
{
char str[20];
int i;
printf(”Enter string:”);
gets(str);
while i < strlen (str) do
push(str[i]);
i++;
end while
while i < strlen (str) do
str[i]=pop();
i++;
end while
printf(”The reversed string is”);
puts(str);
Urjit Patel (MNNIT,Allahabad) Stacks August 30, 2015 22 / 26
Evaluation of postfix expression using stack
The steps involved to evaluate the postfix expression are-
Scan the symbol of array postfix one by one from left to right
If symbol is operand , we push it in stack.
If the symbol is operator , we pop two elements from stack and apply the
operator to these two elements.
push the result on the stack.
Lets understand with an example.
Example:-3 ∗ (4 + 5)/2 → 345 + ∗2 → 13
Remaining postfix string int stack(top)
345 + ∗2/ empty
45 + ∗2/ 3
5 + ∗2/ 3 4
+ ∗ 2/ 3 4 5
*2/ 3 9
2/ 27
/ 27 2
NULL 13 ← ans
Urjit Patel (MNNIT,Allahabad) Stacks August 30, 2015 23 / 26
Pseudo Code for evaluating post expressions
long int evalpost()
{
long int a,b,temp,result;
unsigned int i;
while i < strlen (postfix) do
if postfix [i] ≤ 9 then
push (postfix [i] − 0 );
else
a=pop();
b=pop();
switch(postfix[i])
case + : temp = b + a; break;
case − : temp = b − a; break;
case ∗ : temp = b ∗ a; break;
case / : temp = b/a; break;
case % : temp = b%a; break;
case : temp = pow(a, b); break;
}
end ifUrjit Patel (MNNIT,Allahabad) Stacks August 30, 2015 24 / 26
Pseudo Code for evaluating post expressions
}
push(temp);
}
}
result=pop();
return result;
Urjit Patel (MNNIT,Allahabad) Stacks August 30, 2015 25 / 26
The End
Urjit Patel (MNNIT,Allahabad) Stacks August 30, 2015 26 / 26

More Related Content

What's hot

STACK ( LIFO STRUCTURE) - Data Structure
STACK ( LIFO STRUCTURE) - Data StructureSTACK ( LIFO STRUCTURE) - Data Structure
STACK ( LIFO STRUCTURE) - Data StructureYaksh Jethva
 
computer notes - List implementation
computer notes - List implementationcomputer notes - List implementation
computer notes - List implementationecomputernotes
 
ICTCS 2015 - Exploiting linearity in sharing analysis of object-oriented prog...
ICTCS 2015 - Exploiting linearity in sharing analysis of object-oriented prog...ICTCS 2015 - Exploiting linearity in sharing analysis of object-oriented prog...
ICTCS 2015 - Exploiting linearity in sharing analysis of object-oriented prog...Gianluca Amato
 
Analysis of algorithms
Analysis of algorithmsAnalysis of algorithms
Analysis of algorithmsiqbalphy1
 
Searching techniques with progrms
Searching techniques with progrmsSearching techniques with progrms
Searching techniques with progrmsMisssaxena
 
Data structures &amp; algorithms
Data structures &amp; algorithmsData structures &amp; algorithms
Data structures &amp; algorithmsPrabhu R
 
Purely functional data structures demystified
Purely functional data structures demystifiedPurely functional data structures demystified
Purely functional data structures demystifiedMohit Thatte
 
Simple Linear Regression with R
Simple Linear Regression with RSimple Linear Regression with R
Simple Linear Regression with RJerome Gomes
 
Module System in Standard ML
Module System in Standard MLModule System in Standard ML
Module System in Standard MLKeheliya Gallaba
 
Comparative Performance Analysis & Complexity of Different Sorting Algorithm
Comparative Performance Analysis & Complexity of Different Sorting AlgorithmComparative Performance Analysis & Complexity of Different Sorting Algorithm
Comparative Performance Analysis & Complexity of Different Sorting Algorithmpaperpublications3
 

What's hot (14)

STACK ( LIFO STRUCTURE) - Data Structure
STACK ( LIFO STRUCTURE) - Data StructureSTACK ( LIFO STRUCTURE) - Data Structure
STACK ( LIFO STRUCTURE) - Data Structure
 
Stacks and queues
Stacks and queuesStacks and queues
Stacks and queues
 
computer notes - List implementation
computer notes - List implementationcomputer notes - List implementation
computer notes - List implementation
 
ICTCS 2015 - Exploiting linearity in sharing analysis of object-oriented prog...
ICTCS 2015 - Exploiting linearity in sharing analysis of object-oriented prog...ICTCS 2015 - Exploiting linearity in sharing analysis of object-oriented prog...
ICTCS 2015 - Exploiting linearity in sharing analysis of object-oriented prog...
 
Data structures
Data structuresData structures
Data structures
 
Analysis of algorithms
Analysis of algorithmsAnalysis of algorithms
Analysis of algorithms
 
Excel formula
Excel formulaExcel formula
Excel formula
 
Searching techniques with progrms
Searching techniques with progrmsSearching techniques with progrms
Searching techniques with progrms
 
Stacks
StacksStacks
Stacks
 
Data structures &amp; algorithms
Data structures &amp; algorithmsData structures &amp; algorithms
Data structures &amp; algorithms
 
Purely functional data structures demystified
Purely functional data structures demystifiedPurely functional data structures demystified
Purely functional data structures demystified
 
Simple Linear Regression with R
Simple Linear Regression with RSimple Linear Regression with R
Simple Linear Regression with R
 
Module System in Standard ML
Module System in Standard MLModule System in Standard ML
Module System in Standard ML
 
Comparative Performance Analysis & Complexity of Different Sorting Algorithm
Comparative Performance Analysis & Complexity of Different Sorting AlgorithmComparative Performance Analysis & Complexity of Different Sorting Algorithm
Comparative Performance Analysis & Complexity of Different Sorting Algorithm
 

Similar to Stacks

VCE Unit 03vv.pptx
VCE Unit 03vv.pptxVCE Unit 03vv.pptx
VCE Unit 03vv.pptxskilljiolms
 
unit 5 stack & queue.ppt
unit 5 stack & queue.pptunit 5 stack & queue.ppt
unit 5 stack & queue.pptSeethaDinesh
 
Difference between stack and queue
Difference between stack and queueDifference between stack and queue
Difference between stack and queuePulkitmodi1998
 
Module 2 ppt.pptx
Module 2 ppt.pptxModule 2 ppt.pptx
Module 2 ppt.pptxSonaPathak4
 
What is Stack, Its Operations, Queue, Circular Queue, Priority Queue
What is Stack, Its Operations, Queue, Circular Queue, Priority QueueWhat is Stack, Its Operations, Queue, Circular Queue, Priority Queue
What is Stack, Its Operations, Queue, Circular Queue, Priority QueueBalwant Gorad
 
Data Structure.pptx
Data Structure.pptxData Structure.pptx
Data Structure.pptxSajalFayyaz
 
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,queueRai University
 
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,queueRai University
 
Mca ii dfs u-3 linklist,stack,queue
Mca ii dfs u-3 linklist,stack,queueMca ii dfs u-3 linklist,stack,queue
Mca ii dfs u-3 linklist,stack,queueRai University
 
STACK AND QUEUE CIRCULAR QUEUE PPTS.pptx
STACK AND QUEUE CIRCULAR QUEUE PPTS.pptxSTACK AND QUEUE CIRCULAR QUEUE PPTS.pptx
STACK AND QUEUE CIRCULAR QUEUE PPTS.pptxsunitha1792
 
Collections lecture 35 40
Collections lecture 35 40Collections lecture 35 40
Collections lecture 35 40bhawna sharma
 
STACK, LINKED LIST ,AND QUEUE
STACK, LINKED LIST ,AND QUEUESTACK, LINKED LIST ,AND QUEUE
STACK, LINKED LIST ,AND QUEUEDev Chauhan
 

Similar to Stacks (20)

VCE Unit 03vv.pptx
VCE Unit 03vv.pptxVCE Unit 03vv.pptx
VCE Unit 03vv.pptx
 
DS UNIT 1.pdf
DS UNIT 1.pdfDS UNIT 1.pdf
DS UNIT 1.pdf
 
DS UNIT 1.pdf
DS UNIT 1.pdfDS UNIT 1.pdf
DS UNIT 1.pdf
 
unit 5 stack & queue.ppt
unit 5 stack & queue.pptunit 5 stack & queue.ppt
unit 5 stack & queue.ppt
 
Stack and queue
Stack and queueStack and queue
Stack and queue
 
Difference between stack and queue
Difference between stack and queueDifference between stack and queue
Difference between stack and queue
 
Data structures1
Data structures1Data structures1
Data structures1
 
Stack and its operations
Stack and its operationsStack and its operations
Stack and its operations
 
stacks and queues
stacks and queuesstacks and queues
stacks and queues
 
Module 2 ppt.pptx
Module 2 ppt.pptxModule 2 ppt.pptx
Module 2 ppt.pptx
 
What is Stack, Its Operations, Queue, Circular Queue, Priority Queue
What is Stack, Its Operations, Queue, Circular Queue, Priority QueueWhat is Stack, Its Operations, Queue, Circular Queue, Priority Queue
What is Stack, Its Operations, Queue, Circular Queue, Priority Queue
 
Basic data-structures-v.1.1
Basic data-structures-v.1.1Basic data-structures-v.1.1
Basic data-structures-v.1.1
 
Data Structure.pptx
Data Structure.pptxData Structure.pptx
Data Structure.pptx
 
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
 
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
 
Mca ii dfs u-3 linklist,stack,queue
Mca ii dfs u-3 linklist,stack,queueMca ii dfs u-3 linklist,stack,queue
Mca ii dfs u-3 linklist,stack,queue
 
STACK AND QUEUE CIRCULAR QUEUE PPTS.pptx
STACK AND QUEUE CIRCULAR QUEUE PPTS.pptxSTACK AND QUEUE CIRCULAR QUEUE PPTS.pptx
STACK AND QUEUE CIRCULAR QUEUE PPTS.pptx
 
Collections lecture 35 40
Collections lecture 35 40Collections lecture 35 40
Collections lecture 35 40
 
STACK, LINKED LIST ,AND QUEUE
STACK, LINKED LIST ,AND QUEUESTACK, LINKED LIST ,AND QUEUE
STACK, LINKED LIST ,AND QUEUE
 
Stack
StackStack
Stack
 

More from Shivam Singh

More from Shivam Singh (7)

Trees
TreesTrees
Trees
 
What If Microsoft sold diapers!? MS Diapers!
What If Microsoft sold diapers!? MS Diapers!What If Microsoft sold diapers!? MS Diapers!
What If Microsoft sold diapers!? MS Diapers!
 
Search Algprithms
Search AlgprithmsSearch Algprithms
Search Algprithms
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search Tree
 
Graph Theory
Graph TheoryGraph Theory
Graph Theory
 
Linked List
Linked ListLinked List
Linked List
 
Sorting Algorithms
Sorting AlgorithmsSorting Algorithms
Sorting Algorithms
 

Recently uploaded

CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfCCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfAsst.prof M.Gokilavani
 
IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024Mark Billinghurst
 
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
 
power system scada applications and uses
power system scada applications and usespower system scada applications and uses
power system scada applications and usesDevarapalliHaritha
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVRajaP95
 
Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxpurnimasatapathy1234
 
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...Soham Mondal
 
GDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSCAESB
 
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerStudy on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerAnamika Sarkar
 
Call Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile serviceCall Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile servicerehmti665
 
Heart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptxHeart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptxPoojaBan
 
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionSachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionDr.Costas Sachpazis
 
HARMONY IN THE HUMAN BEING - Unit-II UHV-2
HARMONY IN THE HUMAN BEING - Unit-II UHV-2HARMONY IN THE HUMAN BEING - Unit-II UHV-2
HARMONY IN THE HUMAN BEING - Unit-II UHV-2RajaP95
 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130Suhani Kapoor
 
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
 
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escortsranjana rawat
 
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSAPPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSKurinjimalarL3
 

Recently uploaded (20)

CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfCCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
 
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
 
IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024
 
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
 
power system scada applications and uses
power system scada applications and usespower system scada applications and uses
power system scada applications and uses
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
 
Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptx
 
🔝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...
 
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
 
GDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentation
 
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerStudy on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
 
Call Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile serviceCall Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile service
 
Heart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptxHeart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptx
 
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionSachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
 
HARMONY IN THE HUMAN BEING - Unit-II UHV-2
HARMONY IN THE HUMAN BEING - Unit-II UHV-2HARMONY IN THE HUMAN BEING - Unit-II UHV-2
HARMONY IN THE HUMAN BEING - Unit-II UHV-2
 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
 
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
 
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
 
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSAPPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
 

Stacks

  • 1. A Brief Introduction to Stacks Urjit Patel MNNIT,Allahabad August 30, 2015 Urjit Patel (MNNIT,Allahabad) Stacks August 30, 2015 1 / 26
  • 2. Index What is Stack? Understanding LIFO Basic operations used in stack Implementation of stack using array and linked list Advantages of stack Application Urjit Patel (MNNIT,Allahabad) Stacks August 30, 2015 2 / 26
  • 3. What is Stack? Figure: Basic model of stack In computer science, a stack or LIFO (last in, first out) is an abstract data type that serves as a collection of elements, with two principal operations: push, which adds an element to the collection, and pop, which removes the last element that was added. It is an ordered group of homogeneous items of elements. Elements are added to and removed from the top of the stack (the most recently added items are at the top of the stack). Refer Figure:1. Urjit Patel (MNNIT,Allahabad) Stacks August 30, 2015 3 / 26
  • 4. What is Stack? (Contd.) A stack is a restricted data structure, because only a small number of operations are performed on it. The nature of the pop and push operations also means that stack elements have a natural order. Elements are removed from the stack in the reverse order to the order of their addition. Now we will discuss Stack in detail but before that lets understand the term LIFO. Urjit Patel (MNNIT,Allahabad) Stacks August 30, 2015 4 / 26
  • 5. Understanding LIFO Stack is also know as LIFO. The term LIFO stands for LAST IN FIRST OUT. The basic concept can be illustrated by thinking of your LIFO set as a stack of plates or books where you can only take the top item off the stack in order to remove things from it. Your cannot remove a book placed in between without removing the books placed on top of it.this is basic principle used behind stacks. Urjit Patel (MNNIT,Allahabad) Stacks August 30, 2015 5 / 26
  • 6. Basic operations used in stack Push Push inserts an element at top of the stack. Pop Pop deletes the element placed at the top of the stack. isEmpty isEmpty checks whether the stack is empty or not. isFull isFull checks whether the stack is full or not. Urjit Patel (MNNIT,Allahabad) Stacks August 30, 2015 6 / 26
  • 7. Implementation Since stack is a linear list, it can be implemented using arrays or linked lists. In the next two sections we will study the two implementations of stack. Urjit Patel (MNNIT,Allahabad) Stacks August 30, 2015 7 / 26
  • 8. Using Array Urjit Patel (MNNIT,Allahabad) Stacks August 30, 2015 8 / 26
  • 9. Push (Using array) Function-Adds an element at the top of the stack. Precondition-Stack has been initialized and is not full. Post-condition-newItem is at the top of the stack. void push(int item) { if isFull () then { printf (”StackOverflow n”); return; } end if top = top + 1; stack [top] = item; } Urjit Patel (MNNIT,Allahabad) Stacks August 30, 2015 9 / 26
  • 10. Pop (Using array) Function-Removes topItem from stack and returns it in item. Precondition-Stack has been initialized and is not empty. Post-condition-Top element has been removed from stack and item is a copy of the removed element. int pop() { intitem; if isEmpty () then { printf (”StackUnderflow n”); exit (1) ; } end if item = stack [arr] ; top = top − 1; return item; } Urjit Patel (MNNIT,Allahabad) Stacks August 30, 2015 10 / 26
  • 11. isEmpty (Using array) Function-Checks whether the stack is empty or not. Post-condition-returns true if empty or else false. int isEmpty() { if (top == −1) then return 1; else return 0; end if } Urjit Patel (MNNIT,Allahabad) Stacks August 30, 2015 11 / 26
  • 12. isFull (Using array) Function-Checks whether the stack is full or not. Post-condition-returns true if full or else false. int isFull() { if (top == MAX − 1) then return 1; else return 0; end if } Urjit Patel (MNNIT,Allahabad) Stacks August 30, 2015 12 / 26
  • 13. Review Stack using array Urjit Patel (MNNIT,Allahabad) Stacks August 30, 2015 13 / 26
  • 14. Using Linked Lists When the size of stack is not known in advance, it is better to implement linked list. We will take a single linked list so the structure of the node would be- struct node { int info; struct node *link; }; Urjit Patel (MNNIT,Allahabad) Stacks August 30, 2015 14 / 26
  • 15. Push (Using Linked List) Function-Adds an element at the top of the stack. Precondition-Stack has been initialized and is not full. Post-condition-newItem is at the top of the stack. void push(int item) { struct node *tmp; tmp= (stuct node ∗) malloc (struct node); if tmp == NULL then { printf (”StackOverflow n”); return; } end if tmp → info = item; tmp → link = top; top = tmp; } Urjit Patel (MNNIT,Allahabad) Stacks August 30, 2015 15 / 26
  • 16. Pop (Using Linked List) Function-Removes topItem from stack and returns it in item. Precondition-Stack has been initialized and is not empty. Post-condition-Top element has been removed from stack and item is a copy of the removed element. int pop() { struct node *tmp; if isEmpty () then { printf (”Stack Underflow n”); exit (1) } end if tmp = top; item = tmp → info; top = tmp → link; free (tmp) ; return item;Urjit Patel (MNNIT,Allahabad) Stacks August 30, 2015 16 / 26
  • 17. isEmpty (Using Linked List) Function-Checks whether the stack is empty or not. Post-condition-returns true if empty or else false. int isEmpty() { if (top == NULL) then return 1; else return 0; end if } Urjit Patel (MNNIT,Allahabad) Stacks August 30, 2015 17 / 26
  • 18. Array vs Linked List Array Linked List Accessing Stack elements can be randomly Accessed using Subscript Variable While In Linked List We have to Traverse Through the Linked List for Accessing El- ement. Memory Structure The stack elements are stored at con- tiguous memory. The stack elements are stored at dif- ferent memory lo- cations but joined through pointers. Insertion deletion As the Array ele- ments are stored in Consecutive memory Locations so While Inserting elements we have to create space for Insertion. While in linked list one has to only change the pointers. Urjit Patel (MNNIT,Allahabad) Stacks August 30, 2015 18 / 26
  • 19. Array vs Linked List (Contd.) Array Linked List Memory Allocation Memory Should be allocated at Compile-Time in ar- ray . i.e at the time when Programmer is Writing Program. In Linked list mem- ory can be allocated at Run-Time , i.e After executing Pro- gram Allocation type Stack uses Static Memory Allocation While linked list uses dynamic memory al- location Urjit Patel (MNNIT,Allahabad) Stacks August 30, 2015 19 / 26
  • 20. Application of stack The various application are Reversing of string Conversion of infix forms to postfix forms. Check well-formed paranthesis. Used in compilers to check the syntax whether is it correct or not. We shall discuss some applications now. Urjit Patel (MNNIT,Allahabad) Stacks August 30, 2015 20 / 26
  • 21. Reverse a string using stack The basic procedure is simple. First push each character of string into the stack. Then pop each character out of the stack. The resultant would be the reversed string. Urjit Patel (MNNIT,Allahabad) Stacks August 30, 2015 21 / 26
  • 22. Pseudo Code for reversal main() { char str[20]; int i; printf(”Enter string:”); gets(str); while i < strlen (str) do push(str[i]); i++; end while while i < strlen (str) do str[i]=pop(); i++; end while printf(”The reversed string is”); puts(str); Urjit Patel (MNNIT,Allahabad) Stacks August 30, 2015 22 / 26
  • 23. Evaluation of postfix expression using stack The steps involved to evaluate the postfix expression are- Scan the symbol of array postfix one by one from left to right If symbol is operand , we push it in stack. If the symbol is operator , we pop two elements from stack and apply the operator to these two elements. push the result on the stack. Lets understand with an example. Example:-3 ∗ (4 + 5)/2 → 345 + ∗2 → 13 Remaining postfix string int stack(top) 345 + ∗2/ empty 45 + ∗2/ 3 5 + ∗2/ 3 4 + ∗ 2/ 3 4 5 *2/ 3 9 2/ 27 / 27 2 NULL 13 ← ans Urjit Patel (MNNIT,Allahabad) Stacks August 30, 2015 23 / 26
  • 24. Pseudo Code for evaluating post expressions long int evalpost() { long int a,b,temp,result; unsigned int i; while i < strlen (postfix) do if postfix [i] ≤ 9 then push (postfix [i] − 0 ); else a=pop(); b=pop(); switch(postfix[i]) case + : temp = b + a; break; case − : temp = b − a; break; case ∗ : temp = b ∗ a; break; case / : temp = b/a; break; case % : temp = b%a; break; case : temp = pow(a, b); break; } end ifUrjit Patel (MNNIT,Allahabad) Stacks August 30, 2015 24 / 26
  • 25. Pseudo Code for evaluating post expressions } push(temp); } } result=pop(); return result; Urjit Patel (MNNIT,Allahabad) Stacks August 30, 2015 25 / 26
  • 26. The End Urjit Patel (MNNIT,Allahabad) Stacks August 30, 2015 26 / 26