Learning Linked List
if (!!!true) {
console.log("iff");
} else {
console.log("else");
}
!!!true is false so it is else
if (!!!true) {
console.log("iff");
} else {
console.log("else");
}
output is:- else
!!!true=false
else will execute
!true-> false
!!true → true
!!!true → false
that’s why else will executes
!!!true
!true > false
!!true > true
!!!true > flase
Thats why its print “else”
else will be printed.
if (!!!true) {
console.log("iff");
} else {
console.log("else");
}
!true—>false
else
!!true—>true
iff
!!!true—>false
else
if (!!!true) {
console.log("iff"); // As if statement is false, this line NOT executes
} else {
console.log("else"); // As if statement is false, this line will executes
Output:- else
}```
What is Deque and Dequeue ?
Deque:
The double-ended queue is an abstract data type that generalizes a queue from which elements can be inserted or deleted either from both front(head) or rear(tail) ends.
Deque Data structure
The operations allowed in deque are:
- insert an element at the back
- Insert an element at the front
- delete the element at the back
- delete the element from the front
- get the last element
- get the first element
- check the size of the deque
- check if the deque is empty or not
There are two types of deque:
- Input restricted deque: An input restricted dequeue is a queue in which deletion can be done from both the ends but insertion from the front will not be allowed.
- Output restricted deque: An output restricted dequeue is a queue in which insertion can be done from both the ends but deletion from the rear will not be allowed.
Applications of deque
- Deque can be used as both stack and queue, as it supports both operations.
- Deque can be used as a palindrome checker means that if we read the string from both ends, the string would be the same.
on the other hand dequeue is Queue Deletion operation is also called dequeue. Queue works on FIFO (First In First Out) principle. The element added at the beginning of the queue is deleted first. A new element can be added at REAR of the queue.
The deque stands for Double Ended Queue. Deque is a linear data structure where the insertion and deletion operations are performed from both ends
**The insertion and deletion in a deque can be performed on both ends, it does not follow the FIFO rule. The representation of a deque is given as follows** -
There are two types of deque -
- Input restricted queue
- Output restricted queue
Input restricted Queue:-
In input restricted queue, insertion operation can be performed at only one end, while deletion can be performed from both ends.
Output restricted Queue:-
In output restricted queue, deletion operation can be performed at only one end, while insertion can be performed from both ends
Operations performed on deque:-
There are the following operations that can be applied on a deque -
- Insertion at front
- Insertion at rear
- Deletion at front
- Deletion at rear
We can also perform peek operations in the deque along with the operations listed above. Through peek operation, we can get the deque’s front and rear elements of the deque. So, in addition to the above operations, following operations are also supported in deque -
- Get the front item from the deque
- Get the rear item from the deque
- Check whether the deque is full or not
- Checks whether the deque is empty or not
Thank you so much sir
DEQUE:
Deque or Double Ended Queue is a type of queue in which insertion and removal of elements can either be performed from the front or the rear . Thus, it does not follow FIFO rule (First In First Out).
DEQUEUE:
Dequeue: Remove an element from the front of the queue.
dequeue(){
this.items.shift()
}
class Node {
constructor(value) {
this.value = value; // data
this.next = null; // address
}
}
class LinkedList {
constructor() {
this.head = null;
this.tail = null;
}
append(value) {
// Creating New Node..
const newNode = new Node(value);
// Here we will do the linking code
if (this.head == null && this.tail == null) {
// condition --> Empty LinkedList ( NO NODE as of Now)
this.head = newNode;
this.tail = newNode;
} else {
this.tail.next = newNode;
this.tail = newNode;
}
}
prepend(value) {
// Creating New Node..
const newNode = new Node(value);
if (this.head == null && this.tail == null) {
// condition --> Empty LinkedList ( NO NODE as of Now)
this.head = newNode;
this.tail = newNode;
} else {
newNode.next = this.head;
this.head = newNode;
}
}
print() {
let current = this.head;
const values = [];
while (current) {
values.push(current.value);
current = current.next;
}
console.log(values.join(" -> "));
}
}
const llObject = new LinkedList();
llObject.append(10);
llObject.append(20);
llObject.append(30);
llObject.prepend(40);
llObject.append(50);
llObject.print(); // output
40 → 10 → 20 → 30 → 50
40–>10–>20–>30–>50