SlideShare a Scribd company logo
1 of 72
LINKED LIST
Introduction
ο‚— A linked list is a linear data structure, in which the
elements are not stored at contiguous memory
locations. The elements in a linked list are linked
using pointers as shown in the below image.
ο‚— Linked List can be defined as collection of objects
called nodes that are randomly stored in the
memory.
ο‚— Each node holds its own data and the address of
the next node hence forming a chain like structure.
Advantages of Linked Lists
ο‚— They are a dynamic in nature which allocates the
memory when required.
ο‚— The list is not required to be contiguously present
in the memory. The node can reside any where in the
memory and linked together to make a list. This
achieves optimized utilization of space.
ο‚— List size is limited to the memory size and doesn't
need to be declared in advance.
ο‚— Insertion and deletion operations can be easily
implemented.
ο‚— Stacks and queues can be easily implemented.
Disadvantages of Linked Lists
ο‚— The memory is wasted as pointers require extra
memory for storage.
ο‚— No element can be accessed randomly; it has to
access each node sequentially.
ο‚— Not cache friendly. Since array elements are
contiguous locations, there is locality of reference
which is not there in case of linked lists.
ο‚— Reverse Traversing is difficult in linked list.
Applications of Linked Lists
ο‚— Linked lists are used to implement stacks, queues,
graphs, etc.
ο‚— Polynomial representation
ο‚— Representation of sparse matrices
ο‚— Symbol table creation
ο‚— Mailing list
ο‚— Memory management
ο‚— Linked lists let you insert elements at the beginning
and end of the list.
ο‚— In Linked Lists we don't need to know the size in
advance.
Basic Operations
Following are the basic operations supported by a list.
ο‚— Insertion βˆ’ Adds an element in the list.
ο‚— Deletion βˆ’ Deletes an element from the list.
ο‚— Display βˆ’ Displays the complete list.
ο‚— Search βˆ’ Searches an element using the given key.
ο‚— Delete βˆ’ Deletes an element using the given key.
https://visualgo.net/en/list?slide=1
Representation of Linked List
ο‚— There are two ways to implement a linked list.
ο‚— Static implementation (Using Array)
ο‚— Dynamic implantation (Linked list)
Linked List Implementation using
Array
Linked List Implementation using
Dynamic memory allocation
ο‚— A linked list is a collection of nodes. Each node
consists of two part.
ο‚— Data
ο‚— Address of next node in the list
ο‚— The Head will store the address of first node in the list.
Types of Linked Lists
There are 3 different implementations of Linked List
available, they are:
ο‚— Singly Linked List
ο‚— Doubly Linked List
ο‚— Circular Linked List
Let's know more about them and how they are
different from each other.
Singly Linked List
ο‚— Singly linked lists contain nodes which have a data
part as well as an address part i.e. next, which points
to the next node in the sequence of nodes.
ο‚— A singly linked list is formed when many such nodes
are linked together to form a chain. Each node points
to the next node present in the order. The first node is
always used as a reference to traverse the list and is
called HEAD. The last node points to NULL.
Declaring a Linked list
In C language, a linked list can be implemented using
structure and pointers.
struct LinkedList
{
int data;
struct LinkedList *next;
};
ο‚— The above definition is used to create every node in the list.
The data field stores the element and the next is a pointer
to store the address of the next node.
ο‚— Noticed something unusual with next?
ο‚— In place of a data type, struct LinkedList is written before
next. That's because its a self-referencing pointer. It
means a pointer that points to whatever it is a part of. Here
next is a part of a node and it will point to the next node.
Inserting a node in singly
linked list
A node can be added in three ways
1) At the front (beginning) of the linked list
2) After a given node.
3) At the end of the linked list.
Insertion at the beginning of the
list
ο‚— In the first case, we make a new node and points its
next to the head of the existing list and then change
the head to the newly added node. It is similar to
picture given below.
ο‚— Time complexity of this algorithm is O(1) as it does
constant amount of work.
Insertion of a node after a given
node
ο‚— Time complexity of this algorithm is O(n) as n can
be any position.
Insertion at the end of the linked
list
ο‚— Time complexity of this algorithm is O(n) where n is
the number of nodes in linked list. Since there is a loop
from head to end, the function does O(n) work.
This method can also be optimized to work in O(1) by
keeping an extra pointer to tail (end) of the linked list.
ISRO CS 2014
Consider a single linked list where F and L are
pointers to the first and last elements respectively of
the linked list. The time for performing which of the
given operations depends on the length of the linked
list?
ο‚— Delete the first element of the list
ο‚— Interchange the first two elements of the list
ο‚— Delete the last element of the list
ο‚— Add an element at the end of the list
GATE CS 2020
What is the worst case time complexity of inserting n
elements into an empty linked list, if the linked list
needs to be maintained in sorted order?
ο‚— Θ(n)
ο‚— Θ(n log n)
ο‚— Θ(n2)
ο‚— Θ(1)
Deleting a node from the singly linked
list
A node can be deleted in three ways from a singly
linked list:
1) From the front (beginning) of the linked list
2) After a given node.
3) From the end of the linked list.
Deleting the first node of the
linked list
ο‚— Time complexity of this algorithm is O(1) as it does
constant amount of work.
Deleting the nth node of the linked
list
ο‚— To delete a node from linked list, we need to do
following steps.
1) Find previous node of the node to be deleted.
2) Change the next of previous node.
3) Free memory for the node to be deleted.
ο‚— Time complexity of this algorithm is O(n) where n is
the number of nodes in linked list. Since there is a
loop from head to end, the function does O(n) work.
Deleting the last node of the linked
list
ο‚— Time complexity of this algorithm is O(n) where n is
the number of nodes in linked list. Since there is a
loop from head to end, the function does O(n) work.
Traversing the singly linked
list
1. Create a temporary variable for traversing. Assign
reference of head node to it, say temp = head.
2. Repeat below step till temp!= NULL.
3. temp->data contains the current node data. You can
print it or can perform some calculation on it.
4. Once done, move to next node using temp = temp-
>next;.
5. Go back to 2nd step.
Time complexity of this algorithm is O(n) where n is the
number of nodes in linked list. Since there is a loop
from head to end, the function does O(n) work.
GATE-IT-2004
What does the following function do for a given Linked
List with first node as head?
void fun1(struct node* head) {
if(head == NULL) return;
fun1(head->next);
printf("%d ", head->data);
}
ο‚— Prints all nodes of linked lists
ο‚— Prints all nodes of linked list in reverse order
ο‚— Prints alternate nodes of Linked List
ο‚— Prints alternate nodes in reverse order
Searches an element using the
given key
Search an element in linked list is fairly similar to how you search
an element in arrays. Here we will learn to perform linear
search on singly linked list.
1. Input element to search from user. Store it in some variable
say keyToSearch.
2. Declare two variable one to store index of found element and
other to iterate through list. Say index = 0; and struct node
*curNode = head;
3. If curNode is not NULL and its data is not equal to
keyToSearch. Then, increment index and move curNode to its
next node.
4. Repeat step 3 till curNode != NULL and element is not found,
otherwise move to 5th step.
5. If curNode is not NULL, then element is found hence return
index otherwise -1.
ο‚— Time complexity of this algorithm is O(n) where n is the
number of nodes in linked list. Since there is a loop from head
GATE CS 2020
Let P be a singly linked list. Let Q be the pointer to an
intermediate node x in the list. What is the worst-
case time complexity of the best known algorithm to
delete the node Q from the list?
ο‚— O(n)
ο‚— O(log2 n)
ο‚— O(logn)
ο‚— O(1)
Fast solution is to copy the data from the next node to
the node to be deleted and delete the next node.
UGC NET CS 2016 Aug – II
Consider an implementation of unsorted single linked
list. Suppose it has its representation with a head
and a tail pointer (i.e. pointers to the first and last
nodes of the linked list). Given the representation,
which of the following operation can not be
implemented in O(1) time ?
ο‚— Insertion at the front of the linked list.
ο‚— Insertion at the end of the linked list.
ο‚— Deletion of the front node of the linked list.
ο‚— Deletion of the last node of the linked list.
Doubly Linked List
Doubly linked list is a type of linked list in which each
node apart from storing its data has two links. The
first link points to the previous node in the list and the
second link points to the next node in the list. The first
node of the list has its previous link pointing to NULL
similarly the last node of the list has its next node
pointing to NULL.
XOR List Representation
ο‚— A memory-efficient version of Doubly Linked List that can be
created using only one space for the address field with
every node.
ο‚— The List uses bitwise XOR operation to save space for one
address. In the XOR linked list, instead of storing actual
memory addresses, every node stores the XOR of
addresses of previous and next nodes.
ο‚— Node A: npx = 0 XOR add(B) //bitwise XOR of zero and address
of B
ο‚— Node B: npx = add(A) XOR add(C) // bitwise XOR of address of A
and address of C
ο‚— Node C: npx = add(B) XOR add(D) // bitwise XOR of address of
Basic Operations
Following are the basic operations supported by a list.
ο‚— Insertion βˆ’ Adds an element at the beginning of the
list.
ο‚— Deletion βˆ’ Deletes an element at the beginning of
the list.
ο‚— Insert Last βˆ’ Adds an element at the end of the list.
ο‚— Delete Last βˆ’ Deletes an element from the end of
the list.
ο‚— Insert After βˆ’ Adds an element after an item of the
list.
ο‚— Delete βˆ’ Deletes an element from the list using the
key.
ο‚— Display forward βˆ’ Displays the complete list in a
Advantages over singly linked list
1) A DLL can be traversed in both forward and
backward direction.
2) The delete operation in DLL is more efficient if
pointer to the node to be deleted is given.
3) We can quickly insert a new node before a given
node.
In singly linked list, to delete a node, pointer to the
previous node is needed. To get this previous node,
sometimes the list is traversed. In DLL, we can get
the previous node using previous pointer.
Disadvantages over singly linked
list
1) Every node of DLL Require extra space for an
previous pointer. It is possible to implement DLL
with single pointer though.
2) All operations require an extra pointer previous to
be maintained. For example, in insertion, we need
to modify previous pointers together with next
pointers. For example in following functions for
insertions at different positions, we need 1 or 2
extra steps to set previous pointer.
Add a node at the front
Algorithm
Step 1 - Create a newNode with given value and newNode β†’
prev as NULL.
Step 2 - Check whether list is Empty (head == NULL)
Step 3 - If it is Empty then, assign NULL to newNode β†’
next and newNode to head.
Step 4 - If it is not Empty then, assign head to newNode β†’
next and newNode to head.
Inserting at the end of the list
Algorithm
Step 1 - Create a newNode with given value
and newNode β†’ next as NULL.
Step 2 - Check whether list is Empty (head == NULL)
Step 3 - If it is Empty, then assign NULL to newNode
β†’ previous and newNode to head.
Step 4 - If it is not Empty, then, define a node
pointer temp and initialize with head.
Step 5 - Keep moving the temp to its next node until it
reaches to the last node in the list (until temp β†’
next is equal to NULL).
Step 6 - Assign newNode to temp β†’
next and temp to newNode β†’ previous.
Inserting At Specific location in
the list (After a Node)
Step 1 - Create a newNode with given value.
Step 2 - Check whether list is Empty (head == NULL)
Step 3 - If it is Empty then, assign NULL to both newNode β†’
previous & newNode β†’ next and set newNode to head.
Step 4 - If it is not Empty then, define two node
pointers temp1 & temp2 and initialize temp1 with head.
Step 5 - Keep moving the temp1 to its next node until it reaches to the
node after which we want to insert the newNode (until temp1 β†’ data is
equal to location, here location is the node value after which we want
to insert the newNode).
Step 6 - Every time check whether temp1 is reached to the last node. If it
is reached to the last node then display 'Given node is not found in
the list!!! Insertion not possible!!!' and terminate the function.
Otherwise move the temp1 to next node.
Step 7 - Assign temp1 β†’ next to temp2, newNode to temp1 β†’
next, temp1 to newNode β†’ previous, temp2 to newNode β†’
ISRO CS 2017 - May
ο‚— In a doubly linked list, the number of pointers affected
for an insertion operation will be:
ο‚— 4
ο‚— 0
ο‚— 2
ο‚— 1
ISRO CS 2008
Which of the following operations is performed more
efficiently by doubly linked list than by linear linked list?
ο‚— Deleting a node whose location is given
ο‚— Searching an unsorted list for a given item
ο‚— Inserting a node after the node with a given location
ο‚— Traversing the list to process each node
Deletion Operations in Doubly
Linked List
In a double linked list, the deletion operation can be
performed in three ways as follows...
ο‚— Deleting from Beginning of the list
ο‚— Deleting from End of the list
ο‚— Deleting a Specific Node
Deletion from Beginning of the list
Step 1 - Check whether list is Empty (head == NULL)
Step 2 - If it is Empty then, display 'List is Empty!!! Deletion is not
possible' and terminate the function.
Step 3 - If it is not Empty then, define a Node pointer 'temp' and initialize
with head.
Step 4 - Check whether list is having only one node (temp β†’ previous is
equal to temp β†’ next)
Step 5 - If it is TRUE, then set head to NULL and
delete temp (Setting Empty list conditions)
Step 6 - If it is FALSE, then assign temp β†’ next to head, NULL to head
β†’ previous and delete temp.
Deleting a Specific Node from the
list
Step 1 - Check whether list is Empty (head == NULL)
Step 2 - If it is Empty then, display 'List is Empty!!! Deletion is not
possible' and terminate the function.
Step 3 - If it is not Empty, then define a Node pointer 'temp' and initialize
with head.
Step 4 - Keep moving the temp until it reaches to the exact node to be
deleted or to the last node.
Step 5 - If it is reached to the last node, then display 'Given node not
found in the list! Deletion not possible!!!' and terminate the fuction.
Deletion from End of the list
ο‚— Step 1 - Check whether list is Empty (head == NULL)
ο‚— Step 2 - If it is Empty, then display 'List is Empty!!! Deletion is not
possible' and terminate the function.
ο‚— Step 3 - If it is not Empty then, define a Node pointer 'temp' and
initialize with head.
ο‚— Step 4 - Check whether list has only one Node (temp β†’
previous and temp β†’ next both are NULL)
ο‚— Step 5 - If it is TRUE, then assign NULL to head and delete temp. And
terminate from the function. (Setting Empty list condition)
ο‚— Step 6 - If it is FALSE, then keep moving temp until it reaches to the
last node in the list. (until temp β†’ next is equal to NULL)
ο‚— Step 7 - Assign NULL to temp β†’ previous β†’ next and delete temp.
Displaying a Double Linked
List
Step 1 - Check whether list is Empty (head == NULL)
Step 2 - If it is Empty, then display 'List is
Empty!!!' and terminate the function.
Step 3 - If it is not Empty, then define a Node
pointer 'temp' and initialize with head.
Step 4 - Display 'NULL <--- '.
Step 5 - Keep displaying temp β†’ data with an arrow
(<===>) until temp reaches to the last node
Step 6 - Finally, display temp β†’ data with arrow
pointing to NULL (temp β†’ data ---> NULL).
ISRO CS 2018
ο‚— A doubly linked list is declared as
struct Node {
int Value;
struct Node *Fwd;
struct Node *Bwd;
};
Which of the following segments of code deletes the
node pointed to by X from the doubly linked list, if it is
assumed that X points to neither the first nor the last
node of the list?
ο‚— X->Bwd->Fwd = X->Fwd; X->Fwd->Bwd = X->Bwd ;
ο‚— X->Bwd.Fwd = X->Fwd ; X.Fwd->Bwd = X->Bwd ;
ο‚— X.Bwd->Fwd = X.Bwd ; X->Fwd.Bwd = X.Bwd ;
ο‚— X->Bwd->Fwd = X->Bwd ; X->Fwd->Bwd = X->Fwd;
Circular Linked List
ο‚— In single linked list, every node points to its next node
in the sequence and the last node points NULL. But in
circular linked list, every node points to its next node in
the sequence but the last node points to the first node
in the list.
ο‚— A circular linked list is a sequence of elements in
which every element has a link to its next element
in the sequence and the last element has a link to
the first element.
ο‚— That means circular linked list is similar to the single
linked list except that the last node points to the first
node in the list.
Advantages of Circular Linked
Lists
1) Any node can be a starting point. We can traverse the
whole list by starting from any point. We just need to stop
when the first visited node is visited again.
2) Useful for implementation of queue.
Unlike this implementation, we don’t need to maintain two
pointers for front and rear if we use circular linked list. We
can maintain a pointer to the last inserted node and front
can always be obtained as next of last.
3) Circular lists are useful in applications to repeatedly go
around the list. For example, when multiple applications are
running on a PC, it is common for the operating system to
put the running applications on a list and then to cycle
through them, giving each of them a slice of time to execute,
and then making them wait while the CPU is given to
another application. It is convenient for the operating system
to use a circular list so that when it reaches the end of the
list it can cycle around to the front of the list.
4) Circular Doubly Linked Lists are used for implementation of
advanced data structures like Fibonacci Heap.
Operations
In a circular linked list, we perform the following
operations...
ο‚— Insertion
ο‚— Deletion
ο‚— Display
Insertion
In a circular linked list, the insertion operation can
be performed in three ways. They are as follows...
ο‚— Inserting At Beginning of the list
ο‚— Inserting At End of the list
ο‚— Inserting At Specific location in the list
Inserting At Beginning of the list
Step 1 - Create a newNode with given value.
Step 2 - Check whether list is Empty (head == NULL)
Step 3 - If it is Empty then,
set head = newNode and newNode→next = head .
Step 4 - If it is Not Empty then, define a Node pointer 'temp' and
initialize with 'head'.
Step 5 - Keep moving the 'temp' to its next node until it reaches to the
last node (until 'temp β†’ next == head').
Step 6 - Set 'newNode β†’ next =head', 'head = newNode' and 'temp β†’
next = head'.
Inserting At End of the list
Step 1 - Create a newNode with given value.
Step 2 - Check whether list is Empty (head == NULL).
Step 3 - If it is Empty then, set head = newNode and newNode β†’
next = head.
Step 4 - If it is Not Empty then, define a node pointer temp and initialize
with head.
Step 5 - Keep moving the temp to its next node until it reaches to the last
node in the list (until temp β†’ next == head).
Step 6 - Set temp β†’ next = newNode and newNode β†’ next = head.
Inserting At Specific location in the
list
Deletion Operations
In a circular linked list, the deletion operation can be
performed in three ways those are as follows...
ο‚— Deleting from Beginning of the list
ο‚— Deleting from End of the list
ο‚— Deleting a Specific Node
Deleting from Beginning of the
list
ο‚— Step 1 - Check whether list is Empty (head == NULL)
ο‚— Step 2 - If it is Empty then, display 'List is Empty!!!
Deletion is not possible' and terminate the function.
ο‚— Step 3 - If it is Not Empty then, define two Node
pointers 'temp1' and 'temp2' and initialize both 'temp1'
and 'temp2' with head.
ο‚— Step 4 - Check whether list is having only one node
(temp1 β†’ next == head)
ο‚— Step 5 - If it is TRUE then set head = NULL and
delete temp1 (Setting Empty list conditions)
ο‚— Step 6 - If it is FALSE move the temp1 until it reaches to
the last node. (until temp1 β†’ next == head )
ο‚— Step 7 - Then set head = temp2 β†’ next, temp1 β†’
next = head and delete temp2
Deleting from End of the list
ο‚— Step 1 - Check whether list is Empty (head == NULL)
ο‚— Step 2 - If it is Empty then, display 'List is Empty!!!
Deletion is not possible' and terminate the function.
ο‚— Step 3 - If it is Not Empty then, define two Node
pointers 'temp1' and 'temp2' and initialize 'temp1'
with head.
ο‚— Step 4 - Check whether list has only one Node (temp1
β†’ next == head)
ο‚— Step 5 - If it is TRUE. Then, set head = NULL and
delete temp1. And terminate from the function.
(Setting Empty list condition)
ο‚— Step 6 - If it is FALSE. Then, set 'temp2 = temp1 ' and
move temp1 to its next node. Repeat the same
until temp1 reaches to the last node in the list.
(until temp1 β†’ next == head)
Deleting a Specific Node from the
list
Displaying a circular Linked List
Step 1 - Check whether list is Empty (head == NULL)
Step 2 - If it is Empty, then display 'List is
Empty!!!' and terminate the function.
Step 3 - If it is Not Empty then, define a Node
pointer 'temp' and initialize with head.
Step 4 - Keep displaying temp β†’ data with an arrow
(--->) until temp reaches to the last node
Step 5 - Finally display temp β†’ data with arrow
pointing to head β†’ data.
GATE 2004
ο‚— A circularly linked list is used to represent a Queue.
A single variable p is used to access the Queue. To
which node should p point such that both the
operations enQueue and deQueue can be
performed in constant time?
ο‚— rear node
ο‚— front node
ο‚— not possible with a single pointer
ο‚— node next to front
Polynomial Representation
A polynomial p(x) is the expression in variable x which
is in the form (axn + bxn-1 + …. + jx+ k), where a, b, c
…., k fall in the category of real numbers and 'n' is
non negative integer, which is called the degree of
polynomial.An essential characteristic of the
polynomial is that each term in the polynomial
expression consists of two parts:
ο‚— Coefficient
ο‚— Exponent
Representation of Polynomial
Polynomial can be represented in the various ways.
These are:
ο‚— By the use of arrays
ο‚— By the use of Linked List
Representation of Polynomial
Using Arrays
Addition of Two Polynomial
ο‚— For adding two polynomials using arrays is
straightforward method, since both the arrays may be
added up element wise beginning from 0 to n-1,
resulting in addition of two polynomials.
ο‚— Addition of two polynomials using linked list requires
comparing the exponents, and wherever the exponents
are found to be same, the coefficients are added up.
ο‚— For terms with different exponents, the complete term
is simply added to the result thereby making it a part of
addition result.
Multiplication of Two Polynomial
ο‚— Multiplication of two polynomials however requires
manipulation of each node such that the exponents
are added up and the coefficients are multiplied.
ο‚— After each term of first polynomial is operated upon
with each term of the second polynomial, then the
result has to be added up by comparing the
exponents and adding the coefficients for similar
exponents and including terms as such with
dissimilar exponents in the result.
Representation of Polynomial
Using Linked List
To understand this concept better let's first brush up all the
basic contents that are required.
ο‚— linked list is a data structure that stores each element as
an object in a node of the list. every note contains two parts
data han and links to the next node.
ο‚— Polynomial is a mathematical expression that consists of
variables and coefficients. for example x^2 - 4x + 7
ο‚— In the Polynomial linked list, the coefficients and
exponents of the polynomial are defined as the data node of
the list.
ο‚— For adding two polynomials that are stored as a linked list.
We need to add the coefficients of variables with the same
power. In a linked list node contains 3 members, coefficient
value link to the next node. A linked list that is used to store
Polynomial looks like βˆ’ 4x7 + 12x2 + 45
Addition of two polynomials using
Linked list
ο‚— Adding two polynomials that are represented by a
linked list. We check values at the exponent value of
the node. For the same values of exponent, we will
add the coefficients.
ο‚— Example,
Input :p1= 13x8 + 7x5 + 32x2 + 54p2= 3x12 + 17x5 + 3x3 +
98
Output : 3x12 + 13x8 + 24x5 + 3x3 + 32x2 + 152
Multiplication of two polynomials
using Linked list
Two Variables Polynomial
ο‚— Polynomials in two variables are algebraic
expressions consisting of terms in the form axnym . The
degree of each term in a polynomial in two variables is
the sum of the exponents in each term and
the degree of the polynomial is the largest such sum.
ο‚— Here are some examples of polynomials in two
variables and their degrees.
x2yβˆ’6x3y12+10x2βˆ’7y+1 degree : 15
6x4+8y4βˆ’xy2 degree : 4
x4y2βˆ’x3y3βˆ’xy+x4 degree : 6
6x14βˆ’10y3+3xβˆ’11y degree : 14

