FSR170823 :- DSA Questions | Queue | LIFO & FIFO

Q.s 1:- What is Queue data structure and give Technical example of Queue.
Q.s 2:- Explain LIFO and FIFO with respective data structure
Q.s 3 :- Write an Implementation of Queue in Javascript

  1. A queue is a data structure that follows the First in First Out (FIFO) principle, meaning that the first element added to the queue will be the first one to be removed.

Example: Imagine we have a printer that multiple users want to use to print there documents. A queue data structure can be used to manage the print jobs in a sequence and orderly manner.

  1. LIFO-Last in first out is a data structure means the last element added to the structure is the first one to be removed.

FIFO-First in first out is a data structure means the first element added is the first one to be removed.

LIFO data Structure- STACK
FIFO data Structure- QUEUE

1 Like
  1. Queue is an ADT which is used to perform first in first out operation it also used in printers where the first request will be resolved following the other

  2. LIFO means Last in first out. where, a data is taken out and in from the end
    FIFO Means means First in First Out where a data is taken out in the first but added in the end

class queue {
    constructor() {
        this.items = [];
        this.length = 0;
        this.inqueue = function (el) {
            this.items.push(el);
            this.length++
        }
        this.outqueue = function () {
            if (this.length === 0) {
                console.log(-1)
            }
            this.items.shift();
        }
  
    }
    read() {
        console.log(this.items, this.length);
    }
}


let a = new queue();
a.inqueue(1)
a.inqueue("Bhuvan")
a.inqueue(3)
a.outqueue()

a.read()

1 Like

1.Queue is a linear data structure. It follows FIFO-first in first out.
Technical examples of Queue is printer management ,Traffic systems, Call centres.

2.LIFO-Last In First Out: It uses stack data structure .The addition/removal of elements can happen only at the end.
FIFO-First In First Out: It uses queue data structure .The addition of elements happen only at the end .The removal of elements happen only at the start.

1 Like
  1. What is Queue data structure and give Technical examples of Queue.
    Ans: Queue is the linear data structure that uses a FIFO (First In First Out) Principle. In a queue, elements are added at the rear end and removed from the front end
    Example: Making an HTTP request to the server

  2. Explain LIFO and FIFO with respective data structure
    Ans: LIFO is a Last In First Out Principle, Stack uses a LIFO principle, where the elements added at the last are removed first. FIFO is the First In First Out Principle, used in Queue, where the elements inserted at the rear end are deleted first.

  3. Write an Implementation of Queue in Javascript
    Ans:

class Queue {
    constructor() {
        this.queue = [];
        this.size = 0;
        this.enqueue = function (x) {
            this.queue.push(x);
            this.size++;
        }
        this.dequeue = function () {
            if (this.size == 0) {
                console.log('Queue is empty');
            }
            this.size--;
            this.queue.shift();
        }
        this.front = function () {
            if (this.size == 0) {
                console.log('Queue is Empty');
            }
            console.log(`First Element in the queue is ${this.queue[0]}`);
        }
    }
    read() {
        console.log(this.queue);
    }
}

let q = new Queue();
q.enqueue(1);
q.enqueue(2);
q.enqueue(3);
q.enqueue(4);
q.enqueue(5);
q.front();
q.read();
q.dequeue();
q.read();
q.front();
1 Like

Q3. Implementation of Queue in Javascript.

