SlideShare ist ein Scribd-Unternehmen logo
1 von 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

Weitere ähnliche Inhalte

Ähnlich wie FIFO Queue and LIFO Stack Data Structures Explained

Ähnlich wie 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
 

Mehr von SherinRappai

Extensible markup language ppt as part of Internet Technology
Extensible markup language ppt as part of Internet TechnologyExtensible markup language ppt as part of Internet Technology
Extensible markup language ppt as part of Internet TechnologySherinRappai
 
Java script ppt from students in internet technology
Java script ppt from students in internet technologyJava script ppt from students in internet technology
Java script ppt from students in internet technologySherinRappai
 
Building Competency and Career in the Open Source World
Building Competency and Career in the Open Source WorldBuilding Competency and Career in the Open Source World
Building Competency and Career in the Open Source WorldSherinRappai
 
How to Build a Career in Open Source.pptx
How to Build a Career in Open Source.pptxHow to Build a Career in Open Source.pptx
How to Build a Career in Open Source.pptxSherinRappai
 
Issues in Knowledge representation for students
Issues in Knowledge representation for studentsIssues in Knowledge representation for students
Issues in Knowledge representation for studentsSherinRappai
 
A* algorithm of Artificial Intelligence for BCA students
A* algorithm of Artificial Intelligence for BCA studentsA* algorithm of Artificial Intelligence for BCA students
A* algorithm of Artificial Intelligence for BCA studentsSherinRappai
 
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
 

Mehr von SherinRappai (20)

Extensible markup language ppt as part of Internet Technology
Extensible markup language ppt as part of Internet TechnologyExtensible markup language ppt as part of Internet Technology
Extensible markup language ppt as part of Internet Technology
 
Java script ppt from students in internet technology
Java script ppt from students in internet technologyJava script ppt from students in internet technology
Java script ppt from students in internet technology
 
Building Competency and Career in the Open Source World
Building Competency and Career in the Open Source WorldBuilding Competency and Career in the Open Source World
Building Competency and Career in the Open Source World
 
How to Build a Career in Open Source.pptx
How to Build a Career in Open Source.pptxHow to Build a Career in Open Source.pptx
How to Build a Career in Open Source.pptx
 
Issues in Knowledge representation for students
Issues in Knowledge representation for studentsIssues in Knowledge representation for students
Issues in Knowledge representation for students
 
A* algorithm of Artificial Intelligence for BCA students
A* algorithm of Artificial Intelligence for BCA studentsA* algorithm of Artificial Intelligence for BCA students
A* algorithm of Artificial Intelligence for BCA students
 
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
 

Kürzlich hochgeladen

EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEarley Information Science
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...Neo4j
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityPrincipled Technologies
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonAnna Loughnan Colquhoun
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfsudhanshuwaghmare1
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfEnterprise Knowledge
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Igalia
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessPixlogix Infotech
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024The Digital Insurer
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Enterprise Knowledge
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CVKhem
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 

Kürzlich hochgeladen (20)

EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your Business
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 

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.