More Related Content

What's hot

Data Structures - Lecture 7 [Linked List]
Data Structures - Lecture 7 [Linked List]Data Structures - Lecture 7 [Linked List]
Data Structures - Lecture 7 [Linked List]Muhammad Hammad Waseem
Β 
Data Structure and Algorithms Linked List
Data Structure and Algorithms Linked ListData Structure and Algorithms Linked List
Data Structure and Algorithms Linked ListManishPrajapati78
Β 
Binary search tree operations
Binary search tree operationsBinary search tree operations
Binary search tree operationsKamran Zafar
Β 
Circular linked list
Circular linked listCircular linked list
Circular linked listchauhankapil
Β 
Double Linked List (Algorithm)
Double Linked List (Algorithm)Double Linked List (Algorithm)
Double Linked List (Algorithm)Huba Akhtar
Β 
Data Structures with C Linked List
Data Structures with C Linked ListData Structures with C Linked List
Data Structures with C Linked ListReazul Islam
Β 
Singly link list
Singly link listSingly link list
Singly link listRojin Khadka
Β 
Linked List
Linked ListLinked List
Linked ListRaaviKapoor
Β 
Lec 17 heap data structure
Lec 17 heap data structureLec 17 heap data structure
Lec 17 heap data structureSajid Marwat
Β 
Queue_Data_Structure.pptx
Queue_Data_Structure.pptxQueue_Data_Structure.pptx
Queue_Data_Structure.pptxsandeep54552
Β 
Unit 3 graph chapter6
Unit 3  graph chapter6Unit 3  graph chapter6
Unit 3 graph chapter6DrkhanchanaR
Β 

