Linked List
Linked List
1
Linked Lists
A B C
Head
data pointer
Linked Lists
3
Linked Lists
Types of linked lists:
4
Single linked list
A singly linked list contains two fields in each node - an
information field and the linked field.
•The information field contains the data of that node.
•The link field contains the memory address of the next node.
There is only one link field in each node, the linked list is
called singly linked list.
5
Basic Operations on a list
• Creating a List
• Inserting an element in a
list
• Deleting an element from a
list
• Searching a list
• Reversing a list
Creating a Node
class Node {
// constructor
constructor(element)
{
this.element = element;
this.next = null
}
} class LinkedList {
constructor()
{
this.head = null;
this.size = 0;
}
}
7
add(element)
{
// creates a new node
var node = new Node(element);
// to store current node
var current;
// if list is Empty add the
// element and make it head
if (this.head == null)
this.head = node;
else {
current = this.head;
// iterate to the end of the
// list
while (current.next==NULL) {
current = current.next;
}
// add node
current.next = node;
}
this.size++;
}
Illustration: Insertion
A B C
Item to be
tmp X inserted
A B C
curr
X
Insertion of Node at Beginning
10
Insertion of Node at Beginning
var node = new Node(element);
else{
node.next= this.head;
this.head = node;
return this.head; }
11
Insertion of Node at given
location
12
Insertion of Node at random
location (element ,index)
var node = new Node(element);
13
Insertion of Node at given
location (element ,index)
if (index == 0) {
node.next = this.head; prev = curr;
this.head = node; curr = curr.next;
} }
else {
curr = this.head;
var it = 0; // adding an element
node.next = curr;
// iterate over the list to find prev.next = node;
the position to insert }
while (it < index) { this.size++;
it++; }
}
14
15
Deletion at Beginning
if(!this.head){
return null;
}
this.head = this.head.next;
return this.head;
}
16
Deletion at End
17
Deletion at End
if(!this.head){
return null; while(tail.next !== null)
} {
previous = tail;
// if only one node in the list tail = tail.next;
if(this.head.next==null) }
{
this.head = null; previous.next = null;
return; return this.head;
} }
18
19
Deletion of Node from given
location
20
Deletion of Node from given
location (index)
if (index > 0 && index > this.size)
return -1; while (it < index) {
else { it++;
var curr, prev, it = 0; prev = curr;
curr = this.head; curr = curr.next;
}
// deleting first element // remove the element
if (index == = 0) { prev.next = curr.next;
this.head = curr.next; }
} this.size--;
else {
// iterate over the list to the // return the remove element
position to remoce an element return curr.element;
}
}
21
Doubly linked list
It is a linked list in which each node is points both to the next
node and also to the previous node.
In doubly linked list each node contains three parts:
◦ NEXT : It is a pointer field that contains the address of the next node
◦ PREV: It is a pointer field that contains the address of the previous
node.
◦ INFO: It contains the actual data.
In the first node, if PREV contains NULL, it indicated that it is the
first
node in the list.
The node in which NEXT contains, NULL indicates that the node is the
last node.
22
Operations of Doubly linked
list
1. Create list
2. Insertion in the linked list
3. Deletion in the linked list
4. Traverse Linked list
23
Creating a Node
class Node {
// constructor
constructor(element)
{
this.element = element;
this.next = null
this.previous = null
class LinkedList {
}
constructor()
}
{
this.head = null;
this.tail = null;
this.size = 0;
}
}
24
Creation of linked list
25
// create the first node
var firstnode = new Node(12);
this.head =firstnode;
if(head==NULL)
{
this.head = node;
this.tail = node;
}
else
{
node.prev = tail;
tail.next = node;
this.tail = node;
}
this.size++;
27
Insertion of Node at Beginning
28
Insertion of Node at Beginning
var node = new Node(data); // Insertion at head
// List is currently empty else{
if (this.head === null) { node.next = this.head;
this.head = node; this.head.prev = node;
this.tail = node; this.head = node;
this.size++; return this.head;
return this.head; }
}
29
Insertion of Node at given
location (element ,index)
30
Insertion of Node at given
location (element ,index)
var node = new Node(data); // Insertion at head
// List is currently empty if(index==1)
if (this.head === null) { {
this.head = node; node.next = this.head;
this.tail = node; this.head.prev = node;
this.size++; this.head = node;
return this.head; return this.head;
} }
31
Insertion of Node at given
location (element ,index)
var iter = 1;
var curr = this.head;
while (iter < index)
{
curr = curr.next;
iter++;
}
curr.prev.next=node;
node.prev=curr.prev;
node.next = curr;
curr.prev=node;
32
Deletion at Beginning
33
Deletion at Beginning
if(!this.head)
{
return false;
}
else if(this.head.next=NULL)
{
this.head=NULL;
this.head=NULL
}
this.head = this.head.next;
return this.head;
this.head.prev=NULL;
}
34
Deletion at End
if(head==NULL)
{
return false;
}
else if(this.head.next=NULL)
{
this.head=NULL;
this.head=NULL
}
else
{
tail.prev =tail;
}
this.size--;
35
Deletion of Node from given
location (index)
36
Deletion of Node from given
location (index)
// List is currently empty // Insertion at head
if (this.head === null) { if(index==1)
this.head = null; {
this.tail = null; this.head = this.head.next;
return this.head;
this.size++;
this.head.prev=NULL;
return this.head;
}
}
37
Deletion of Node from given
location (index)
var iter = 1;
var curr = this.head;
while (iter < index)
{
curr = curr.next;
iter++;
}
curr.prev.next=curr.next;
curr.next.prev=curr.prev;
size--;
38
Single circular linked list
The link field of the last node contains the
memory address of the first node, such a linked
list is called circular linked list.
·In a circular linked list every node is accessible
from a given node.
39
Doubly circular linked list
40