SlideShare a Scribd company logo
1 of 89
UNIT III
Stack
• 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).
• The last element to be added is the first to be removed
(LIFO: Last In, First Out).
• A stack is a non primitive linear data structure in
which addition of new element or deletion of
existing element takes place at the same end,
known as top of the stack.
• The stack is also called as Pushdown list.
• Stack is an ordered list of similar data type.
Operations on stack
• The operation of insertion of an element into the
stack is called Push operation. The operation of
removing an element from the stack is called Pop
operation. Data is stored and retrieved in a Last In
First Out basis. It is called the LIFO principle.
That is the element which is inserted last will be
deleted first.
• Stack is said to be in Overflow state when it is
completely full and is said to be in Underflow state
if it is completely empty.
Stack implementation
A stack is a simple linear data structure. It can be
implemented in the following ways.
i) Static implementation ii) Dynamic Implementation
Static Implementation
Static implementation can be achieved by using arrays.
Limitations:
Once a size of the array is declared, it cannot be modified
during program execution.
If the reserved memory is more, then the memory will be
wasted.
Therefore this method is suitable only when we know exactly
the number of elements to be stored..
Dynamic implementation
Pointers can be used to create stack. There is no
restriction on the number of elements.
It is possible to increase the size of the stack and
memory is utilized efficiently with the use of pointers.
Creating a stack
#define size 5
int top=-1;
int stk[size];
// declare this globally so that all functions can
access.
#include<stdio.h>
#include<conio.h>
#define size 5
void push();
int pop();
void display();
int top=-1,stk[size];
void main()
{
int ch,item;
clrscr();
while(1)
{
printf("n1.Pushn");
printf("2.Popn");
printf("3.Displayn");
printf("4.Exitn");
printf("Enter ur choicen");
scanf("%d",&ch);
switch(ch)
{
case 1:
push();
break;
case 2: item=pop();
if(item!=-1)
printf("Popped element=%dn",item);
break;
case 3:
display();
break;
case 4:
exit(0);
default: printf("Invalid choicen");
}
} }
void push( )
{
if(top==(size-1))
printf("Stack full...Stack Overflown");
else
{
printf("Enter the elementn");
scanf("%d",&item);
top++;
stk[top]=item;
}
}
Display()
void display()
{
int i;
i=top;
printf("Stack Contentsn");
if(i= =(-1))
printf("Empty stack.....Stack Underflow.....n");
else
{
while(i!=(-1))
{
printf("%dt",stk[i]);
i--; } } }
int pop()
{
int item;
if(top==(-1))
{
printf("Empty Stack.....Stack underflow....n");
return(-1);
}
else
{
item=stk[top];
top--;
return(item);
}
}
Applications
The simplest application of a stack is to reverse a word.
You push a given word to stack - letter by letter - and
then pop letters from the stack.
Another application is an "undo" mechanism in text
editors; this operation is accomplished by keeping all
text changes in a stack.
Backtracking:
Consider a simple example of finding the correct path in a
maze. There are a series of points, from the starting point
to the destination. We start from one point. To reach the
final destination, there are several paths. Suppose we
choose a random path. After following a certain path, we
realise that the path we have chosen is wrong. So we
This can be done with the use of stacks. With the help
of stacks, we remember the point where we have
reached. This is done by pushing that point into the
stack. In case we end up on the wrong path, we can pop
the last point from the stack and thus return to the last
point and continue our quest to find the right path.
This is called backtracking
What is a queue?
• It is an ordered group of homogeneous items of
elements.
• Queues have two ends:
– Elements are added at one end.
– Elements are removed from the other end.
• The element added first is also removed first
(FIFO: First In, First Out).
Operations on Queue
a. Inserting an element into queue
b. Deleting an element from queue
c. Checking the status whether queue is empty
d. Checking the status whether queue is full
e. Displaying the elements of queue
• The first element inserted into a queue is the first
element to be removed. For this reason queue is
referred as first-in-first-out (FIFO) list.
/* Program to simulate the working of a Linear Queue
using an array*/
#include<stdio.h>
#include<conio.h>
void qinsert();
void qdelete();
void qdisplay();
int queue[10],front=0,rear=-1;
int max=5;
void main()
{
int ch;
do
{ printf("nLINEAR QUEUE OPERATIONS n");
printf("1.Insert n");
printf("2.Delete n");
printf("3.Display n");
printf("4.Exit n");
printf("Enter your choicen");
scanf("%d",&ch);
switch(ch)
{
case 1:qinsert();
break;
case 2:qdelete(); break;
case 3:qdisplay();
break;
case 4:exit(0);
default:printf("n WRONG CHOICE");
}
}
while(ch!=4);
}
void qinsert()
{
int item;
if(rear==max-1)
printf("Queue is full");
else
{
printf("Enter the value to insertn");
scanf("%d",&item);
rear++;
queue[rear]=item;
} }
void qdelete()
{
int item;
if(front==rear)
{
front=rear=-1
printf("Queue is empty");
}
else
{
item=queue[front];
printf("%d is deletedn",item);
void qdisplay()
{
int item;
int p=front;
if(front==rear)
{
front=rear=-1
printf("Queue is empty");
}
else
{ printf("nQueue Elements");
while(p<=rear)
{
printf("%dt",queue[p]);
p++; } } }
Queue empty and Queue full Conditions
int qempty()
{
if(front == rear+1)
return 1;
else
return 0;
}
int qfull( )
{
if(rear = = max-1)
return 1;
else
return 0;
}
09/10/08 33
Example: Consider the following queue (linear queue).
Rear = 4 and Front = 1 and N = 7
10 50 30 40
(1) Insert 20. Now Rear = 5 and Front = 1
1 2 3 4 5 6 7
10 50 30 40 20
1 2 3 4 5 6 7
(2) Delete Front Element. Now Rear = 5 and Front = 2
50 30 40 20
1 2 3 4 5 6 7
(3) Delete Front Element. Now Rear = 5 and Front = 3
30 40 20
1 2 3 4 5 6 7
(4) Insert 60. Now Rear = 6 and Front = 3
30 40 20 60
1 2 3 4 5 6 7
Circular Queue
Circular queue is a linear data structure. It follows FIFO
principle.
In circular queue the last node is connected back to the
first node to make a circle.
Circular linked list fallow the First In First Out principle
Elements are added at the rear end and the elements are
deleted at front end of the queue
Both the front and the rear pointers points to the
beginning of the array.
It is also called as “Ring buffer”.
35
Example: Consider the following circular queue with N = 5.
1. Initially, Rear = 0, Front = 0.
2. Insert 10, Rear = 1, Front = 1.
3. Insert 50, Rear = 2, Front = 1.
4. Insert 20, Rear = 3, Front = 0.
5. Insert 70, Rear = 4, Front = 1.
6. Delete front, Rear = 4, Front = 2.
Rear
Rear
Rear
Rear
Rear
Front
Front
Front
Front
Front
36
7. Insert 100, Rear = 5, Front = 2.
8. Insert 40, Rear = 1, Front = 2.
9. Insert 140, Rear = 1, Front = 2.
As Front = Rear + 1, so Queue overflow.
10. Delete front, Rear = 1, Front = 3.
Front
Rear
Front
Rear
Rear
Rear
Front
Front
11. Delete front, Rear = 1, Front = 4.
12. Delete front, Rear = 1, Front = 5.
Rear
Rear
Front
Front
Double-Ended Queue
• A double-ended queue is an abstract data type similar
to an simple queue, it allows you to insert and delete
from both sides means items can be added or deleted
from the front or rear end.
There are two variations of a deque namely,
1. Input – restricted deque
2. Output – restricted deque
The input – restricted deque allows insertions at only one
end.
While an output – restricted deque permits deletions
from only at one end.
void dqdeletef()
{
if((front==-1)&&(rear==-1))
printf("Dequeue underflown");
else
{
printf("%d is deletedn",dq[front]);
if(front==rear)
front=rear=-1;
else
front=(front+1)%n;
}
void dqdeleter()
{
if((front==-1)&&(rear==-1))
printf("Dequeue underflown");
else
{
printf("%d is deletedn",dq[rear]);
if(front==rear)
front=rear=-1;
else
if(rear==0) rear=n-1;
else rear=(rear-1)%n;
} }
void dqinsertr()
{
int item;
if(front==(rear+1)%n)
printf("Dequeue overflown");
else
{
printf("Enter the value to insertn");
scanf("%d",&item);
if(front==-1) front=rear=0;
else
rear=(rear+1)%n; dq[rear]=item;
} }
void dqinsertf()
{
int item;
if(front==(rear+1)%n)
printf("Dequeue overflown");
else
{
printf("Enter the value to insertn");
scanf("%d",&item);
if(front==-1) front=rear=0;
else if(front==0) front=n-1;
else front=(front-1)%n;
dq[front]=item; } }
Priority Queue
A priority queue is different from a "normal" queue,
because instead of being a "first-in-first-out" data
structure, values come out in order by priority.
Rules
1.Element of higher priority is processed before any
element of lower priority.
2.Two elements with same priority are processed
according to the order in which they are added in to the
queue.
ASCENDING-->
Items are entered arbitrarily & only the smallest item may
be removed.
DESCENDING-->
Items are entered arbitrarily & only the largest item may
be removed. A descending priority queue is similar but
allows deletion of only the largest item.
FIFO Queue and LIFO Stack Data Structures Explained
FIFO Queue and LIFO Stack Data Structures Explained
FIFO Queue and LIFO Stack Data Structures Explained
FIFO Queue and LIFO Stack Data Structures Explained
FIFO Queue and LIFO Stack Data Structures Explained
FIFO Queue and LIFO Stack Data Structures Explained
FIFO Queue and LIFO Stack Data Structures Explained
FIFO Queue and LIFO Stack Data Structures Explained
FIFO Queue and LIFO Stack Data Structures Explained
FIFO Queue and LIFO Stack Data Structures Explained
FIFO Queue and LIFO Stack Data Structures Explained
FIFO Queue and LIFO Stack Data Structures Explained
FIFO Queue and LIFO Stack Data Structures Explained
FIFO Queue and LIFO Stack Data Structures Explained
FIFO Queue and LIFO Stack Data Structures Explained
FIFO Queue and LIFO Stack Data Structures Explained
FIFO Queue and LIFO Stack Data Structures Explained
FIFO Queue and LIFO Stack Data Structures Explained
FIFO Queue and LIFO Stack Data Structures Explained
FIFO Queue and LIFO Stack Data Structures Explained
FIFO Queue and LIFO Stack Data Structures Explained
FIFO Queue and LIFO Stack Data Structures Explained
FIFO Queue and LIFO Stack Data Structures Explained
FIFO Queue and LIFO Stack Data Structures Explained
FIFO Queue and LIFO Stack Data Structures Explained
FIFO Queue and LIFO Stack Data Structures Explained
FIFO Queue and LIFO Stack Data Structures Explained
FIFO Queue and LIFO Stack Data Structures Explained
FIFO Queue and LIFO Stack Data Structures Explained
FIFO Queue and LIFO Stack Data Structures Explained
FIFO Queue and LIFO Stack Data Structures Explained
FIFO Queue and LIFO Stack Data Structures Explained
FIFO Queue and LIFO Stack Data Structures Explained
FIFO Queue and LIFO Stack Data Structures Explained
FIFO Queue and LIFO Stack Data Structures Explained
FIFO Queue and LIFO Stack Data Structures Explained
FIFO Queue and LIFO Stack Data Structures Explained
FIFO Queue and LIFO Stack Data Structures Explained
FIFO Queue and LIFO Stack Data Structures Explained
FIFO Queue and LIFO Stack Data Structures Explained
FIFO Queue and LIFO Stack Data Structures Explained
FIFO Queue and LIFO Stack Data Structures Explained

More Related Content

Similar to FIFO Queue and LIFO Stack Data Structures Explained

Similar to FIFO Queue and LIFO Stack Data Structures Explained (20)

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.pptx university exam preparation
Stack and Queue.pptx university exam preparationStack and Queue.pptx university exam preparation
Stack and Queue.pptx university exam preparation
 
Queue
QueueQueue
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
 
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
 
@Chapter 4 DSA Part II.pptx
@Chapter 4 DSA Part II.pptx@Chapter 4 DSA Part II.pptx
@Chapter 4 DSA Part II.pptx
 
Stacks, Queues, Deques
Stacks, Queues, DequesStacks, Queues, Deques
Stacks, Queues, Deques
 
Stack & Queue
Stack & QueueStack & Queue
Stack & Queue
 
Queue
QueueQueue
Queue
 
My lectures circular queue
My lectures circular queueMy lectures circular queue
My lectures circular queue
 
CEN 235 4. Abstract Data Types - Queue and Stack.pdf
CEN 235 4. Abstract Data Types - Queue and Stack.pdfCEN 235 4. Abstract Data Types - Queue and Stack.pdf
CEN 235 4. Abstract Data Types - Queue and Stack.pdf
 
Queue
QueueQueue
Queue
 
Data structure , stack , queue
Data structure , stack , queueData structure , stack , queue
Data structure , stack , queue
 
LEC4-DS ALGO.pdf
LEC4-DS  ALGO.pdfLEC4-DS  ALGO.pdf
LEC4-DS ALGO.pdf
 
23 stacks-queues-deques
23 stacks-queues-deques23 stacks-queues-deques
23 stacks-queues-deques
 
Queues
Queues Queues
Queues
 
Stack and queue
Stack and queueStack and queue
Stack and queue
 
Queue
QueueQueue
Queue
 
Stack and queue
Stack and queueStack and queue
Stack and queue
 
Module 2 ppt.pptx
Module 2 ppt.pptxModule 2 ppt.pptx
Module 2 ppt.pptx
 

More from SherinRappai

COMPUTING AND PROGRAMMING FUNDAMENTAL.pptx
COMPUTING AND PROGRAMMING FUNDAMENTAL.pptxCOMPUTING AND PROGRAMMING FUNDAMENTAL.pptx
COMPUTING AND PROGRAMMING FUNDAMENTAL.pptxSherinRappai
 
neuralnetwork.pptx
neuralnetwork.pptxneuralnetwork.pptx
neuralnetwork.pptxSherinRappai
 
Rendering Algorithms.pptx
Rendering Algorithms.pptxRendering Algorithms.pptx
Rendering Algorithms.pptxSherinRappai
 
Introduction to Multimedia.pptx
Introduction to Multimedia.pptxIntroduction to Multimedia.pptx
Introduction to Multimedia.pptxSherinRappai
 
Working with data.pptx
Working with data.pptxWorking with data.pptx
Working with data.pptxSherinRappai
 
Introduction to PHP.pptx
Introduction to PHP.pptxIntroduction to PHP.pptx
Introduction to PHP.pptxSherinRappai
 
Aggregate functions in SQL.pptx
Aggregate functions in SQL.pptxAggregate functions in SQL.pptx
Aggregate functions in SQL.pptxSherinRappai
 
Introduction to DBMS.pptx
Introduction to DBMS.pptxIntroduction to DBMS.pptx
Introduction to DBMS.pptxSherinRappai
 
Input_Output_Organization.pptx
Input_Output_Organization.pptxInput_Output_Organization.pptx
Input_Output_Organization.pptxSherinRappai
 
Computer Organization and Design.pptx
Computer Organization and Design.pptxComputer Organization and Design.pptx
Computer Organization and Design.pptxSherinRappai
 

More from SherinRappai (15)

Unit 2.pptx
Unit 2.pptxUnit 2.pptx
Unit 2.pptx
 
COMPUTING AND PROGRAMMING FUNDAMENTAL.pptx
COMPUTING AND PROGRAMMING FUNDAMENTAL.pptxCOMPUTING AND PROGRAMMING FUNDAMENTAL.pptx
COMPUTING AND PROGRAMMING FUNDAMENTAL.pptx
 
Clustering.pptx
Clustering.pptxClustering.pptx
Clustering.pptx
 
neuralnetwork.pptx
neuralnetwork.pptxneuralnetwork.pptx
neuralnetwork.pptx
 
Rendering Algorithms.pptx
Rendering Algorithms.pptxRendering Algorithms.pptx
Rendering Algorithms.pptx
 
Introduction to Multimedia.pptx
Introduction to Multimedia.pptxIntroduction to Multimedia.pptx
Introduction to Multimedia.pptx
 
Linked List.pptx
Linked List.pptxLinked List.pptx
Linked List.pptx
 
system model.pptx
system model.pptxsystem model.pptx
system model.pptx
 
SE UNIT-1.pptx
SE UNIT-1.pptxSE UNIT-1.pptx
SE UNIT-1.pptx
 
Working with data.pptx
Working with data.pptxWorking with data.pptx
Working with data.pptx
 
Introduction to PHP.pptx
Introduction to PHP.pptxIntroduction to PHP.pptx
Introduction to PHP.pptx
 
Aggregate functions in SQL.pptx
Aggregate functions in SQL.pptxAggregate functions in SQL.pptx
Aggregate functions in SQL.pptx
 
Introduction to DBMS.pptx
Introduction to DBMS.pptxIntroduction to DBMS.pptx
Introduction to DBMS.pptx
 
Input_Output_Organization.pptx
Input_Output_Organization.pptxInput_Output_Organization.pptx
Input_Output_Organization.pptx
 
Computer Organization and Design.pptx
Computer Organization and Design.pptxComputer Organization and Design.pptx
Computer Organization and Design.pptx
 

Recently uploaded

Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsMark Billinghurst
 
Vertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsVertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsMiki Katsuragi
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticscarlostorres15106
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfAddepto
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 3652toLead Limited
 
My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024The Digital Insurer
 
APIForce Zurich 5 April Automation LPDG
APIForce Zurich 5 April  Automation LPDGAPIForce Zurich 5 April  Automation LPDG
APIForce Zurich 5 April Automation LPDGMarianaLemus7
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Scott Keck-Warren
 
SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxNavinnSomaal
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsSergiu Bodiu
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Enterprise Knowledge
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsMemoori
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyAlfredo García Lavilla
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr BaganFwdays
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitecturePixlogix Infotech
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Mark Simos
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brandgvaughan
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupFlorian Wilhelm
 

Recently uploaded (20)

Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR Systems
 
Vertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsVertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering Tips
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdf
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
 
My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024
 
APIForce Zurich 5 April Automation LPDG
APIForce Zurich 5 April  Automation LPDGAPIForce Zurich 5 April  Automation LPDG
APIForce Zurich 5 April Automation LPDG
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024
 
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptxE-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
 
SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptx
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platforms
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial Buildings
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easy
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC Architecture
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brand
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project Setup
 

FIFO Queue and LIFO Stack Data Structures Explained

  • 2. Stack • 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). • The last element to be added is the first to be removed (LIFO: Last In, First Out).
  • 3. • A stack is a non primitive linear data structure in which addition of new element or deletion of existing element takes place at the same end, known as top of the stack. • The stack is also called as Pushdown list. • Stack is an ordered list of similar data type.
  • 4.
  • 5. Operations on stack • The operation of insertion of an element into the stack is called Push operation. The operation of removing an element from the stack is called Pop operation. Data is stored and retrieved in a Last In First Out basis. It is called the LIFO principle. That is the element which is inserted last will be deleted first. • Stack is said to be in Overflow state when it is completely full and is said to be in Underflow state if it is completely empty.
  • 6. Stack implementation A stack is a simple linear data structure. It can be implemented in the following ways. i) Static implementation ii) Dynamic Implementation Static Implementation Static implementation can be achieved by using arrays. Limitations: Once a size of the array is declared, it cannot be modified during program execution. If the reserved memory is more, then the memory will be wasted. Therefore this method is suitable only when we know exactly the number of elements to be stored..
  • 7.
  • 8. Dynamic implementation Pointers can be used to create stack. There is no restriction on the number of elements. It is possible to increase the size of the stack and memory is utilized efficiently with the use of pointers.
  • 9.
  • 10. Creating a stack #define size 5 int top=-1; int stk[size]; // declare this globally so that all functions can access.
  • 11. #include<stdio.h> #include<conio.h> #define size 5 void push(); int pop(); void display(); int top=-1,stk[size]; void main() { int ch,item; clrscr();
  • 13. case 2: item=pop(); if(item!=-1) printf("Popped element=%dn",item); break; case 3: display(); break; case 4: exit(0); default: printf("Invalid choicen"); } } }
  • 14. void push( ) { if(top==(size-1)) printf("Stack full...Stack Overflown"); else { printf("Enter the elementn"); scanf("%d",&item); top++; stk[top]=item; } }
  • 15. Display() void display() { int i; i=top; printf("Stack Contentsn"); if(i= =(-1)) printf("Empty stack.....Stack Underflow.....n"); else { while(i!=(-1)) { printf("%dt",stk[i]); i--; } } }
  • 16. int pop() { int item; if(top==(-1)) { printf("Empty Stack.....Stack underflow....n"); return(-1); } else { item=stk[top]; top--; return(item); } }
  • 17. Applications The simplest application of a stack is to reverse a word. You push a given word to stack - letter by letter - and then pop letters from the stack. Another application is an "undo" mechanism in text editors; this operation is accomplished by keeping all text changes in a stack. Backtracking: Consider a simple example of finding the correct path in a maze. There are a series of points, from the starting point to the destination. We start from one point. To reach the final destination, there are several paths. Suppose we choose a random path. After following a certain path, we realise that the path we have chosen is wrong. So we
  • 18. This can be done with the use of stacks. With the help of stacks, we remember the point where we have reached. This is done by pushing that point into the stack. In case we end up on the wrong path, we can pop the last point from the stack and thus return to the last point and continue our quest to find the right path. This is called backtracking
  • 19. What is a queue? • It is an ordered group of homogeneous items of elements. • Queues have two ends: – Elements are added at one end. – Elements are removed from the other end. • The element added first is also removed first (FIFO: First In, First Out).
  • 20.
  • 21.
  • 22.
  • 23.
  • 24. Operations on Queue a. Inserting an element into queue b. Deleting an element from queue c. Checking the status whether queue is empty d. Checking the status whether queue is full e. Displaying the elements of queue • The first element inserted into a queue is the first element to be removed. For this reason queue is referred as first-in-first-out (FIFO) list.
  • 25. /* Program to simulate the working of a Linear Queue using an array*/ #include<stdio.h> #include<conio.h> void qinsert(); void qdelete(); void qdisplay(); int queue[10],front=0,rear=-1; int max=5; void main() { int ch; do
  • 26. { printf("nLINEAR QUEUE OPERATIONS n"); printf("1.Insert n"); printf("2.Delete n"); printf("3.Display n"); printf("4.Exit n"); printf("Enter your choicen"); scanf("%d",&ch); switch(ch) { case 1:qinsert(); break; case 2:qdelete(); break;
  • 27. case 3:qdisplay(); break; case 4:exit(0); default:printf("n WRONG CHOICE"); } } while(ch!=4); }
  • 28. void qinsert() { int item; if(rear==max-1) printf("Queue is full"); else { printf("Enter the value to insertn"); scanf("%d",&item); rear++; queue[rear]=item; } }
  • 29. void qdelete() { int item; if(front==rear) { front=rear=-1 printf("Queue is empty"); } else { item=queue[front]; printf("%d is deletedn",item);
  • 30. void qdisplay() { int item; int p=front; if(front==rear) { front=rear=-1 printf("Queue is empty"); } else { printf("nQueue Elements"); while(p<=rear) { printf("%dt",queue[p]); p++; } } }
  • 31. Queue empty and Queue full Conditions int qempty() { if(front == rear+1) return 1; else return 0; } int qfull( ) { if(rear = = max-1) return 1; else return 0; }
  • 32. 09/10/08 33 Example: Consider the following queue (linear queue). Rear = 4 and Front = 1 and N = 7 10 50 30 40 (1) Insert 20. Now Rear = 5 and Front = 1 1 2 3 4 5 6 7 10 50 30 40 20 1 2 3 4 5 6 7 (2) Delete Front Element. Now Rear = 5 and Front = 2 50 30 40 20 1 2 3 4 5 6 7 (3) Delete Front Element. Now Rear = 5 and Front = 3 30 40 20 1 2 3 4 5 6 7 (4) Insert 60. Now Rear = 6 and Front = 3 30 40 20 60 1 2 3 4 5 6 7
  • 33. Circular Queue Circular queue is a linear data structure. It follows FIFO principle. In circular queue the last node is connected back to the first node to make a circle. Circular linked list fallow the First In First Out principle Elements are added at the rear end and the elements are deleted at front end of the queue Both the front and the rear pointers points to the beginning of the array. It is also called as “Ring buffer”.
  • 34. 35 Example: Consider the following circular queue with N = 5. 1. Initially, Rear = 0, Front = 0. 2. Insert 10, Rear = 1, Front = 1. 3. Insert 50, Rear = 2, Front = 1. 4. Insert 20, Rear = 3, Front = 0. 5. Insert 70, Rear = 4, Front = 1. 6. Delete front, Rear = 4, Front = 2. Rear Rear Rear Rear Rear Front Front Front Front Front
  • 35. 36 7. Insert 100, Rear = 5, Front = 2. 8. Insert 40, Rear = 1, Front = 2. 9. Insert 140, Rear = 1, Front = 2. As Front = Rear + 1, so Queue overflow. 10. Delete front, Rear = 1, Front = 3. Front Rear Front Rear Rear Rear Front Front 11. Delete front, Rear = 1, Front = 4. 12. Delete front, Rear = 1, Front = 5. Rear Rear Front Front
  • 36.
  • 37. Double-Ended Queue • A double-ended queue is an abstract data type similar to an simple queue, it allows you to insert and delete from both sides means items can be added or deleted from the front or rear end.
  • 38.
  • 39. There are two variations of a deque namely, 1. Input – restricted deque 2. Output – restricted deque The input – restricted deque allows insertions at only one end. While an output – restricted deque permits deletions from only at one end.
  • 40.
  • 41.
  • 42. void dqdeletef() { if((front==-1)&&(rear==-1)) printf("Dequeue underflown"); else { printf("%d is deletedn",dq[front]); if(front==rear) front=rear=-1; else front=(front+1)%n; }
  • 43. void dqdeleter() { if((front==-1)&&(rear==-1)) printf("Dequeue underflown"); else { printf("%d is deletedn",dq[rear]); if(front==rear) front=rear=-1; else if(rear==0) rear=n-1; else rear=(rear-1)%n; } }
  • 44. void dqinsertr() { int item; if(front==(rear+1)%n) printf("Dequeue overflown"); else { printf("Enter the value to insertn"); scanf("%d",&item); if(front==-1) front=rear=0; else rear=(rear+1)%n; dq[rear]=item; } }
  • 45. void dqinsertf() { int item; if(front==(rear+1)%n) printf("Dequeue overflown"); else { printf("Enter the value to insertn"); scanf("%d",&item); if(front==-1) front=rear=0; else if(front==0) front=n-1; else front=(front-1)%n; dq[front]=item; } }
  • 46. Priority Queue A priority queue is different from a "normal" queue, because instead of being a "first-in-first-out" data structure, values come out in order by priority. Rules 1.Element of higher priority is processed before any element of lower priority. 2.Two elements with same priority are processed according to the order in which they are added in to the queue.
  • 47. ASCENDING--> Items are entered arbitrarily & only the smallest item may be removed. DESCENDING--> Items are entered arbitrarily & only the largest item may be removed. A descending priority queue is similar but allows deletion of only the largest item.