What's hot (20)

Data Structures - Lecture 7 [Linked List]
Data Structures - Lecture 7 [Linked List]Data Structures - Lecture 7 [Linked List]
Data Structures - Lecture 7 [Linked List]
Β 
Data Structure and Algorithms Linked List
Data Structure and Algorithms Linked ListData Structure and Algorithms Linked List
Data Structure and Algorithms Linked List
Β 
Binary search tree operations
Binary search tree operationsBinary search tree operations
Binary search tree operations
Β 
Circular linked list
Circular linked listCircular linked list
Circular linked list
Β 
Doubly Linked List
Doubly Linked ListDoubly Linked List
Doubly Linked List
Β 
Double Linked List (Algorithm)
Double Linked List (Algorithm)Double Linked List (Algorithm)
Double Linked List (Algorithm)
Β 
Data Structures with C Linked List
Data Structures with C Linked ListData Structures with C Linked List
Data Structures with C Linked List
Β 
Singly link list
Singly link listSingly link list
Singly link list
Β 
single linked list
single linked listsingle linked list
single linked list
Β 
linked list
linked list linked list
linked list
Β 
Heaps & priority queues
Heaps & priority queuesHeaps & priority queues
Heaps & priority queues
Β 
Linked List
Linked ListLinked List
Linked List
Β 
Linked List
Linked ListLinked List
Linked List
Β 
Lec 17 heap data structure
Lec 17 heap data structureLec 17 heap data structure
Lec 17 heap data structure
Β 
Queue_Data_Structure.pptx
Queue_Data_Structure.pptxQueue_Data_Structure.pptx
Queue_Data_Structure.pptx
Β 
Heap and heapsort
Heap and heapsortHeap and heapsort
Heap and heapsort
Β 
Linkedlist
LinkedlistLinkedlist
Linkedlist
Β 
Unit 3 graph chapter6
Unit 3  graph chapter6Unit 3  graph chapter6
Unit 3 graph chapter6
Β 
Doubly linked list (animated)
Doubly linked list (animated)Doubly linked list (animated)
Doubly linked list (animated)
Β 
linked list
linked listlinked list
linked list
Β 

