DSA - Queue Flashcards
what is DS
way to store and organize data
is Queue linear DS
yea queue is linear data str
is queue abstract DS
yes, queue is an abstract DS
what is FIFO
first in first out
what are operation on queue
enqueue, dequeue
what is enqueue
inserting node from rear end is known as enquque
what is dequeue
deleting node from front end part is known as dequeue
what are front and rear
front and rear are two variables
what does front part is used for
front part is used for deleting operation
what is use of rear variable
we use rear variable to add data into the queue
from which node we can insert data
we can insert data from rear end
from which part we can delete data in queue
we can delete data from front part in queue
suppose there are three variables, 2, 3, 4 where would front and rear point
front will point to 2 and
rear will point to 4, end of queue
suppose there are three variables, 2, 3, 4 and now you want to insert data into queue, from where will you insert it
we can insert data into queue from rear end, which is pointing to last element
whats the first step when inserting / enqueing element into queue
first we increment the variable, rear++
insert data => second step
what is LILO
LILO is last in last out
do we have to pass parameter into enqueue()
yes we have to pass data, we are inserting into enqueue() function
do we have to pass anything into dequeue() function
no we dont have to pass anython into dequeue()
why we dont pass anything into dequeue()
by default the element is getting deleted is from front part
what is front() or peek() function
it gives us first element without deleting the element
isFull()
this function will return true if queue is empty, else it will return false
isEmpty()
if queue is empty this function will return true, else this function will return false
for which values we initialize front and rear
we initialize front and rear to -1
which variable we increment while doing enqueue()
we increment rear variable while insert elements
which variable stays at the same position while inserting element to queue
front stays at its place while incrementing
what happens when we dequeue an element
we increment the value value of front
peek() returns what
peek() function returns position where front is pointintg
when front and rear become equal
when there is only one element, then the front and rear points to the same position
what is meaning of
#define n 5
define N, makes global variable and add 5 to it
what is first thing we check into enqueue() function
first thing we check in enqueue() function, is that is queue is overflow or not
overflow means, is queue is full or not
what are applications of queue
serving request from single source
in OS processes are placed into queue
why we need to pass int x, to enqueue() function
into x, we are going to send information or data
how to take array for queue
int queue[5];
we can use
int queue[N];
take two variables and initialize them to -1
int front = -1;
int rear = -1;
do we need to declare front and rear globally
yes, we have to declare front and rear globally
define enqueue() function
void enqueue(int x) {}
how to check overflow of queue
if(rear == N - 1) {
printf(āoverflow or queue is full \nā);
}
how to check this second condition
else if(rear == -1 && front == -1){
front = rear = 0;
queue[rear] = x;
}
what is second we check in enqueue function
in enqueue function second thing we had to check is that, is queue is full empty or not, is that front and rear has -1 values, means that queue is not having any elementyet
what is third case in enqueue function
in this third condition, queue is not empty, queue is not overflow or not full filled
we handle this in third case
what to do, when queue is not full or queue is not completely empty.
write code for third conditionā¦
else{
rear++;
queue[rear];
}
} // enqueue ends here
what is first case we have to check in dequeue() function
first thing we have to check in dequeue function is that, is queue is empty or not
declare dequeue function
void dequeue()ā
what if queue is empty when we call dequeue function
if we call dequeue function, then its underflow condition
how to check if queue is empty or not, what variables we have to use
we have to use both front and rear variables to check if queue is empty or not
how to write condition
if(front == -1 && rear == -1){
printf(āqueue is empty that is underflow case \nā);
}
what is second condition we have to check in dequeue function
if queue is having one element or not
why do we write
front = rear = -1;
in second condition of dequeue function
if there is only element, then after deleting it, front and rear should be having -1 value into it
how to check if queue is having only one element
else if(front == rear) {
front = rear = -1;
}
what we check in third condition
if there are more than one element in queue
write third condition for dequeue function
else{
front++;
}
how to print dequeued element
before doing front++, we have to print element
write code for displaying dequeued elemetn
void dequeue(){
// if queue is empty
// if queue has only one element
// more than one element
if(front == -1 && queue == -1){
// empty queue
} else if(front == rear) {
// one element only
front = rear = -1;
} else {
printf(%dā, queue[front]);
}
}
why we write front++ in dequeue
to delete we have to increment the value of front variable