```
class Queue {
    constructor() {
        this.items = [];
        this.length = 0;
        this.enqueue = function (element) {
            this.items.push(element);
            this.length += 1;
        }

        this.dequeue = function () {
            if (this.length == 0) {
                console.log(-1);
            }
            this.length--
            return this.items.shift();
        }


        this.front = function () {
            if (this.length == 0) {
                return-1;
            }

            console.log(this.items[0]);
        }

        this.search = function (element) {
            if (this.items.includes(element)) {
                console.log(`${element} is at index of ${this.items.indexOf(element)}`);
            }
            else {
                return-1;
            }
        }




        this.delete = function (element) {
            //this.length--;
            this.items.splice(element, 1);

        }



    }
    read() {
        console.log(this.items, this.length);
    }
}

let x = new Queue;
x.enqueue("vinzencho");
x.enqueue("money_heist");
x.enqueue("professor");
x.enqueue("berlin");
x.enqueue("palermo");
x.dequeue();
x.front();
x.search("berlin");
x.search("tokyo");
x.delete(0);
x.read();












```
```
1 Like
  1. A Queue is defined as a linear data structure that is open at both ends and the operations are performed in First In First Out (FIFO) order.
    eg:- consider a queue in data structure example. Here, a line of people is waiting to buy a ticket at a cinema hall**. A new person will join the line from the end and the person standing at the front will be the first to get the ticket and leave the line.

2.The Last-In, First-Out (LIFO) method assumes that the last unit to arrive in inventory or more recent is sold first.
eg:- An example of LIFO (Last In, First Out) would be a stack of plates. The last plate placed on the stack would be the first plate taken off.

First-In-First-Out (FIFO) method assumes that the oldest unit of inventory is the sold first.
eg:-People enter a line (queue) to get to the Ticket Counter in an organized manner. The person to enter the queue first, will get the ticket first and leave the queue. The person entering the queue next will get the ticket after the person in front of him.

1 Like

1
Queue : A queue is a linear data structure that stores the elements sequentially . It uses the FIFO approach (First In First Out) for accessing elements.
example:
Queues are typically used to manage threads in multithreading and implementing priority queuing systems.

2.LIFO : is known as last in first out used in Stack data structure .
example: a stack of books that you arrange in a pile .
FIFO : is known as first in first out used in Queue data structure .
example: waiting infront of elevator.
3.

class Queue{
 constructor(){
        this.items=[]
        this.size=0;
        this.enqueue=function(element){
            this.items.push(element);
            this.size+=1;
        }
        this.dequeue=function(){
            if(this.size==0){
                console.log("queue is empty");
            }
            this.size--;
           return this.items.shift();
        }
 this.front =function(){
            if(this.size==0){
                console.log(" Queue is empty ");
            }
           
            console.log(`First element is ${this.items[0]} `);
           }
          
       
 read(){
        console.log(this.items,this.size);

    }
}
let obj= new Queue();
obj.enqueue('Rama');
obj.enqueue('Jyothi');
obj.enqueue('Krishnammagari');
obj.enqueue('Age');
obj.read();
 obj.dequeue();
 obj.dequeue();
obj.front();

   
1 Like

Q1. What is Queue data structure and give a Technical example of a Queue.

A queue is a data structure that follows the FIFO (First-In-First-Out) policy, in which whatever comes first goes out first. This is similar to a line at a ticket booth. The first person in line gets served first, and the last person in line gets served last.

1 Like
3. class Queue {
    constructor() {
        this.items = [];
        this.length = 0;
        this.enqueue = function (element) {
            this.items.push(element);
            this.length += 1;
        }

        this.dequeue = function () {
            if (this.length == 0) {
                console.log(-1);
            }
            this.length--
            return this.items.shift();
        }


        this.front = function () {
            if (this.length == 0) {
                return-1;
            }

            console.log(this.items[0]);
        }

        this.search = function (element) {
            if (this.items.includes(element)) {
                console.log(`${element} is at index of ${this.items.indexOf(element)}`);
            }
            else {
                return-1;
            }
        }




        this.delete = function (element) {
            //this.length--;
            this.items.splice(element, 1);

        }



    }
    read() {
        console.log(this.items, this.length);
    }
}

let x = new Queue;
x.enqueue("michan");
x.enqueue("shizuka");
x.enqueue("giyan");
x.enqueue("suneo");
x.enqueue("hiral");
x.dequeue();
x.front();
x.search("hiral");
x.search("janardhan reddy");
x.delete(0);
x.read();
here
1 Like

Q2. Explain LIFO and FIFO with respective data structures.

LIFO stands for ā€˜last in, first outā€™ and uses a stack data structure. In a LIFO data structure, the newest element added to the stack is first. On the other hand, FIFO stands for ā€˜first in, first outā€™ and uses a queue data structure. In a FIFO data structure, the first element added to the queue is processed first.

1 Like

ans 3

class Queue {
    constructor() {
        this.items = {};
        this.headIndex = 0;
        this.tailIndex = 0;
    }

    enqueue(element) {
        this.items[this.tailIndex] = element;
        this.tailIndex++;
    }

    dequeue() {
        let removedElement = this.items[this.headIndex];
        delete this.items[this.headIndex];
        this.headIndex++;
        return removedElement;
    }

    peek() {
        let peekElement = this.items[this.headIndex];
        return peekElement;
    }

    size() {
        return this.tailIndex - this.headIndex;
    }

    isEmpty() {
        if (this.tailIndex - this.headIndex == 0) {
            return true;
        }
        else {
            return false;
        }
    }

    clear() {
        this.items = {};
        this.headIndex = 0;
        this.tailIndex = 0;
    }
}
let queue = new Queue();

// add items to queue
queue.enqueue(8);
queue.enqueue(6);
queue.enqueue(4);
queue.enqueue(2);

console.log("Queue after adding items: ");
console.log(queue.items);


queue.dequeue();

console.log("Queue after deleting the first item:");
console.log(queue.items);

console.log("First item of the queue = " + queue.peek());

queue.clear();

console.log("After clearing the queue: ");
console.log(queue.items);// program to implement queue data structure
class Queue {
    constructor() {
        this.items = {};
        this.headIndex = 0;
        this.tailIndex = 0;
    }

    enqueue(element) {
        this.items[this.tailIndex] = element;
        this.tailIndex++;
    }

    dequeue() {
        let removedElement = this.items[this.headIndex];
        delete this.items[this.headIndex];
        this.headIndex++;
        return removedElement;
    }

    peek() {
        let peekElement = this.items[this.headIndex];
        return peekElement;
    }

    size() {
        return this.tailIndex - this.headIndex;
    }

    isEmpty() {
        if (this.tailIndex - this.headIndex == 0) {
            return true;
        }
        else {
            return false;
        }
    }

    clear() {
        this.items = {};
        this.headIndex = 0;
        this.tailIndex = 0;
    }
}
let queue = new Queue();
queue.enqueue(8);
queue.enqueue(6);
queue.enqueue(4);
queue.enqueue(2);

console.log("Queue after adding items: ");
console.log(queue.items);

queue.dequeue();

console.log("Queue after deleting the first item:");
console.log(queue.items);

console.log("First item of the queue = " + queue.peek());

queue.clear();

console.log("After clearing the queue: ");
console.log(queue.items);
1 Like

Q.1)----- A linear data structure that is open at both ends and the operations are performed in First In First Out is called queue data structureā€¦(FIFO).

Q.2)----- LIFO is one of the methods of processing data. LIFO works on the principle that the items that entered the last are the first to be removed.
---------FIFO-First in first out is a data structure means the first element added is the first one to be removed.

LIFO data Structureā€”STACK
FIFO data Structureā€” QUEUE

1 Like
  1. Queue is linear data structure. The elements are added Front as well as rear in Queue. It follow the FIFO principle.
    ex: cashier line in store

2)LIFO- last in first out,elements which added at last that will be removed first.
FIFO -First in First out, elements which added in rear that will be deleted first.

class Queue{
constructor(){
this.item = [];
this.length = 0;
this.enqueue = function(element){
this.item.push(element);
this.length += 1;
}
this.dequeue = function(){
if(this.length == 0){
console.log(-1);
}
this.lengthā€“
return this.item.shift();
}
this.front = function(){
if(this.length == 0){
console.log(-1);
}
console.log(this.item[0]);
}
this.search = function(element){
if(this.item.includes(element)){
console.log(${element} is at index ${this.item.indexOf(element)})
}
else{
console.log(-1);
}
}
}
read(){
console.log(this.item,this.length);
}
}

let x=new Queue();
x.enqueue(ā€œhiiā€);
x.enqueue(ā€œhelloā€);
x.enqueue(ā€œgoodā€);
x.enqueue(ā€œmorningā€);
x.enqueue(23);
x.enqueue(55);
x.enqueue(ā€œtanuā€);
x.enqueue(ā€œhaviā€);
x.search(ā€œhaviā€);
x.search(ā€œhiiā€);
x.search(ā€œshanuā€);
// x.dequeue();
// x.dequeue();
x.front();
x.read();

1 Like

Q1ā€“A queue is a linear data structure that stores and manipulate elements sequentially.
example-in a computer we are connect many devices like printer, scanner, to processing some task that device need to a system or processer that time they send request to system that time who is sending first request he will provide first service like first come first service.

Q2ā€“LIFO is last in fist out it is a one ended structure.it means the object are insert and remove in one side that why last in first out. stack follows LIFO.
FIFO is a two ended structure in that insert in one end and removing in another end that why it is first in first out . queue follows FIFO.
Q3ā€“
class Queue1{
constructor(){
this.data=[];
this.size=0;
this.enqueue=function(ele){
this.data.push(ele);
this.size+=1;

    }
    this.dequeue=function(ele){
        if(this.size==0){
            console.log(-1);
        }
       this.size--;
        return this.data.shift();


    }

read(){
console.log(this.items,this.length);

}

let z= new Queue();
z.enqueue(ā€œxerryā€);

z.enqueue(ā€œwarryā€);
z.dequeue();
z.read();

1 Like

class Queue{
constructor(){
this.items=[];
this.length=0;
this.enqueue=function(a){
this.items.push(a);
this.length += 1;
}
this.dequeue=function(){
if(this.length==0){
console.log(-1);
}
this.lengthā€“;
return this.items.shift()
}
this.front=function(){
if(this.length==0){
console.log(-1);
}
console.log(this.items[0]);
}
}
read(){
console.log(this.items,this.length);
}
}
let x=new Queue();
x.enqueue(ā€œvalliā€);
x.enqueue(ā€œjyoā€);
x.enqueue(ā€œanuā€);
x.dequeue();
x.front();
x.read();

1 Like