Similar to 1.3 Linked List.pptx

Linked list (introduction) 1
Linked list (introduction) 1Linked list (introduction) 1
Linked list (introduction) 1DrSudeshna
Β 
Linked list in Data Structure and Algorithm
Linked list in Data Structure and Algorithm Linked list in Data Structure and Algorithm
Linked list in Data Structure and Algorithm KristinaBorooah
Β 
Operations on linked list
Operations on linked listOperations on linked list
Operations on linked listSumathi Kv
Β 
Algo>ADT list & linked list
Algo>ADT list & linked listAlgo>ADT list & linked list
Algo>ADT list & linked listAin-ul-Moiz Khawaja
Β 
linked list in data structure
linked list in data structure linked list in data structure
linked list in data structure shameen khan
Β 
ds-lecture-4-171012041008 (1).pdf
ds-lecture-4-171012041008 (1).pdfds-lecture-4-171012041008 (1).pdf
ds-lecture-4-171012041008 (1).pdfKamranAli649587
Β 
data structures and applications power p
data structures and applications power pdata structures and applications power p
data structures and applications power pMeghaKulkarni27
Β 
Linkedlists
LinkedlistsLinkedlists
LinkedlistsRajendran
Β 
Link list
Link listLink list
Link listzzzubair
Β 
In this lab we will write code for working with a Linked List. Node .pdf
In this lab we will write code for working with a Linked List.  Node .pdfIn this lab we will write code for working with a Linked List.  Node .pdf
In this lab we will write code for working with a Linked List. Node .pdffms12345
Β 
csc211_lecture_21.pptx
csc211_lecture_21.pptxcsc211_lecture_21.pptx
csc211_lecture_21.pptxASADAHMAD811380
Β 
Ch 1 intriductions
Ch 1 intriductionsCh 1 intriductions
Ch 1 intriductionsirshad17
Β 
Linked List in Data Structure
Linked List in Data StructureLinked List in Data Structure
Linked List in Data StructureMeghaj Mallick
Β 
Deletion from single way linked list and search
Deletion from single way linked list and searchDeletion from single way linked list and search
Deletion from single way linked list and searchEstiak Khan
Β 
linked list using c
linked list using clinked list using c
linked list using cVenkat Reddy
Β 

Similar to 1.3 Linked List.pptx (20)

