You will learn about linear data structures like stack and Queue. and also implementation by using array and linked list. at last, we will discuss the various applications of stack and queue and other varients of queues.
Programming in Java: Arrays. Last delivered in 2016. All educational material listed or linked to on these pages in relation to King's College London may be provided for reference only, and therefore does not necessarily reflect the current course content.
Method overloading, recursion, passing and returning objects from method, new...JAINAM KAPADIYA
Method and Method overloading
How to overload method in JAVA?
Method overloading Examples of the program.
New operator
Recursion
Passing and returning objects from methods.
Programming in Java: Arrays. Last delivered in 2016. All educational material listed or linked to on these pages in relation to King's College London may be provided for reference only, and therefore does not necessarily reflect the current course content.
Method overloading, recursion, passing and returning objects from method, new...JAINAM KAPADIYA
Method and Method overloading
How to overload method in JAVA?
Method overloading Examples of the program.
New operator
Recursion
Passing and returning objects from methods.
Topics Covered
==============================
Overview of .NET
Overview of ASP.NET
Creating an ASP.NET Web Form
Adding Event Procedures
Validating User Input
It consists of important different concepts related to PL/SQL that are covered like the Basic structure of PL/SQL block, Variables and Constants in PL/SQL, Control Structures i.e., Conditional, Iterative, and Sequential Control, Procedure and Function, Cursors and its Types, Applications of implicit and explicit cursors, Triggers and its Types and Exception Handling.
Topics Covered
==============================
Overview of .NET
Overview of ASP.NET
Creating an ASP.NET Web Form
Adding Event Procedures
Validating User Input
It consists of important different concepts related to PL/SQL that are covered like the Basic structure of PL/SQL block, Variables and Constants in PL/SQL, Control Structures i.e., Conditional, Iterative, and Sequential Control, Procedure and Function, Cursors and its Types, Applications of implicit and explicit cursors, Triggers and its Types and Exception Handling.
This material aims to enable students to:
1) Understanding stack concept
2) Understanding push, pop, peek or top, isEmpty, isFull operation in a stack
3) Understanding working of stack
4) Knowing of stack application
This PPT is very much useful for practitioners who are all making products and services to society. Mangers think innovatively and come up with innovative ideas. It is a 5 stage processing also called a design thinking process. The stages are empathize, define, ideate, prototype and test.
Final project report on grocery store management system..pdfKamal Acharya
In today’s fast-changing business environment, it’s extremely important to be able to respond to client needs in the most effective and timely manner. If your customers wish to see your business online and have instant access to your products or services.
Online Grocery Store is an e-commerce website, which retails various grocery products. This project allows viewing various products available enables registered users to purchase desired products instantly using Paytm, UPI payment processor (Instant Pay) and also can place order by using Cash on Delivery (Pay Later) option. This project provides an easy access to Administrators and Managers to view orders placed using Pay Later and Instant Pay options.
In order to develop an e-commerce website, a number of Technologies must be studied and understood. These include multi-tiered architecture, server and client-side scripting techniques, implementation technologies, programming language (such as PHP, HTML, CSS, JavaScript) and MySQL relational databases. This is a project with the objective to develop a basic website where a consumer is provided with a shopping cart website and also to know about the technologies used to develop such a website.
This document will discuss each of the underlying technologies to create and implement an e- commerce website.
HEAP SORT ILLUSTRATED WITH HEAPIFY, BUILD HEAP FOR DYNAMIC ARRAYS.
Heap sort is a comparison-based sorting technique based on Binary Heap data structure. It is similar to the selection sort where we first find the minimum element and place the minimum element at the beginning. Repeat the same process for the remaining elements.
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)MdTanvirMahtab2
This presentation is about the working procedure of Shahjalal Fertilizer Company Limited (SFCL). A Govt. owned Company of Bangladesh Chemical Industries Corporation under Ministry of Industries.
NUMERICAL SIMULATIONS OF HEAT AND MASS TRANSFER IN CONDENSING HEAT EXCHANGERS...ssuser7dcef0
Power plants release a large amount of water vapor into the
atmosphere through the stack. The flue gas can be a potential
source for obtaining much needed cooling water for a power
plant. If a power plant could recover and reuse a portion of this
moisture, it could reduce its total cooling water intake
requirement. One of the most practical way to recover water
from flue gas is to use a condensing heat exchanger. The power
plant could also recover latent heat due to condensation as well
as sensible heat due to lowering the flue gas exit temperature.
Additionally, harmful acids released from the stack can be
reduced in a condensing heat exchanger by acid condensation. reduced in a condensing heat exchanger by acid condensation.
Condensation of vapors in flue gas is a complicated
phenomenon since heat and mass transfer of water vapor and
various acids simultaneously occur in the presence of noncondensable
gases such as nitrogen and oxygen. Design of a
condenser depends on the knowledge and understanding of the
heat and mass transfer processes. A computer program for
numerical simulations of water (H2O) and sulfuric acid (H2SO4)
condensation in a flue gas condensing heat exchanger was
developed using MATLAB. Governing equations based on
mass and energy balances for the system were derived to
predict variables such as flue gas exit temperature, cooling
water outlet temperature, mole fraction and condensation rates
of water and sulfuric acid vapors. The equations were solved
using an iterative solution technique with calculations of heat
and mass transfer coefficients and physical properties.
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdffxintegritypublishin
Advancements in technology unveil a myriad of electrical and electronic breakthroughs geared towards efficiently harnessing limited resources to meet human energy demands. The optimization of hybrid solar PV panels and pumped hydro energy supply systems plays a pivotal role in utilizing natural resources effectively. This initiative not only benefits humanity but also fosters environmental sustainability. The study investigated the design optimization of these hybrid systems, focusing on understanding solar radiation patterns, identifying geographical influences on solar radiation, formulating a mathematical model for system optimization, and determining the optimal configuration of PV panels and pumped hydro storage. Through a comparative analysis approach and eight weeks of data collection, the study addressed key research questions related to solar radiation patterns and optimal system design. The findings highlighted regions with heightened solar radiation levels, showcasing substantial potential for power generation and emphasizing the system's efficiency. Optimizing system design significantly boosted power generation, promoted renewable energy utilization, and enhanced energy storage capacity. The study underscored the benefits of optimizing hybrid solar PV panels and pumped hydro energy supply systems for sustainable energy usage. Optimizing the design of solar PV panels and pumped hydro energy supply systems as examined across diverse climatic conditions in a developing country, not only enhances power generation but also improves the integration of renewable energy sources and boosts energy storage capacities, particularly beneficial for less economically prosperous regions. Additionally, the study provides valuable insights for advancing energy research in economically viable areas. Recommendations included conducting site-specific assessments, utilizing advanced modeling tools, implementing regular maintenance protocols, and enhancing communication among system components.
Using recycled concrete aggregates (RCA) for pavements is crucial to achieving sustainability. Implementing RCA for new pavement can minimize carbon footprint, conserve natural resources, reduce harmful emissions, and lower life cycle costs. Compared to natural aggregate (NA), RCA pavement has fewer comprehensive studies and sustainability assessments.
Forklift Classes Overview by Intella PartsIntella Parts
Discover the different forklift classes and their specific applications. Learn how to choose the right forklift for your needs to ensure safety, efficiency, and compliance in your operations.
For more technical information, visit our website https://intellaparts.com
1. UNIT V : Stack and Queue
By
Mr.S.Selvaraj
Asst. Professor (SRG) / CSE
Kongu Engineering College
Perundurai, Erode, Tamilnadu, India
Thanks to and Resource from : Sumitabha Das, “Computer Fundamentals and C Programming”, 1st Edition, McGraw Hill, 2018.
20CST21 – Programming and Linear Data Structures
4. Text Book and Reference Book
7/15/2021 5.1 _ Introduction to Stack 4
5. Unit IV : Contents
1. Introduction to Stack
2. Implementation of stack using
– Array and linked list
3. Application of stack
– Infix to Postfix expression conversion
– Postfix expression evaluation
4. Introduction to Queue
5. Implementation of Queue using
– Array and linked list
6. Other variations of Queue
7. Applications of Queue
7/15/2021 5
5.1 _ Introduction to Stack
6. Introduction to Stack
• Stack is a simple data structure which has
bounded capacity.
• In stack, data is added and removed at only one
end called the Top.
• When an item is inserted on to the stack, it is
placed on Top of the stack.
• When an item is removed from the stack, it
removes the Top most element.
• There are two basic operations performed on a
Stack:
– Push()
– Pop()
7/15/2021 5.1 _ Introduction to Stack 6
7. Introduction to Stack
• In stack terminology,
– insertion operation is called Push() operation and
– removal operation is called Pop() operation.
• The last element that is pushed into the stack
is the first element to be popped out of the
stack.
• That is, it follows Last-In First-Out (LIFO)
strategy.
• Some of the features of stack are as follows:
7/15/2021 5.1 _ Introduction to Stack 7
8. Features of Stack
• Stack is an ordered list of similar type of
elements.
• It is a linear list where all insertions and deletions
are permitted only at one end of the list called
Top.
• Stack is a LIFO (Last-In-First-Out) structure.
• When a stack is full then it is said to be in
Overflow state
• When a stack is empty then it is said to be in
Underflow state.
7/15/2021 5.1 _ Introduction to Stack 8
12. Application of Stack in Real time scenario
• A stack of plates on the dining table
• Wearing and Removing of Bangles in our hand
7/15/2021 5.1 _ Introduction to Stack 12
13. Applications of stack in Computer Science and Programming
• Support of function calls
– When a function is invoked, the function which is invoked last will be
completed first. This is the property of stack.
– Primary purpose is to store the return address.
• Support of recursive functions
– When a function calls itself recursively, a return address needs to be stored
for each activation of the function so that it can later be used to return from
the function activation.
• An "undo/redo" mechanism in text editors
• Expression Evaluation
– Used to evaluate prefix, postfix and infix expressions.
• Expression Conversion
– An expression can be represented in prefix, postfix or infix form. Stack can be
used to convert one form of expression to another
• Reverse a string
– Push a given word to stack letter by letter and then pop letters from the stack
one by one
• Forward and backward feature in web browsers
• Parenthesis Checking
– Stack is used to check the proper opening and closing of parenthesis
7/15/2021 5.1 _ Introduction to Stack 13
14. Implementation of Stack
• Stack can be implemented using any one of
the following data structures:
– Array
– Linked List
• Implementing a stack using array can store
fixed number of data values but using linked
list hold any number of elements .
7/15/2021 5.1 _ Introduction to Stack 14
15. Stack using Array
• Stack can be implemented using one
dimensional array.
• Array can be created in predefined size.
• We can not increase the size of the array if we
have more elements to insert.
• That is, Stack implementation with array can
hold only fixed number of elements
7/15/2021 5.1 _ Introduction to Stack 15
16. Stack using Array
• we need an integer variable to keep track of
Top most element in the stack.
• It should be initialized with -1 to indicate stack
empty.
7/15/2021 5.1 _ Introduction to Stack 16
17. Stack using Array
• In the above definition, SIZE is a constant
which represents maximum number of
elements stored in a stack.
• Value of Top = SIZE – 1 indicates stack is full.
• In a stack, initially Top is set to -1.
• Top is used to keep track of the index of the
Top most element.
7/15/2021 5.1 _ Introduction to Stack 17
18. Stack using Array
• The following table represents status of stack
for different values of Top.
7/15/2021 5.1 _ Introduction to Stack 18
19. Push(value) - Inserting value into the stack
• Step 1: Check whether stack is already FULL.
Stack is Full when Top= SIZE-1.
• Step 2: If it is FULL then display "Stack is
FULL" and stop the function.
• Step 3: If it is not FULL, then increment Top
value by one and store the value at stk[Top].
7/15/2021 5.1 _ Introduction to Stack 19
20. Pop() - Remove a value from the Stack
• Step 1: Check whether stack is EMPTY. It is
Empty when Top= -1
• Step 2: If it is EMPTY, then display "Stack is
Empty" and stop the function.
• Step 3: If it is not EMPTY, then delete Top
most element (ie., stack[Top]) and decrement
Top value by one.
Top = Top -1
7/15/2021 5.1 _ Introduction to Stack 20
21. Display() - Display the elements of a Stack
• Step 1: Check whether stack is EMPTY.
• Step 2: If it is EMPTY, then display "Stack is Empty"
and terminate the function.
• Step 3: Initialize a variable 'i' with a value Top.
• Step 4: Display the value of stack[i] and decrement
the value of i by one
• Step 5: Repeat step 4 until “i” value becomes 0.
7/15/2021 5.1 _ Introduction to Stack 21
29. UNIT V : Stack and Queue
By
Mr.S.Selvaraj
Asst. Professor (SRG) / CSE
Kongu Engineering College
Perundurai, Erode, Tamilnadu, India
Thanks to and Resource from : Sumitabha Das, “Computer Fundamentals and C Programming”, 1st Edition, McGraw Hill, 2018.
20CST21 – Programming and Linear Data Structures
30. Unit IV : Contents
1. Introduction to Stack
2. Implementation of stack using
– Array and linked list
3. Application of stack
– Infix to Postfix expression conversion
– Postfix expression evaluation
4. Introduction to Queue
5. Implementation of Queue using
– Array and linked list
6. Other variations of Queue
7. Applications of Queue
7/15/2021 30
5.2 _ Implementation of Stack
31. Implementation of Stack
• Stack can be implemented using any one of
the following data structures:
– Array
– Linked List
• Implementing a stack using array can store
fixed number of data values but using linked
list hold any number of elements .
7/15/2021 5.2 _ Implementation of Stack 31
32. Stack using Array
• Stack can be implemented using one
dimensional array.
• Array can be created in predefined size.
• We can not increase the size of the array if we
have more elements to insert.
• That is, Stack implementation with array can
hold only fixed number of elements
7/15/2021 5.2 _ Implementation of Stack 32
33. Stack using Array
• we need an integer variable to keep track of
Top most element in the stack.
• It should be initialized with -1 to indicate stack
empty.
7/15/2021 5.2 _ Implementation of Stack 33
34. Stack using Array
• In the above definition, SIZE is a constant
which represents maximum number of
elements stored in a stack.
• Value of Top = SIZE – 1 indicates stack is full.
• In a stack, initially Top is set to -1.
• Top is used to keep track of the index of the
Top most element.
7/15/2021 5.2 _ Implementation of Stack 34
35. Stack using Array
• The following table represents status of stack
for different values of Top.
7/15/2021 5.2 _ Implementation of Stack 35
36. Push(value) - Inserting value into the stack
• Step 1: Check whether stack is already FULL.
Stack is Full when Top= SIZE-1.
• Step 2: If it is FULL then display "Stack is
FULL" and stop the function.
• Step 3: If it is not FULL, then increment Top
value by one and store the value at stk[Top].
7/15/2021 5.2 _ Implementation of Stack 36
37. Pop() - Remove a value from the Stack
• Step 1: Check whether stack is EMPTY. It is
Empty when Top= -1
• Step 2: If it is EMPTY, then display "Stack is
Empty" and stop the function.
• Step 3: If it is not EMPTY, then delete Top
most element (ie., stack[Top]) and decrement
Top value by one.
Top = Top -1
7/15/2021 5.2 _ Implementation of Stack 37
38. Display() - Display the elements of a Stack
• Step 1: Check whether stack is EMPTY.
• Step 2: If it is EMPTY, then display "Stack is Empty"
and terminate the function.
• Step 3: Initialize a variable 'i' with a value Top.
• Step 4: Display the value of stack[i] and decrement
the value of i by one
• Step 5: Repeat step 4 until “i” value becomes 0.
7/15/2021 5.2 _ Implementation of Stack 38
39. 7/15/2021 5.2 _ Implementation of Stack 39
Stack using Array
45. Array implementation of stack
• Pros: Easy to implement. Memory is not efficiently
utilized as pointers are not involved.
• Cons: It is not dynamic. It doesn’t grow and shrink
depending on needs at runtime
7/15/2021 45
5.2 _ Implementation of Stack
46. Linked list Implementation of Stack
//Initially,
struct stack
{
int data;
struct stack *next;
}*head,*temp;
//Function Prototype
void push(void);
void pop(void);
void display(void);
int isempty(void);
7/15/2021 46
5.2 _ Implementation of Stack
47. Linked list Implementation of Stack
void push()
{
int x;
printf("n enter the numbern");
scanf("%d",&x);
temp=(struct stack *)malloc(sizeof(struct stack));
temp->data=x;
temp->next=NULL;
if(head==NULL)
{
head=temp;
}
else
{
temp->next=head;
head=temp;
}
}
7/15/2021 47
5.2 _ Implementation of Stack
48. Linked list Implementation of Stack
int isempty()
{
if(head==NULL)
return 1;
else
return 0;
}
void pop()
{
if(isempty())
printf("n stack is emptyn");
else
{
printf("n The popped element is %d",head->data);
head=head->next;
}
}
7/15/2021 48
5.2 _ Implementation of Stack
49. Linked list Implementation of Stack
void display()
{
if(isempty())
printf("n stack is emptyn");
else
{
temp=head;
while(temp!=NULL)
{
printf("%d->",temp->data);
temp=temp->next;
}
printf("NULL");
}
}
7/15/2021 49
5.2 _ Implementation of Stack
50. Linked list Implementation of Stack
int main()
{
int c;
while(1)
{
printf("n1.push 2.pop 3.display 4.exitn");
scanf("%d",&c);
switch(c)
{
case 1:
push();
break;
case 2:
pop();
break;
7/15/2021 50
5.2 _ Implementation of Stack
51. Linked list Implementation of Stack
case 3:
display();
break;
case 4:
exit(0);
}
}
return 0;
}
7/15/2021 51
5.2 _ Implementation of Stack
53. UNIT V : Stack and Queue
By
Mr.S.Selvaraj
Asst. Professor (SRG) / CSE
Kongu Engineering College
Perundurai, Erode, Tamilnadu, India
Thanks to and Resource from : Sumitabha Das, “Computer Fundamentals and C Programming”, 1st Edition, McGraw Hill, 2018.
20CST21 – Programming and Linear Data Structures
54. Unit IV : Contents
1. Introduction to Stack
2. Implementation of stack using
– Array and linked list
3. Application of stack
– Infix to Postfix expression conversion
– Postfix expression evaluation
4. Introduction to Queue
5. Implementation of Queue using
– Array and linked list
6. Other variations of Queue
7. Applications of Queue
7/15/2021 54
5.3 _ Application of Stack
55. Applications of Stack
Balancing Parenthesis
Postfix Expressions Evaluation
Infix to Postfix Conversion
Towers of Hanoi problem
String Reversal
sudoku solver
7/15/2021 55
5.3 _ Application of Stack
56. Infix, Prefix, Postfix Expression
• Infix expression:
– An expression is called the Infix expression if the operator
appears in between the operands in the expression.
– Simply of the form (operand1 operator operand2).
– Operators are written in-between their operands.
X + Y
A * ( B + C ) / D
• Prefix expression (also known as "Polish notation"):
– An expression is called the Prefix expression if the
operator appears before the operands in the expression.
– Simply of the form (operator operand1 operand2).
– Operators are written before their operands.
+ X Y
/ * A + B C D
7/15/2021 56
5.3 _ Application of Stack
57. Infix, Prefix, Postfix Expression
• Postfix expression (also known as "Reverse
Polish notation"):
– An expression is called the postfix expression if the
operator appears in the expression after the
operands.
– Simply of the form (operand1 operand2 operator).
– Operators are written after their operands.
X Y +
A B C + * D /
7/15/2021 57
5.3 _ Application of Stack
59. Infix to Postfix Conversion
Algorithm
1. Scan the infix expression from left to right.
2. If the scanned character is an operand, output it.
3. Else,
…..3.1 If the precedence of the scanned operator is greater than
the precedence of the operator in the stack or the stack is
empty or the stack contains a ‘(‘, push it.
…..3.2 Else, Pop all the operators from the stack which are
greater than or equal to in precedence than that of the
scanned operator. After doing that Push the scanned
operator to the stack. (If you encounter parenthesis while
popping then stop there and push the scanned operator in
the stack.)
4. If the scanned character is an ‘(‘, push it to the stack.
5. If the scanned character is an ‘)’, pop the stack and output all
the operators until a ‘(‘ is encountered, and discard both
the parenthesis.
6. Repeat steps 2-6 until infix expression is scanned.
7. Print the output
8. Pop and output from the stack until it is not empty.
7/15/2021 59
5.3 _ Application of Stack
60. An illustration
• First, the symbol a is read, so it is passed through to the output. Then '+' is
read and pushed onto the stack.
• Next b is read and passed through to the output
• Next a '*' is read. The top entry on the operator stack has lower
precedence than '*', so nothing is output and '*' is put on the stack.
• Next, c is read and output.
7/15/2021 60
5.3 _ Application of Stack
61. An illustration
• The next symbol is a '+'. Checking the stack, we find that we
will pop a '*' and place it on the output, pop the other '+',
which is not of lower but equal priority, on the stack, and then
push the '+'.
• The next symbol read is an '(', which, being of highest
precedence, is placed on the stack. Then d is read and output.
7/15/2021 61
5.3 _ Application of Stack
62. An illustration
• We continue by reading a '*'. Since open parentheses do not
get removed except when a closed parenthesis is being
processed, there is no output. Next, e is read and output.
• The next symbol read is a '+'. We pop and output '*' and then
push '+'. Then we read and output
7/15/2021 62
5.3 _ Application of Stack
63. An illustration
• Now we read a ')', so the stack is emptied back to the '('. We
output a '+'.
• We read a '*' next; it is pushed onto the stack. Then g is read
and output.
7/15/2021 63
5.3 _ Application of Stack
64. An illustration
• The input is now empty, so we pop and output symbols from
the stack until it is empty.
7/15/2021 64
5.3 _ Application of Stack
70. Problems
1) Infix : (A+B) * (C-D)
Infix to Postfix : AB+CD-*
2) Infix : A-(B/C)*(A/K)-L
Infix to Postfix : ABC/-AK/L-*
7/15/2021 70
5.3 _ Application of Stack
71. Expression Evaluation
The simple algorithm uses a stack and is as follows:
1. Make an empty stack.
2. Read an input string.
3. Read characters one by one until end of string is
encountered.
4. If the character is a digit, convert it into an integer
and push onto the stack.
5. If the character is not a digit, pop two numbers from
the stack and perform the corresponding operation
and push the result onto the stack.
6. At end of the string, pop the result from the stack
7/15/2021 71
5.3 _ Application of Stack
72. ILLUSTRATION
7/15/2021
Next '+' is read, so 3 and 2 are popped from the stack and their sum, 5, is pushed.
Next 8 is pushed.
72
5.3 _ Application of Stack
73. 7/15/2021
Now '*' is read, so 8 and 5 are popped as 8 * 5 = 40 is pushed.
73
5.3 _ Application of Stack
77. UNIT V : Stack and Queue
By
Mr.S.Selvaraj
Asst. Professor (SRG) / CSE
Kongu Engineering College
Perundurai, Erode, Tamilnadu, India
Thanks to and Resource from : Sumitabha Das, “Computer Fundamentals and C Programming”, 1st Edition, McGraw Hill, 2018.
20CST21 – Programming and Linear Data Structures
78. Unit IV : Contents
1. Introduction to Stack
2. Implementation of stack using
– Array and linked list
3. Application of stack
– Infix to Postfix expression conversion
– Postfix expression evaluation
4. Introduction to Queue
5. Implementation of Queue using
– Array and linked list
6. Other variations of Queue
7. Applications of Queue
7/15/2021 78
5.4 _ Introduction to Queue and its
Implementation
80. Introduction to Queue
• Queue is a simple linear data structure which is a
ordered list of similar type of items.
• In Queue, data is added at one end called the rear, and
data is removed at the other end called the front.
• Queue follows FIFO (First-In-First-Out) strategy i.e. The
element which is inserted first is the one removed first.
7/15/2021 80
5.4 _ Introduction to Queue and its
Implementation
82. Queue ADT
• The following operations are implemented in queue
data structure:
– Isfull() – used to check whether the queue is full.
– Isempty() – used to check whether the queue is empty.
– Enqueue() – used to add a new item to the end of the
queue. (Insertion Operation)
– Dequeue()- used to remove the front item from the queue.
(Deletion Operation)
– Display() - used to display all the elements in the queue.
7/15/2021 82
5.4 _ Introduction to Queue and its
Implementation
83. Applications of Queue
• CPU scheduling
– When multiple processes require CPU at the same time,
CPU scheduling algorithms schedules the tasks for
submission. Usually these algorithms are implemented
using Queue data structure.
• Printer Queue
– Jobs submitted to a printer are printed in order of their
arrival.
• Call Center phone systems
– All the calls are queued until a service representative is
free. Calls are serviced based on the order of their arrival.
7/15/2021 83
5.4 _ Introduction to Queue and its
Implementation
84. Implementation Of Queue
• There are two ways to implement a queue:
–Sequential allocation Using array
–Linked allocation Using linked list
7/15/2021 84
5.4 _ Introduction to Queue and its
Implementation
85. Linear Queue – Definition and Overflow
7/15/2021
5.4 _ Introduction to Queue and its
Implementation
85
86. Linear Queue – Underflow
7/15/2021
5.4 _ Introduction to Queue and its
Implementation
86
87. Linear Queue – Enqueue
7/15/2021
5.4 _ Introduction to Queue and its
Implementation
87
88. Linear Queue – Dequeue
7/15/2021
5.4 _ Introduction to Queue and its
Implementation
88
89. Linear Queue – Traversal / Display
7/15/2021
5.4 _ Introduction to Queue and its
Implementation
89
90. Array implementation of Queue
Initially,
#define MAX 3
int q[MAX], rear=-1, front=-1;
Function Prototype
void enqueue(void);
void dequeue(void);
int isfull(void);
int isempty(void);
void display(void);
7/15/2021 90
5.4 _ Introduction to Queue and its
Implementation
91. Array implementation of Queue
int isfull()
{
if(rear==MAX-1)
return 1;
else
return 0;
}
int isempty()
{
if(front==-1||front>rear)
return 1;
else
return 0;
}
7/15/2021 91
5.4 _ Introduction to Queue and its
Implementation
92. Array implementation of Queue
void enqueue()
{
int x;
if(isfull())
printf("n queue is fulln");
else
{
printf("n enter the numbern");
scanf("%d",&x);
if(rear==-1)
{
front=0;
q[++rear]=x;
}
else
{
q[++rear]=x;
}
}
}
7/15/2021 92
5.4 _ Introduction to Queue and its
Implementation
93. Array implementation of Queue
void dequeue()
{
if(isempty())
printf("n queue is emptyn");
else if(front==rear)
{
printf("n The dequeued element is %dn",q[front]);
front=rear=-1;
}
else
{
printf("n The dequeued element is %dn",q[front]);
front++;
}
}
7/15/2021 93
5.4 _ Introduction to Queue and its
Implementation
94. Array implementation of Queue
void display()
{
int i;
if(isempty())
printf("n queue is emptyn");
else
{
printf("n the elements in the queue aret");
for(i=front;i<=rear; i++)
printf("%dt",q[i]);
}
}
7/15/2021 94
5.4 _ Introduction to Queue and its
Implementation
95. Array implementation of Queue
int main()
{
int c;
while(1)
{
printf("n1.enqueue 2.dequeue 3.display 4.exitn");
scanf("%d",&c);
switch(c)
{
case 1:
enqueue();
break;
case 2:
dequeue();
break;
case 3:
display();
break;
case 4:
exit(0);
}
}
return 0;
}
7/15/2021 95
5.4 _ Introduction to Queue and its
Implementation
96. Linked List implementation of Queue – Define and underflow
7/15/2021
5.4 _ Introduction to Queue and its
Implementation
96
97. Linked List implementation of Queue - Display
7/15/2021
5.4 _ Introduction to Queue and its
Implementation
97
98. Linked List implementation of Queue - Enqueue
7/15/2021
5.4 _ Introduction to Queue and its
Implementation
98
99. Linked List implementation of Queue - Enqueue
7/15/2021
5.4 _ Introduction to Queue and its
Implementation
99
100. Linked List implementation of Queue - Dequeue
7/15/2021
5.4 _ Introduction to Queue and its
Implementation
100
101. Linked List implementation of Queue
struct queue
{
int data;
struct queue *next;
}*rear, *front,*temp;
Function Prototype
void enqueue(void);
void dequeue(void);
void display(void);
int isempty(void);
7/15/2021 101
5.4 _ Introduction to Queue and its
Implementation
102. Linked List implementation of Queue
void enqueue()
{
int x;
printf("n enter the numbern");
scanf("%d",&x);
temp=(struct queue *)malloc(sizeof(struct queue));
temp->data=x;
temp->next=NULL;
if(front==NULL)
{
front=rear=temp;
}
else
{
rear->next=temp;
rear=temp;
}
}
7/15/2021 102
5.4 _ Introduction to Queue and its
Implementation
103. Linked List implementation of Queue
int isempty()
{
if(front==NULL)
return 1;
else
return 0;
}
void dequeue()
{
if(isempty())
printf("n queue is emptyn");
else
{
printf("n The deleted element is %d",front->data);
front=front->next;
}
}
7/15/2021 103
5.4 _ Introduction to Queue and its
Implementation
104. Linked List implementation of Queue
void display()
{
if(isempty())
printf("n queue is emptyn");
else
{
temp=front;
while(temp!=NULL)
{
printf("%d->",temp->data);
temp=temp->next;
}
printf("NULL");
}
}
7/15/2021 104
5.4 _ Introduction to Queue and its
Implementation
105. Linked List implementation of Queue
int main()
{
int c;
while(1)
{
printf("n1.enqueue 2.dequeue 3.display 4.exitn");
scanf("%d",&c);
switch(c)
{
case 1:
enqueue();
break;
case 2:
dequeue();
break;
7/15/2021 105
5.4 _ Introduction to Queue and its
Implementation
106. Linked List implementation of Queue
case 3:
display();
break;
case 4:
exit(0);
}
}
return 0;
}
7/15/2021 106
5.4 _ Introduction to Queue and its
Implementation
108. UNIT V : Stack and Queue
By
Mr.S.Selvaraj
Asst. Professor (SRG) / CSE
Kongu Engineering College
Perundurai, Erode, Tamilnadu, India
Thanks to and Resource from : Sumitabha Das, “Computer Fundamentals and C Programming”, 1st Edition, McGraw Hill, 2018.
20CST21 – Programming and Linear Data Structures
109. Unit IV : Contents
1. Introduction to Stack
2. Implementation of stack using
– Array and linked list
3. Application of stack
– Infix to Postfix expression conversion
– Postfix expression evaluation
4. Introduction to Queue
5. Implementation of Queue using
– Array and linked list
6. Other variations of Queue
7. Applications of Queue
7/15/2021 109
5.5 _ Types of Queue and its Applications
110. Types of Queue
• Linear Queue
• Circular Queue
• Double Ended Queue
• Priority Queue
7/15/2021 5.5 _ Types of Queue and its Applications 110
113. Linear Queue
• In Linear Queue, an
– insertion takes place from one end
– deletion occurs from another end
• The end at which the
– insertion takes place is known as the rear end,
– deletion takes place is known as front end
• It strictly follows the FIFO rule.
7/15/2021 5.5 _ Types of Queue and its Applications 113
114. Linear Queue
• The linear Queue can be represented, as shown
in the below figure:
• The elements are inserted from the rear end, and
if we insert more elements in a Queue, then the
rear value gets incremented on every insertion.
7/15/2021 5.5 _ Types of Queue and its Applications 114
115. Linear Queue
• If we want to show the deletion, then it can be
represented as:
• The front pointer points to the next element, and the
element which was previously pointed by the front
pointer was deleted.
7/15/2021 5.5 _ Types of Queue and its Applications 115
116. Drawbacks of Linear Queue
• The major drawback of using a linear Queue is
that insertion is done only from the rear end.
• If the first three elements are deleted from the
Queue, we cannot insert more elements even
though the space is available in a Linear Queue.
• In this case, the linear Queue shows
the overflow condition as the rear is pointing to
the last element of the Queue.
• Solution is Circular Queue.
7/15/2021 5.5 _ Types of Queue and its Applications 116
118. Why was the concept of the circular queue introduced?
• There was one limitation in the array
implementation of Queue.
• If the rear reaches to the end position of the
Queue then there might be possibility that
some vacant spaces are left in the beginning
which cannot be utilized.
• So, to overcome such limitations, the concept
of the circular queue was introduced.
7/15/2021 5.5 _ Types of Queue and its Applications 118
119. 7/15/2021 5.5 _ Types of Queue and its Applications 119
Why was the concept of the circular queue introduced?
120. • As we can see in the previous image, the rear is at the last position
of the Queue and front is pointing somewhere rather than the
0th position.
• In the above array, there are only two elements and other three
positions are empty.
• if we try to insert the element then it will show that there are no
empty spaces in the Queue.
• There is one solution to avoid such wastage of memory space by
shifting both the elements at the left and adjust the front and rear
end accordingly.
• It is not a practically good approach because shifting all the
elements will consume lots of time.
• The efficient approach to avoid the wastage of the memory is to
use the circular queue data structure
7/15/2021 5.5 _ Types of Queue and its Applications 120
Why was the concept of the circular queue introduced?
121. Circular Queue
• It is linear data structure.
• operations are performed based on FIFO (First In First Out)
principle
• last position is connected back to the first position to make a
circle.
• It is also called ‘Ring Buffer’.
7/15/2021 121
5.5 _ Types of Queue and its Applications
122. Circular Queue
• In a normal Queue, we can insert elements until queue becomes
full. But once queue becomes full, we can not insert the next
element even if there is a space in front of queue.
• In circular queue, we can insert the next element even rear is
reached last position.
7/15/2021 5.5 _ Types of Queue and its Applications 122
123. Operations of Circular Queue
• The following are the operations that can be
performed on a circular queue:
– Front: It is used to get the front element from the
Queue.
– Rear: It is used to get the rear element from the
Queue.
– enQueue(value): This function is used to insert the
new value in the Queue. The new element is always
inserted from the rear end.
– deQueue(): This function deletes an element from the
Queue. The deletion in a Queue always takes place
from the front end.
7/15/2021 5.5 _ Types of Queue and its Applications 123
124. Steps on Enqueue Operation
• First, we will check whether the Queue is full
or not.
• Initially the front and rear are set to -1.
• When we insert the first element in a Queue,
front and rear both are set to 0.
• When we insert a new element, the rear gets
incremented, i.e., rear=rear+1.
7/15/2021 5.5 _ Types of Queue and its Applications 124
125. Steps on Enqueue Operation (contd.,)
7/15/2021 5.5 _ Types of Queue and its Applications 125
126. Steps on Dequeue Operation
• First, we check whether the Queue is empty or
not.
• If the queue is empty, we cannot perform the
dequeue operation.
• When the element is deleted, the value of front
gets decremented by 1.
• If there is only one element left which is to be
deleted, then the front and rear are reset to -1.
7/15/2021 5.5 _ Types of Queue and its Applications 126
127. Circular Queue - Applications
• Memory Management: The unused memory locations in the case
of ordinary queues can be utilized in circular queues.
• Traffic system: In a computer-controlled traffic system, circular
queues are used to switch on the traffic lights one by one
repeatedly as per the time set.
• CPU Scheduling: Operating systems often maintain a queue of
processes that are ready to execute or that are waiting for a
particular event to occur (Round-robin scheduling).
7/15/2021 127
5.5 _ Types of Queue and its Applications
128. Double Ended Queue (Deque)
7/15/2021 5.5 _ Types of Queue and its Applications 128
129. Double Ended Queue (Deque)
The deque stands for Double Ended Queue.
In the queue, the insertion takes place from one end while
the deletion takes place from another end.
The end at which the
insertion occurs is known as the rear end
deletion occurs is known as front end.
linear data structure
7/15/2021 129
5.5 _ Types of Queue and its Applications
130. Properties of Deque
• Deque can be used both as stack and queue as it allows the
insertion and deletion operations on both ends.
• (1) In deque, the insertion and deletion operation can be
performed from one side.
• The stack follows the LIFO rule in which both the insertion
and deletion can be performed only from one end;
• Therefore, we conclude that deque can be considered as a
stack.
7/15/2021 5.5 _ Types of Queue and its Applications 130
131. Properties of Deque
• (2) In deque, the insertion can be performed on one
end, and the deletion can be done on another end.
• The queue follows the FIFO rule in which the element
is inserted on one end and deleted from another end.
• Therefore, we conclude that the deque can also be
considered as the queue.
7/15/2021 5.5 _ Types of Queue and its Applications 131
132. Types of Deque
• There are two types of Queues:
– Input-restricted queue
– output-restricted queue
7/15/2021 5.5 _ Types of Queue and its Applications 132
133. Input restricted queue
• The input-restricted queue means that some
restrictions are applied to the insertion.
• In input-restricted queue,
– the insertion is applied to one end
– the deletion is applied from both the ends.
• This kind of Queue does not follow FIFO(first in first
out).
• Uses: This queue is used in the cases where the
consumption of the data needs to be in FIFO order
but and if there is a need to remove the recently
inserted data for some reasons and one such case
can be irrelevant data, performance issue, etc.
7/15/2021 133
5.5 _ Types of Queue and its Applications
134. Output restricted queue
• The output-restricted queue means that some
restrictions are applied to the deletion operation.
• In an output-restricted queue,
– the deletion can be applied only from one end
– the insertion is possible from both ends
• Uses: This queue is used in the case where the inputs
have some priority order to be executed and the
input can be placed even in the first place so that it is
executed first.
7/15/2021 134
5.5 _ Types of Queue and its Applications
135. List of Operations performed on Deque
7/15/2021 5.5 _ Types of Queue and its Applications 135
136. Double Ended Queue (Deque)
• A deque can be implemented either using a doubly-linked
list or circular array.
• In both implementation, we can implement all operations in O(1)
time.
7/15/2021 5.5 _ Types of Queue and its Applications 136
137. Applications of Deque
• The deque can be used as a stack and queue; therefore, it
can perform both redo and undo operations in software
applications.
• It can be used as a palindrome checker means that if we
read the string from both ends, then the string would be
the same.
• It can be used for multiprocessor scheduling. Suppose we
have two processors, and each processor has one process
to execute. Each processor is assigned with a process or a
job, and each process contains multiple threads. Each
processor maintains a deque that contains threads that are
ready to execute.
• Internet browser’s history
7/15/2021 5.5 _ Types of Queue and its Applications 137
138. Priority Queue
• A priority queue is an abstract data type that behaves similarly to
the normal queue except that each element has some priority.
• The element with the highest priority would come first in a
priority queue.
• The priority of the elements in a priority queue will determine the
order in which elements are removed from the priority queue.
• The priority queue supports only comparable elements, which
means that the elements are either arranged in an ascending or
descending order.
• For example, suppose we have some values like 1, 3, 4, 8, 14, 22
inserted in a priority queue with an ordering imposed on the
values is from least to the greatest.
• Therefore, the
– 1 number would be having the highest priority
– 22 will be having the lowest priority
7/15/2021 5.5 _ Types of Queue and its Applications 138
139. Characteristics of a Priority queue
• Every element in a priority queue has some
priority associated with it.
• An element with the higher priority will be
deleted before the deletion of the lesser
priority.
• If two elements in a priority queue have the
same priority, they will be arranged using the
FIFO principle.
7/15/2021 5.5 _ Types of Queue and its Applications 139
140. Priority Queue Example
• We have a priority queue that contains the following values:
1, 3, 4, 8, 14, 22
• All the values are arranged in ascending order.
• Now, we will observe how the priority queue will look after
performing the following operations:
• poll(): This function will remove the highest priority element from
the priority queue. In the above priority queue, the '1' element has
the highest priority, so it will be removed from the priority queue.
• add(2): This function will insert '2' element in a priority queue. As 2
is the smallest element among all the numbers so it will obtain the
highest priority.
• poll(): It will remove '2' element from the priority queue as it has
the highest priority queue.
• add(5): It will insert 5 element after 4 as 5 is larger than 4 and
lesser than 8, so it will obtain the third highest priority in a priority
queue.
7/15/2021 5.5 _ Types of Queue and its Applications 140
141. Types of Priority Queue
• There are two types of priority queue:
– Ascending order priority queue
– Descending order priority queue
7/15/2021 5.5 _ Types of Queue and its Applications 141
142. Ascending order priority queue
• In ascending order priority queue, a lower priority
number is given as a higher priority in a priority.
• For example, we take the numbers from 1 to 5
arranged in an ascending order like 1,2,3,4,5;
• Therefore, the smallest number, i.e., 1 is given as the
highest priority in a priority queue.
7/15/2021 5.5 _ Types of Queue and its Applications 142
143. Descending order priority queue
• In descending order priority queue, a higher priority
number is given as a higher priority in a priority.
• For example, we take the numbers from 1 to 5
arranged in descending order like 5, 4, 3, 2, 1;
• Therefore, the largest number, i.e., 5 is given as the
highest priority in a priority queue.
7/15/2021 5.5 _ Types of Queue and its Applications 143
144. Implementation of Priority Queue
• The priority queue can be implemented in
four ways that include
– arrays
– linked list
– heap data structure and
– binary search tree
• The heap data structure is the most efficient
way of implementing the priority queue.
7/15/2021 5.5 _ Types of Queue and its Applications 144
145. Priority Queue Operations
• The common operations that we can perform
on a priority queue are
– Insertion
– deletion and
– peek
7/15/2021 5.5 _ Types of Queue and its Applications 145
147. What is Heap?
• A heap is a tree-based data structure that forms a
complete binary tree, and satisfies the heap property.
• If A is a parent node of B, then A is ordered with
respect to the node B for all nodes A and B in a heap.
• It means that the value of the parent node could be
more than or equal to the value of the child node
or
• the value of the parent node could be less than or
equal to the value of the child node.
• Therefore, we can say that there are two types of
heap.
– Max Heap
– Min Heap
7/15/2021 5.5 _ Types of Queue and its Applications 147
148. Max heap
• The max heap is a heap in which the value of
the parent node is greater than the value of
the child nodes.
7/15/2021 5.5 _ Types of Queue and its Applications 148
149. Min heap
• The min heap is a heap in which the value of
the parent node is less than the value of the
child nodes.
7/15/2021 5.5 _ Types of Queue and its Applications 149
150. Applications of Priority Queue
• CPU Scheduling
• It is used in the Dijkstra's shortest path algorithm.
• It is used in prim's algorithm
• It is used in data compression techniques like Huffman
code.
• It is used in heap sort.
• It is also used in operating system like
– priority scheduling,
– load balancing and
– interrupt handling.
7/15/2021 5.5 _ Types of Queue and its Applications 150
151. Applications of Queue
• Queue is used when things don’t have to be processed
immediately, but have to be processed in First In First Out order
like Breadth First Search.
• 1) When a resource is shared among multiple consumers. Examples
include CPU scheduling, Disk Scheduling.
2) When data is transferred asynchronously (data not necessarily
received at same rate as sent) between two processes. Examples
include IO Buffers, pipes, file IO, etc.
3) In Operating systems:
a) Semaphores
b) FCFS ( first come first serve) scheduling, example: FIFO queue
c) Spooling in printers
d) Buffer for devices like keyboard
4) In Networks:
a) Queues in routers/ switches
b) Mail Queues
5) Variations: (Deque, Priority Queue, Doubly Ended Priority
Queue )
7/15/2021 5.5 _ Types of Queue and its Applications 151