Linked list (introduction) 1
Linked list (introduction) 1Linked list (introduction) 1
Linked list (introduction) 1
Β 
Linked list in Data Structure and Algorithm
Linked list in Data Structure and Algorithm Linked list in Data Structure and Algorithm
Linked list in Data Structure and Algorithm
Β 
Linked List
Linked ListLinked List
Linked List
Β 
Operations on linked list
Operations on linked listOperations on linked list
Operations on linked list
Β 
Linked List
Linked ListLinked List
Linked List
Β 
Linked list (1).pptx
Linked list (1).pptxLinked list (1).pptx
Linked list (1).pptx
Β 
Algo>ADT list & linked list
Algo>ADT list & linked listAlgo>ADT list & linked list
Algo>ADT list & linked list
Β 
linked list in data structure
linked list in data structure linked list in data structure
linked list in data structure
Β 
ds-lecture-4-171012041008 (1).pdf
ds-lecture-4-171012041008 (1).pdfds-lecture-4-171012041008 (1).pdf
ds-lecture-4-171012041008 (1).pdf
Β 
data structures and applications power p
data structures and applications power pdata structures and applications power p
data structures and applications power p
Β 
Linkedlists
LinkedlistsLinkedlists
Linkedlists
Β 
Link list
Link listLink list
Link list
Β 
In this lab we will write code for working with a Linked List. Node .pdf
In this lab we will write code for working with a Linked List.  Node .pdfIn this lab we will write code for working with a Linked List.  Node .pdf
In this lab we will write code for working with a Linked List. Node .pdf
Β 
csc211_lecture_21.pptx
csc211_lecture_21.pptxcsc211_lecture_21.pptx
csc211_lecture_21.pptx
Β 
Link list assi
Link list assiLink list assi
Link list assi
Β 
Ch 1 intriductions
Ch 1 intriductionsCh 1 intriductions
Ch 1 intriductions
Β 
Linked List in Data Structure
Linked List in Data StructureLinked List in Data Structure
Linked List in Data Structure
Β 
module 3-.pptx
module 3-.pptxmodule 3-.pptx
module 3-.pptx
Β 
Deletion from single way linked list and search
Deletion from single way linked list and searchDeletion from single way linked list and search
Deletion from single way linked list and search
Β 
linked list using c
linked list using clinked list using c
linked list using c
Β 

Recently uploaded

Past, Present and Future of Generative AI
Past, Present and Future of Generative AIPast, Present and Future of Generative AI
Past, Present and Future of Generative AIabhishek36461
Β 
Study on Air-Water & Water-Water Heat Exchange in a Finned ο»ΏTube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned ο»ΏTube ExchangerStudy on Air-Water & Water-Water Heat Exchange in a Finned ο»ΏTube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned ο»ΏTube ExchangerAnamika Sarkar
Β 
πŸ”9953056974πŸ”!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...
πŸ”9953056974πŸ”!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...πŸ”9953056974πŸ”!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...
πŸ”9953056974πŸ”!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...9953056974 Low Rate Call Girls In Saket, Delhi NCR
Β 
Model Call Girl in Narela Delhi reach out to us at πŸ”8264348440πŸ”
Model Call Girl in Narela Delhi reach out to us at πŸ”8264348440πŸ”Model Call Girl in Narela Delhi reach out to us at πŸ”8264348440πŸ”
Model Call Girl in Narela Delhi reach out to us at πŸ”8264348440πŸ”soniya singh
Β 
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...VICTOR MAESTRE RAMIREZ
Β 
Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024hassan khalil
Β 
Internship report on mechanical engineering
Internship report on mechanical engineeringInternship report on mechanical engineering
Internship report on mechanical engineeringmalavadedarshan25
Β 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVRajaP95
Β 
Biology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptxBiology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptxDeepakSakkari2
Β 
microprocessor 8085 and its interfacing
microprocessor 8085  and its interfacingmicroprocessor 8085  and its interfacing
microprocessor 8085 and its interfacingjaychoudhary37
Β 
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSAPPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSKurinjimalarL3
Β 
Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.eptoze12
Β 
Artificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptxArtificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptxbritheesh05
Β 
main PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidmain PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidNikhilNagaraju
Β 
Call Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile serviceCall Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile servicerehmti665
Β 
power system scada applications and uses
power system scada applications and usespower system scada applications and uses
power system scada applications and usesDevarapalliHaritha
Β 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
Β 

Recently uploaded (20)

Past, Present and Future of Generative AI
Past, Present and Future of Generative AIPast, Present and Future of Generative AI
Past, Present and Future of Generative AI
Β 
β˜… CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
β˜… CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCRβ˜… CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
β˜… CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
Β 
Study on Air-Water & Water-Water Heat Exchange in a Finned ο»ΏTube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned ο»ΏTube ExchangerStudy on Air-Water & Water-Water Heat Exchange in a Finned ο»ΏTube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned ο»ΏTube Exchanger
Β 
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptxExploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Β 
πŸ”9953056974πŸ”!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...
πŸ”9953056974πŸ”!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...πŸ”9953056974πŸ”!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...
πŸ”9953056974πŸ”!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...
Β 
Model Call Girl in Narela Delhi reach out to us at πŸ”8264348440πŸ”
Model Call Girl in Narela Delhi reach out to us at πŸ”8264348440πŸ”Model Call Girl in Narela Delhi reach out to us at πŸ”8264348440πŸ”
Model Call Girl in Narela Delhi reach out to us at πŸ”8264348440πŸ”
Β 
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
Β 
Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024
Β 
Internship report on mechanical engineering
Internship report on mechanical engineeringInternship report on mechanical engineering
Internship report on mechanical engineering
Β 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
Β 
Biology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptxBiology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptx
Β 
microprocessor 8085 and its interfacing
microprocessor 8085  and its interfacingmicroprocessor 8085  and its interfacing
microprocessor 8085 and its interfacing
Β 
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSAPPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
Β 
Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.
Β 
Artificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptxArtificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptx
Β 
main PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidmain PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfid
Β 
Call Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile serviceCall Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile service
Β 
power system scada applications and uses
power system scada applications and usespower system scada applications and uses
power system scada applications and uses
Β 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
Β 
young call girls in Rajiv ChowkπŸ” 9953056974 πŸ” Delhi escort Service
young call girls in Rajiv ChowkπŸ” 9953056974 πŸ” Delhi escort Serviceyoung call girls in Rajiv ChowkπŸ” 9953056974 πŸ” Delhi escort Service
young call girls in Rajiv ChowkπŸ” 9953056974 πŸ” Delhi escort Service
Β 

1.3 Linked List.pptx

  • 2. Introduction ο‚— A linked list is a linear data structure, in which the elements are not stored at contiguous memory locations. The elements in a linked list are linked using pointers as shown in the below image. ο‚— Linked List can be defined as collection of objects called nodes that are randomly stored in the memory. ο‚— Each node holds its own data and the address of the next node hence forming a chain like structure.
  • 3. Advantages of Linked Lists ο‚— They are a dynamic in nature which allocates the memory when required. ο‚— The list is not required to be contiguously present in the memory. The node can reside any where in the memory and linked together to make a list. This achieves optimized utilization of space. ο‚— List size is limited to the memory size and doesn't need to be declared in advance. ο‚— Insertion and deletion operations can be easily implemented. ο‚— Stacks and queues can be easily implemented.
  • 4. Disadvantages of Linked Lists ο‚— The memory is wasted as pointers require extra memory for storage. ο‚— No element can be accessed randomly; it has to access each node sequentially. ο‚— Not cache friendly. Since array elements are contiguous locations, there is locality of reference which is not there in case of linked lists. ο‚— Reverse Traversing is difficult in linked list.
  • 5. Applications of Linked Lists ο‚— Linked lists are used to implement stacks, queues, graphs, etc. ο‚— Polynomial representation ο‚— Representation of sparse matrices ο‚— Symbol table creation ο‚— Mailing list ο‚— Memory management ο‚— Linked lists let you insert elements at the beginning and end of the list. ο‚— In Linked Lists we don't need to know the size in advance.
  • 6. Basic Operations Following are the basic operations supported by a list. ο‚— Insertion βˆ’ Adds an element in the list. ο‚— Deletion βˆ’ Deletes an element from the list. ο‚— Display βˆ’ Displays the complete list. ο‚— Search βˆ’ Searches an element using the given key. ο‚— Delete βˆ’ Deletes an element using the given key. https://visualgo.net/en/list?slide=1
  • 7. Representation of Linked List ο‚— There are two ways to implement a linked list. ο‚— Static implementation (Using Array) ο‚— Dynamic implantation (Linked list)
  • 9. Linked List Implementation using Dynamic memory allocation ο‚— A linked list is a collection of nodes. Each node consists of two part. ο‚— Data ο‚— Address of next node in the list ο‚— The Head will store the address of first node in the list.
  • 10. Types of Linked Lists There are 3 different implementations of Linked List available, they are: ο‚— Singly Linked List ο‚— Doubly Linked List ο‚— Circular Linked List Let's know more about them and how they are different from each other.
  • 11. Singly Linked List ο‚— Singly linked lists contain nodes which have a data part as well as an address part i.e. next, which points to the next node in the sequence of nodes. ο‚— A singly linked list is formed when many such nodes are linked together to form a chain. Each node points to the next node present in the order. The first node is always used as a reference to traverse the list and is called HEAD. The last node points to NULL.
  • 12. Declaring a Linked list In C language, a linked list can be implemented using structure and pointers. struct LinkedList { int data; struct LinkedList *next; }; ο‚— The above definition is used to create every node in the list. The data field stores the element and the next is a pointer to store the address of the next node. ο‚— Noticed something unusual with next? ο‚— In place of a data type, struct LinkedList is written before next. That's because its a self-referencing pointer. It means a pointer that points to whatever it is a part of. Here next is a part of a node and it will point to the next node.
  • 13. Inserting a node in singly linked list A node can be added in three ways 1) At the front (beginning) of the linked list 2) After a given node. 3) At the end of the linked list.
  • 14. Insertion at the beginning of the list ο‚— In the first case, we make a new node and points its next to the head of the existing list and then change the head to the newly added node. It is similar to picture given below. ο‚— Time complexity of this algorithm is O(1) as it does constant amount of work.
  • 15. Insertion of a node after a given node ο‚— Time complexity of this algorithm is O(n) as n can be any position.
  • 16. Insertion at the end of the linked list ο‚— Time complexity of this algorithm is O(n) where n is the number of nodes in linked list. Since there is a loop from head to end, the function does O(n) work. This method can also be optimized to work in O(1) by keeping an extra pointer to tail (end) of the linked list.
  • 17. ISRO CS 2014 Consider a single linked list where F and L are pointers to the first and last elements respectively of the linked list. The time for performing which of the given operations depends on the length of the linked list? ο‚— Delete the first element of the list ο‚— Interchange the first two elements of the list ο‚— Delete the last element of the list ο‚— Add an element at the end of the list
  • 18. GATE CS 2020 What is the worst case time complexity of inserting n elements into an empty linked list, if the linked list needs to be maintained in sorted order? ο‚— Θ(n) ο‚— Θ(n log n) ο‚— Θ(n2) ο‚— Θ(1)
  • 19. Deleting a node from the singly linked list A node can be deleted in three ways from a singly linked list: 1) From the front (beginning) of the linked list 2) After a given node. 3) From the end of the linked list.
  • 20. Deleting the first node of the linked list ο‚— Time complexity of this algorithm is O(1) as it does constant amount of work.
  • 21. Deleting the nth node of the linked list ο‚— To delete a node from linked list, we need to do following steps. 1) Find previous node of the node to be deleted. 2) Change the next of previous node. 3) Free memory for the node to be deleted. ο‚— Time complexity of this algorithm is O(n) where n is the number of nodes in linked list. Since there is a loop from head to end, the function does O(n) work.
  • 22. Deleting the last node of the linked list ο‚— Time complexity of this algorithm is O(n) where n is the number of nodes in linked list. Since there is a loop from head to end, the function does O(n) work.
  • 23. Traversing the singly linked list 1. Create a temporary variable for traversing. Assign reference of head node to it, say temp = head. 2. Repeat below step till temp!= NULL. 3. temp->data contains the current node data. You can print it or can perform some calculation on it. 4. Once done, move to next node using temp = temp- >next;. 5. Go back to 2nd step. Time complexity of this algorithm is O(n) where n is the number of nodes in linked list. Since there is a loop from head to end, the function does O(n) work.
  • 24. GATE-IT-2004 What does the following function do for a given Linked List with first node as head? void fun1(struct node* head) { if(head == NULL) return; fun1(head->next); printf("%d ", head->data); } ο‚— Prints all nodes of linked lists ο‚— Prints all nodes of linked list in reverse order ο‚— Prints alternate nodes of Linked List ο‚— Prints alternate nodes in reverse order
  • 25. Searches an element using the given key Search an element in linked list is fairly similar to how you search an element in arrays. Here we will learn to perform linear search on singly linked list. 1. Input element to search from user. Store it in some variable say keyToSearch. 2. Declare two variable one to store index of found element and other to iterate through list. Say index = 0; and struct node *curNode = head; 3. If curNode is not NULL and its data is not equal to keyToSearch. Then, increment index and move curNode to its next node. 4. Repeat step 3 till curNode != NULL and element is not found, otherwise move to 5th step. 5. If curNode is not NULL, then element is found hence return index otherwise -1. ο‚— Time complexity of this algorithm is O(n) where n is the number of nodes in linked list. Since there is a loop from head
  • 26. GATE CS 2020 Let P be a singly linked list. Let Q be the pointer to an intermediate node x in the list. What is the worst- case time complexity of the best known algorithm to delete the node Q from the list? ο‚— O(n) ο‚— O(log2 n) ο‚— O(logn) ο‚— O(1) Fast solution is to copy the data from the next node to the node to be deleted and delete the next node.
  • 27. UGC NET CS 2016 Aug – II Consider an implementation of unsorted single linked list. Suppose it has its representation with a head and a tail pointer (i.e. pointers to the first and last nodes of the linked list). Given the representation, which of the following operation can not be implemented in O(1) time ? ο‚— Insertion at the front of the linked list. ο‚— Insertion at the end of the linked list. ο‚— Deletion of the front node of the linked list. ο‚— Deletion of the last node of the linked list.
  • 28. Doubly Linked List Doubly linked list is a type of linked list in which each node apart from storing its data has two links. The first link points to the previous node in the list and the second link points to the next node in the list. The first node of the list has its previous link pointing to NULL similarly the last node of the list has its next node pointing to NULL.
  • 29. XOR List Representation ο‚— A memory-efficient version of Doubly Linked List that can be created using only one space for the address field with every node. ο‚— The List uses bitwise XOR operation to save space for one address. In the XOR linked list, instead of storing actual memory addresses, every node stores the XOR of addresses of previous and next nodes. ο‚— Node A: npx = 0 XOR add(B) //bitwise XOR of zero and address of B ο‚— Node B: npx = add(A) XOR add(C) // bitwise XOR of address of A and address of C ο‚— Node C: npx = add(B) XOR add(D) // bitwise XOR of address of
  • 30. Basic Operations Following are the basic operations supported by a list. ο‚— Insertion βˆ’ Adds an element at the beginning of the list. ο‚— Deletion βˆ’ Deletes an element at the beginning of the list. ο‚— Insert Last βˆ’ Adds an element at the end of the list. ο‚— Delete Last βˆ’ Deletes an element from the end of the list. ο‚— Insert After βˆ’ Adds an element after an item of the list. ο‚— Delete βˆ’ Deletes an element from the list using the key. ο‚— Display forward βˆ’ Displays the complete list in a
  • 31. Advantages over singly linked list 1) A DLL can be traversed in both forward and backward direction. 2) The delete operation in DLL is more efficient if pointer to the node to be deleted is given. 3) We can quickly insert a new node before a given node. In singly linked list, to delete a node, pointer to the previous node is needed. To get this previous node, sometimes the list is traversed. In DLL, we can get the previous node using previous pointer.
  • 32. Disadvantages over singly linked list 1) Every node of DLL Require extra space for an previous pointer. It is possible to implement DLL with single pointer though. 2) All operations require an extra pointer previous to be maintained. For example, in insertion, we need to modify previous pointers together with next pointers. For example in following functions for insertions at different positions, we need 1 or 2 extra steps to set previous pointer.
  • 33. Add a node at the front Algorithm Step 1 - Create a newNode with given value and newNode β†’ prev as NULL. Step 2 - Check whether list is Empty (head == NULL) Step 3 - If it is Empty then, assign NULL to newNode β†’ next and newNode to head. Step 4 - If it is not Empty then, assign head to newNode β†’ next and newNode to head.
  • 34. Inserting at the end of the list Algorithm Step 1 - Create a newNode with given value and newNode β†’ next as NULL. Step 2 - Check whether list is Empty (head == NULL) Step 3 - If it is Empty, then assign NULL to newNode β†’ previous and newNode to head. Step 4 - If it is not Empty, then, define a node pointer temp and initialize with head. Step 5 - Keep moving the temp to its next node until it reaches to the last node in the list (until temp β†’ next is equal to NULL). Step 6 - Assign newNode to temp β†’ next and temp to newNode β†’ previous.
  • 35.
  • 36. Inserting At Specific location in the list (After a Node) Step 1 - Create a newNode with given value. Step 2 - Check whether list is Empty (head == NULL) Step 3 - If it is Empty then, assign NULL to both newNode β†’ previous & newNode β†’ next and set newNode to head. Step 4 - If it is not Empty then, define two node pointers temp1 & temp2 and initialize temp1 with head. Step 5 - Keep moving the temp1 to its next node until it reaches to the node after which we want to insert the newNode (until temp1 β†’ data is equal to location, here location is the node value after which we want to insert the newNode). Step 6 - Every time check whether temp1 is reached to the last node. If it is reached to the last node then display 'Given node is not found in the list!!! Insertion not possible!!!' and terminate the function. Otherwise move the temp1 to next node. Step 7 - Assign temp1 β†’ next to temp2, newNode to temp1 β†’ next, temp1 to newNode β†’ previous, temp2 to newNode β†’
  • 37.
  • 38. ISRO CS 2017 - May ο‚— In a doubly linked list, the number of pointers affected for an insertion operation will be: ο‚— 4 ο‚— 0 ο‚— 2 ο‚— 1 ISRO CS 2008 Which of the following operations is performed more efficiently by doubly linked list than by linear linked list? ο‚— Deleting a node whose location is given ο‚— Searching an unsorted list for a given item ο‚— Inserting a node after the node with a given location ο‚— Traversing the list to process each node
  • 39. Deletion Operations in Doubly Linked List In a double linked list, the deletion operation can be performed in three ways as follows... ο‚— Deleting from Beginning of the list ο‚— Deleting from End of the list ο‚— Deleting a Specific Node
  • 40. Deletion from Beginning of the list Step 1 - Check whether list is Empty (head == NULL) Step 2 - If it is Empty then, display 'List is Empty!!! Deletion is not possible' and terminate the function. Step 3 - If it is not Empty then, define a Node pointer 'temp' and initialize with head. Step 4 - Check whether list is having only one node (temp β†’ previous is equal to temp β†’ next) Step 5 - If it is TRUE, then set head to NULL and delete temp (Setting Empty list conditions) Step 6 - If it is FALSE, then assign temp β†’ next to head, NULL to head β†’ previous and delete temp.
  • 41. Deleting a Specific Node from the list Step 1 - Check whether list is Empty (head == NULL) Step 2 - If it is Empty then, display 'List is Empty!!! Deletion is not possible' and terminate the function. Step 3 - If it is not Empty, then define a Node pointer 'temp' and initialize with head. Step 4 - Keep moving the temp until it reaches to the exact node to be deleted or to the last node. Step 5 - If it is reached to the last node, then display 'Given node not found in the list! Deletion not possible!!!' and terminate the fuction.
  • 42. Deletion from End of the list ο‚— Step 1 - Check whether list is Empty (head == NULL) ο‚— Step 2 - If it is Empty, then display 'List is Empty!!! Deletion is not possible' and terminate the function. ο‚— Step 3 - If it is not Empty then, define a Node pointer 'temp' and initialize with head. ο‚— Step 4 - Check whether list has only one Node (temp β†’ previous and temp β†’ next both are NULL) ο‚— Step 5 - If it is TRUE, then assign NULL to head and delete temp. And terminate from the function. (Setting Empty list condition) ο‚— Step 6 - If it is FALSE, then keep moving temp until it reaches to the last node in the list. (until temp β†’ next is equal to NULL) ο‚— Step 7 - Assign NULL to temp β†’ previous β†’ next and delete temp.
  • 43. Displaying a Double Linked List Step 1 - Check whether list is Empty (head == NULL) Step 2 - If it is Empty, then display 'List is Empty!!!' and terminate the function. Step 3 - If it is not Empty, then define a Node pointer 'temp' and initialize with head. Step 4 - Display 'NULL <--- '. Step 5 - Keep displaying temp β†’ data with an arrow (<===>) until temp reaches to the last node Step 6 - Finally, display temp β†’ data with arrow pointing to NULL (temp β†’ data ---> NULL).
  • 44. ISRO CS 2018 ο‚— A doubly linked list is declared as struct Node { int Value; struct Node *Fwd; struct Node *Bwd; }; Which of the following segments of code deletes the node pointed to by X from the doubly linked list, if it is assumed that X points to neither the first nor the last node of the list? ο‚— X->Bwd->Fwd = X->Fwd; X->Fwd->Bwd = X->Bwd ; ο‚— X->Bwd.Fwd = X->Fwd ; X.Fwd->Bwd = X->Bwd ; ο‚— X.Bwd->Fwd = X.Bwd ; X->Fwd.Bwd = X.Bwd ; ο‚— X->Bwd->Fwd = X->Bwd ; X->Fwd->Bwd = X->Fwd;
  • 45. Circular Linked List ο‚— In single linked list, every node points to its next node in the sequence and the last node points NULL. But in circular linked list, every node points to its next node in the sequence but the last node points to the first node in the list. ο‚— A circular linked list is a sequence of elements in which every element has a link to its next element in the sequence and the last element has a link to the first element. ο‚— That means circular linked list is similar to the single linked list except that the last node points to the first node in the list.
  • 46. Advantages of Circular Linked Lists 1) Any node can be a starting point. We can traverse the whole list by starting from any point. We just need to stop when the first visited node is visited again. 2) Useful for implementation of queue. Unlike this implementation, we don’t need to maintain two pointers for front and rear if we use circular linked list. We can maintain a pointer to the last inserted node and front can always be obtained as next of last. 3) Circular lists are useful in applications to repeatedly go around the list. For example, when multiple applications are running on a PC, it is common for the operating system to put the running applications on a list and then to cycle through them, giving each of them a slice of time to execute, and then making them wait while the CPU is given to another application. It is convenient for the operating system to use a circular list so that when it reaches the end of the list it can cycle around to the front of the list. 4) Circular Doubly Linked Lists are used for implementation of advanced data structures like Fibonacci Heap.
  • 47. Operations In a circular linked list, we perform the following operations... ο‚— Insertion ο‚— Deletion ο‚— Display
  • 48. Insertion In a circular linked list, the insertion operation can be performed in three ways. They are as follows... ο‚— Inserting At Beginning of the list ο‚— Inserting At End of the list ο‚— Inserting At Specific location in the list
  • 49. Inserting At Beginning of the list Step 1 - Create a newNode with given value. Step 2 - Check whether list is Empty (head == NULL) Step 3 - If it is Empty then, set head = newNode and newNodeβ†’next = head . Step 4 - If it is Not Empty then, define a Node pointer 'temp' and initialize with 'head'. Step 5 - Keep moving the 'temp' to its next node until it reaches to the last node (until 'temp β†’ next == head'). Step 6 - Set 'newNode β†’ next =head', 'head = newNode' and 'temp β†’ next = head'.
  • 50. Inserting At End of the list Step 1 - Create a newNode with given value. Step 2 - Check whether list is Empty (head == NULL). Step 3 - If it is Empty then, set head = newNode and newNode β†’ next = head. Step 4 - If it is Not Empty then, define a node pointer temp and initialize with head. Step 5 - Keep moving the temp to its next node until it reaches to the last node in the list (until temp β†’ next == head). Step 6 - Set temp β†’ next = newNode and newNode β†’ next = head.
  • 51. Inserting At Specific location in the list
  • 52. Deletion Operations In a circular linked list, the deletion operation can be performed in three ways those are as follows... ο‚— Deleting from Beginning of the list ο‚— Deleting from End of the list ο‚— Deleting a Specific Node
  • 53. Deleting from Beginning of the list ο‚— Step 1 - Check whether list is Empty (head == NULL) ο‚— Step 2 - If it is Empty then, display 'List is Empty!!! Deletion is not possible' and terminate the function. ο‚— Step 3 - If it is Not Empty then, define two Node pointers 'temp1' and 'temp2' and initialize both 'temp1' and 'temp2' with head. ο‚— Step 4 - Check whether list is having only one node (temp1 β†’ next == head) ο‚— Step 5 - If it is TRUE then set head = NULL and delete temp1 (Setting Empty list conditions) ο‚— Step 6 - If it is FALSE move the temp1 until it reaches to the last node. (until temp1 β†’ next == head ) ο‚— Step 7 - Then set head = temp2 β†’ next, temp1 β†’ next = head and delete temp2
  • 54.
  • 55. Deleting from End of the list ο‚— Step 1 - Check whether list is Empty (head == NULL) ο‚— Step 2 - If it is Empty then, display 'List is Empty!!! Deletion is not possible' and terminate the function. ο‚— Step 3 - If it is Not Empty then, define two Node pointers 'temp1' and 'temp2' and initialize 'temp1' with head. ο‚— Step 4 - Check whether list has only one Node (temp1 β†’ next == head) ο‚— Step 5 - If it is TRUE. Then, set head = NULL and delete temp1. And terminate from the function. (Setting Empty list condition) ο‚— Step 6 - If it is FALSE. Then, set 'temp2 = temp1 ' and move temp1 to its next node. Repeat the same until temp1 reaches to the last node in the list. (until temp1 β†’ next == head)
  • 56.
  • 57. Deleting a Specific Node from the list
  • 58. Displaying a circular Linked List Step 1 - Check whether list is Empty (head == NULL) Step 2 - If it is Empty, then display 'List is Empty!!!' and terminate the function. Step 3 - If it is Not Empty then, define a Node pointer 'temp' and initialize with head. Step 4 - Keep displaying temp β†’ data with an arrow (--->) until temp reaches to the last node Step 5 - Finally display temp β†’ data with arrow pointing to head β†’ data.
  • 59. GATE 2004 ο‚— A circularly linked list is used to represent a Queue. A single variable p is used to access the Queue. To which node should p point such that both the operations enQueue and deQueue can be performed in constant time? ο‚— rear node ο‚— front node ο‚— not possible with a single pointer ο‚— node next to front
  • 60. Polynomial Representation A polynomial p(x) is the expression in variable x which is in the form (axn + bxn-1 + …. + jx+ k), where a, b, c …., k fall in the category of real numbers and 'n' is non negative integer, which is called the degree of polynomial.An essential characteristic of the polynomial is that each term in the polynomial expression consists of two parts: ο‚— Coefficient ο‚— Exponent
  • 61. Representation of Polynomial Polynomial can be represented in the various ways. These are: ο‚— By the use of arrays ο‚— By the use of Linked List
  • 63. Addition of Two Polynomial ο‚— For adding two polynomials using arrays is straightforward method, since both the arrays may be added up element wise beginning from 0 to n-1, resulting in addition of two polynomials. ο‚— Addition of two polynomials using linked list requires comparing the exponents, and wherever the exponents are found to be same, the coefficients are added up. ο‚— For terms with different exponents, the complete term is simply added to the result thereby making it a part of addition result.
  • 64.
  • 65. Multiplication of Two Polynomial ο‚— Multiplication of two polynomials however requires manipulation of each node such that the exponents are added up and the coefficients are multiplied. ο‚— After each term of first polynomial is operated upon with each term of the second polynomial, then the result has to be added up by comparing the exponents and adding the coefficients for similar exponents and including terms as such with dissimilar exponents in the result.
  • 66.
  • 67. Representation of Polynomial Using Linked List To understand this concept better let's first brush up all the basic contents that are required. ο‚— linked list is a data structure that stores each element as an object in a node of the list. every note contains two parts data han and links to the next node. ο‚— Polynomial is a mathematical expression that consists of variables and coefficients. for example x^2 - 4x + 7 ο‚— In the Polynomial linked list, the coefficients and exponents of the polynomial are defined as the data node of the list. ο‚— For adding two polynomials that are stored as a linked list. We need to add the coefficients of variables with the same power. In a linked list node contains 3 members, coefficient value link to the next node. A linked list that is used to store Polynomial looks like βˆ’ 4x7 + 12x2 + 45
  • 68.
  • 69. Addition of two polynomials using Linked list ο‚— Adding two polynomials that are represented by a linked list. We check values at the exponent value of the node. For the same values of exponent, we will add the coefficients. ο‚— Example, Input :p1= 13x8 + 7x5 + 32x2 + 54p2= 3x12 + 17x5 + 3x3 + 98 Output : 3x12 + 13x8 + 24x5 + 3x3 + 32x2 + 152
  • 70.
  • 71. Multiplication of two polynomials using Linked list
  • 72. Two Variables Polynomial ο‚— Polynomials in two variables are algebraic expressions consisting of terms in the form axnym . The degree of each term in a polynomial in two variables is the sum of the exponents in each term and the degree of the polynomial is the largest such sum. ο‚— Here are some examples of polynomials in two variables and their degrees. x2yβˆ’6x3y12+10x2βˆ’7y+1 degree : 15 6x4+8y4βˆ’xy2 degree : 4 x4y2βˆ’x3y3βˆ’xy+x4 degree : 6 6x14βˆ’10y3+3xβˆ’11y degree : 14