0% found this document useful (0 votes)
28 views116 pages

Linked List

A linked list is a linear data structure consisting of nodes that store data and pointers to the next node, allowing for dynamic memory allocation. There are several types of linked lists, including singly, circular, doubly, and doubly circular linked lists, each with its own advantages and disadvantages in terms of memory usage and traversal capabilities. Linked lists are particularly useful for implementing other data structures like stacks and queues due to their ease of insertion and deletion operations.

Uploaded by

patelsajan204
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views116 pages

Linked List

A linked list is a linear data structure consisting of nodes that store data and pointers to the next node, allowing for dynamic memory allocation. There are several types of linked lists, including singly, circular, doubly, and doubly circular linked lists, each with its own advantages and disadvantages in terms of memory usage and traversal capabilities. Linked lists are particularly useful for implementing other data structures like stacks and queues due to their ease of insertion and deletion operations.

Uploaded by

patelsajan204
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 116

Unit 2

Linked List
What is Linked List ?
• Linked list is a linear data structure.
• It is a collection of data elements, called nodes pointing
to the next node by means of a pointer.

Data Next

10 Pointe
r

Node
Linked List
• A node is a collection of two sub-elements or parts.
• A data part that stores the element and a next part that
stores the link to the next node.
• A linked list is a non primitive type of data structure in
which each element is dynamically allocated and in
which elements point to each other to define a linear
relationship.
• Linked list require more memory compared to array
because along with value it stores pointer to next node.
• Linked lists are among the simplest and most common
data structures. They can be used to implement other
data structures like stacks, queues, and symbolic
expressions,
Node Implementation
• The first node is always use as a reference to
traverse the list and is called HEAD.
• The last nod points to NULL.
Types of linked list
• Singly Linked List
• Circular Linked List
• Doubly Linked List
• Doubly Circular Linked List
Singly Linked List
• It is basic type of linked list.
• Each node contains data and pointer to next node.
• It does not store any pointer or reference to the
previous node.
• It has successor and predecessor.
• First node does not have any predecessor while last
node does not have any successor.
• Last node has successor reference as NULL.
• Limitation of singly linked list is we can traverse only
in one direction, forward direction.
• In this type of linked list , only forward sequential
movement is possible, no direct access is allowed.
Singly Linked List
• First node of Linked List is denoted by First or
Head node.
Circular Linked List
• Circular linked list is a singly linked list where last
node points to first node in the list.
• It does not contain null pointers like singly linked
list.
• We can traverse only in one direction that is
forward direction.
Circular Linked List
• It has the biggest advantage of time saving when
we want to go from last node to first node, it
directly points to first node.
• A good example of an application where circular
linked list should be used is a timesharing
problem solved by the operating system.
Doubly Linked list
• Each node of doubly linked list contains data and
two pointers to point previous (LPTR) and next
(RPTR) node.
Doubly Linked list
• L and R in image denotes left most and right most
nodes in the list.
• Left link of L node and right link of R node is
NULL, indicating the end of list for each direction.
Advantages of Doubly
Linked
• Main advantage of doubly linked list is we can
traverse in any direction, forward or reverse.
Drawback of Doubly Linked
list
• Drawback of doubly linked list is it requires more
memory compared to singly linked list because
we need an extra pointer to point previous node.
• We can delete a node with little trouble , since we
have pointers to the previous and next nodes.
Doubly Circular
Linked List
• Circular Doubly Linked List has properties of both
doubly linked list and circular linked list in which
two consecutive elements are linked or connected
by previous and next pointer and the last node
points to first node by next pointer and also the
first node points to last node by previous pointer.
Advantages and disadvantages
of linked list over array
• Advantages of an array
• We can access any element of an array directly
means random access is easy
• It can be used to create other useful data
structures (queues, stacks)
• It is light on memory usage compared to other
structures
• Disadvantages of an array
• Its size is fixed
• It cannot be dynamically resized in most
languages
• It is hard to add/remove elements
• Size of all elements must be same.
• Rigid structure (Rigid = Inflexible or not
changeable)
Advantages and disadvantages
of linked list over array
• Advantages of Linked List
• Dynamic size
• It is easy to add/remove/change elements
• Elements of linked list are flexible, it can be
primary data type or user defined data types
• Disadvantages of Linked List
• Random access is not allowed. We have to access
elements sequentially starting from the first node.
So we cannot do binary search with linked lists.
• It cannot be easily sorted.
• We must traverse 1/2 the list on average to
access any element.
• More complex to create than an array.
• Extra memory space for a pointer is required with
each element of the list.
Linked List Sequential List/
Array
1. In linked list number of 1. in sequential list number of
elements in the list is not fixed. elements in list is fixed.
2. In linked list allocation of 2. In sequential list allocation of
memory is done at runtime. memory is done at compile time.
3. Insertion and deletion 3. Insertion and deletion
operation are very easy and less operation are very lengthy and
time consuming in linked list. time consuming in sequential list
4. In linked list we require 4. In sequential list there is no
pointer variable which occupies need to
extra memory space. use pointer variable so it does
not occupies extra memory
space.
5.Searching in a linked list is 5.Searching is less time
very time consuming because we consuming in
have to traverse entire list even sequential list because we can
if all the elements in the list are use binary search method to
sorted. search an element which is very
efficient
6.In linked list the elements 6. In sequential list elements are
Advantages and disadvantages of stack and

queue implemented using linked list over

array?
Insertion & Deletion Operation
• Insertion and deletion operations are known as push
and pop operation in stack and as insert and delete
operation in queue.
• In the case of an array, if we have n elements list and
it is required to insert a new element between the first
and second element then n- 1 elements of the list
must be moved so as to make room for the new
element.
• In case of linked list, this can be accomplished by only
interchanging pointers.
• Thus, insertion and deletions are more efficient when
performed in linked list then array.
• Searching a node
• If a particular node in a linked list is required, it is
necessary to follow links from the first node onwards
until the desired node is found.
• Where as in the case of an array, directly we can
access any node.
• Join & Split
• We can join two linked list by assigning pointer of
second linked list in the last node of first linked list.
• Just assign null address in the node from where we
want to split one linked list in two parts.
• Joining and splitting of two arrays is much more difficult
compared to linked list.
• Memory
• The pointers in linked list consume additional
memory compared to an array
• Size
• Array is fixed sized so number of elements will be
limited in stack and queue.
• Size of linked list is dynamic and can be changed
easily so it is flexible in number of elements.
Operations on Linked
list
• Traversing a linked list.
• Insert new node at beginning of the list
• Insert new node at end of the list
• Insert new node into ordered list
• Insert new node at any position or in between the list.
• Delete first node of the list
• Delete last node of the list
• Delete node from any specific position in the list
• Searching element in list.
• Merging of two linked list
• Sorting operation of list
SINGLY LINKED
LIST
beginning
of the list
• Steps:
• Create a Node
• Set the node data Values
• Connect the pointers
o Make link of new node as first

• move the First to point to the new node


beginning
of the list
beginning
of the list
• INSERTBEG (VAL,FIRST)
• This function inserts a new element VAL at the
beginning of the linked list. Val is new element.
• FIRST is a pointer which contains address of first
node in the list.
• Avail is a pointer to the top element of the
availability stack.
• NEW is a temporary pointer variable. It is required
that X precedes the node whose address is given
by FIRST.
• 1. [Check for availability stack underflow]
• If AVAIL = NULL then
• Write “Availability stack underflow”
• Return(First)
• 2. [Obtain address of next free node]
• NEWAVAIL
• 3. [Remove free node from availability stack]
• AVAILLINK (AVAIL)
• 4. [Initialize node to the linked list]
• INFO (NEW) VAL
• LINK (NEW) FIRST
• 5. [Assign the address of the Temporary node to
the First Node ]
• FIRSTNEW
• 6. [Finished]
• Return (FIRST)
• These steps are used to create New Node in
every insertion algorithm of linked list.
• C Code: struct Node* new_node = (struct Node*)
malloc (sizeof (struct Node));
Insertion at
beginning
• void insertbeg(struct Node* First, int val)
• {
• /* 1. allocate node */
• struct Node* new_node = (struct Node*)
• malloc(sizeof(struct Node));
• /* 2. put in the data */
• new_node->data = val;
• /* 3. Make next of new node as head */
• new_node->next = First;
• /* 4. move the head to point to the new node */
• First = new_node;
• }
the
list
• Steps:
• Create a Node
• Set the node data Values
• This new node is going to be the last node, so
make link of it as NULL
• If the Linked List is empty, then make the new
node as first
• Else traverse till the last node
• Change the link of last node
the
list
the
list
INSERTEND (VAL,FIRST)
• This function inserts a new element VAL at the
end of the linked list. Val is new element.
• FIRST is a pointer which contains address of first
node in the list.
• Avail is a pointer to the top element of the
availability stack.
• NEW and SAVE is a temporary pointer variable. It
is required that X be inserted at the end of list.
1. [Check for availability stack underflow]
• If AVAIL = NULL then
Write “Availability stack underflow”
Return
2. [Obtain address of next free node]
• NEWAVAIL
3. [Remove free node from availability stack]
• AVAILLINK (AVAIL)
4. [initialize field of new node]
• INFO (NEW) VAL
• LINK (NEW) NULL
5. [If list is empty?]
• If FIRST = NULL then
FIRSTNEW
Return (FIRST)
6. [initialize search for last node]
• SAVEFIRST
7. [Search end of the list]
• Repeat while LINK (SAVE) ≠ NULL
SAVELINK (SAVE)
8. [Set LINK field of last node to NEW ]
• LINK (SAVE) NEW
9. [Finished]
• Return (FIRST)15, 1002 first = 1000
16,1004 17,null
the
list
• void append(struct Node* First, int new_data) {
• /* 1. allocate node */
• struct Node* new_node = (struct Node*) malloc(sizeof(struct Node));
• struct Node *Save = First; /* used in step 5*/
• /* 2. put in the data */
• new_node->data = new_data;
• /* 3. This new node is going to be the last node, so make next of it as NULL*/
• new_node->next = NULL;
• /* 4. If the Linked List is empty, then make the new node as head */
• if (First == NULL) {
• First = new_node; return; }
• /* 5. Else traverse till the last node */
• while (save->next != NULL)
• save = save->next;
• /* 6. Change the next of last node */
• save->next = new_node; return; }
Insertion in the middle
• Steps:
• Create a Node
• Set the node data Values
• Break pointer connection
• Re-connect the pointers
ordered
list
• INSORD (VAL, FIRST)
• This function inserts a new element VAL into the ordered linked
list.
• FIRST is a pointer which contains address of first node in the list.
1. [Check for availability stack underflow]
• If AVAIL = NULL then
Write “Availability stack underflow”
Return
2. [Obtain address of next free node]
• NEWAVAIL
3. [Remove free node from availability stack]
• AVAILLINK (AVAIL)
4. [initialize field of new node]
• INFO (NEW) VAL
5. [If list is empty?]
• If FIRST = NULL then
LINK (NEW) NULL
FIRSTNEW
Return(FIRST)
6. [Does the new node precede all nodes in the list?]
• If INFO (NEW) ≤ INFO (FIRST) then
LINK (NEW) FIRST
FIRSTNEW
Return(FIRST)
7. [Initialize search pointer]
• SAVEFIRST
8. [Search for predecessor of new node]
• Repeat while LINK (SAVE) ≠ NULL and INFO (LINK
(SAVE))≤ INFO (NEW)
SAVELINK (SAVE)
9. [Set LINK field of new node and its predecessor ]
• LINK (NEW) LINK (SAVE)
• LINK (SAVE) NEW
10. [Finished]
• Return (FIRST) info(new)=35
link(new)=2000
10, 1002 20,1004 30,1006 40,1008
position or in between
the list
• INSPOS (VAL, FIRST, N)
• This function inserts a new element VAL into the linked list
specified by address N.
• FIRST is a pointer which contains address of first node in the list.
1. [Check for availability stack underflow]
• If AVAIL = NULL then
Write “Availability stack underflow”
Return
2. [Obtain address of next free node]
• NEWAVAIL
3. [Remove free node from availability stack]
• AVAILLINK (AVAIL)
4. [initialize field of new node]
• INFO (NEW) VAL
5. [If list is empty?]
• If FIRST = NULL then
LINK (NEW) NULL
FIRSTNEW
6. [Search the list until desired address found]
• SAVEFIRST
• Repeat while LINK (SAVE) ≠ NULL and SAVE ≠ N
PREDSAVE
SAVELINK (SAVE)
7. [Node found]
• LINK (PRED) NEW
8. [Finished]
• Return (FIRST)
after
x 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, set newNode → next =
NULL and head = newNode.
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 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 temp is reached
to last node or not. If it is reached to last node then
display 'Given node is not found in the list!!!
Insertion not possible!!!' and terminate the
function. Otherwise move the temp to next node.
Step 7: Finally, Set 'newNode → next = temp →
next' and 'temp → next = newNode'
after
x node
• Given a node prev_node, insert a new node after
the given prev_node
void insertAfter(struct Node* prev_node, int new_data)
{
/*1. check if the given prev_node is NULL */
if (prev_node == NULL)
{
printf("the given previous node cannot be NULL");
return;
}
/* 2. allocate new node */
struct Node* new_node =(struct Node*) malloc(sizeof(struct
Node));
/* 3. put in the data */
new_node->data = new_data;
/* 4. Make next of new node as next of
prev_node */
new_node->next = prev_node->next;
/* 5. move the next of prev_node as new_node
*/
prev_node->next = new_node;
}
Delete node from the
list
• 1) Find previous node of the node to be
• deleted.
• 2) Changed next of previous node.
• 3) Free memory for the node to be deleted.
Delete first node of the
list
• Steps
• Break the pointer connection
• Re-connect the nodes
• Delete the node
Delete first node of the
list
Delete first node of the
list
• DELFIRST(FIRST)
• This function deletes a first node from the list.
• FIRST is a pointer which contains address of first
node in the list.
1. [Check for empty list]
• If FIRST = NULL then
Write “List is empty”
Return (First)
2. [Check for the element in the list and delete it]
• If LINK (FIRST) = NULL then
YINFO (FIRST)
FIRSTNULL
• Else
TEMPFIRST
YINFO (TEMP)
FIRSTLINK (TEMP)
3. [Finished]
• Return (FIRST)
Steps to free node
• LINK(X)  AVAIL
• AVAILX
• return
Delete last node of the
list
Steps
• Break the pointer connection
• Set previous node pointer to NULL
• Delete the node
Delete last node of the
list
Delete last node of the
list
• DELLAST(FIRST)
• This function deletes a last node from the list.
• FIRST is a pointer which contains address of first node in
the list.
1. [Check for empty list]
• If FIRST = NULL then
Write “List is empty”
Return
2. [Check for the element in the list and delete it]
• If LINK (FIRST) = NULL then
YINFO (FIRST)
FIRSTNULL
Return (FIRST)
Else
(Assign the address pointed by FIRST pointer
to TEMP pointer)
TEMPFIRST
Repeat while LINK (TEMP) ≠ NULL
PREDTEMP
TEMPLINK (TEMP)
3. [Delete Last Node]
• YINFO (TEMP)
• LINK (PRED) NULL
4. [Finished]
• Return (FIRST)
Deleting from the
Middle
Steps
• Set previous Node pointer to next node
• Break Node pointer connection
• Delete the node
specific
position in the list
• DELPOS(FIRST,N)
• This function deletes a node from specific location from the list.
• FIRST is a pointer which contains address of first node in the list.
1. [Check for empty list]
• If FIRST = NULL then
Write “List is empty”
Return
2. [If there is only one node?]
• If LINK (FIRST) = NULL then
YINFO (FIRST)
FIRSTNULL
3. [If list contains more than one node?]
• TEMPFIRST
• Repeat while LINK (TEMP) ≠ NULL and TEMP ≠ N
PREDTEMP
TEMPLINK (TEMP)
4. [Node found?]
• If TEMP ≠ N then
Write “Node not found”
• Else
If N=First then
YINFO (FIRST)
FIRST LINK(FIRST)
Else
YINFO (TEMP)
LINK(PRED)  LINK(TEMP)
5. [Finished]
• Return (FIRST)
Copy list
COPY(FIRST)
• This function copy one list into another list.
• FIRST is a pointer which contains address of first node in the
list.
• BEGIN is a pointer which points to the address of first node in
the list.
1. [Empty List?]
• If FIRST = NULL then
BEGINNULL
Return(BEGIN)
2. [Copy First Node]
• If AVAIL = NULL then
Write “Availability stack underflow”
Return(0)
• Else
NEWAVAIL
AVAILLINK (AVAIL)
FIELD (NEW) INFO (FIRST)
BEGINNEW
3. [Initialize traversal]
• SAVEFIRST
4. [Move to next node if not at end of the list]
• Repeat thru step 6 while LINK (SAVE) ≠ NULL
5. [Update predecessor and save pointers]
• PREDNEW
• SAVELINK (SAVE)
6. [Copy node]
• If AVAIL = NULL then
Write “availability stack underflow”
Return(0)
• Else
NEWAVAIL
AVAILLINK (AVAIL)
FIELD (NEW) INFO (SAVE)
PTR (PRED) NEW
7. [Set link of last node and return]
• PTR (NEW) NULL
• Return(BEGIN)
CIRCULAR LINKED
LIST
E elemen
Insert at First Position
• CIRCULAR_LINK_INSERT_FIRST (X, FIRST,
LAST)
• A new element is X
• FIRST and LAST a pointer to the first and last
element of a linked linear list respectively whose
typical node contains INFO and LINK fields.
• AVAIL is a pointer to the top element of the
availability stack
• NEW is a temporary points variable.
• This function inserts X. It is required that X
precedes the node whose address is given by
FIRST.
Insert at First Position
1. [Create New Empty Node]
• NEW  NODE *
2. [Initialize fields of new node and its link to the list]
• INFO (NEW) X
• If FIRST = NULL
then LINK (NEW) NEW
FIRST  LAST  NEW
return(FIRST)
• else
LINK (NEW)  FIRST
LINK (LAST)  NEW
FIRST  NEW
return(FIRST)
NEW NODE *
1. [Check for availability stack underflow]
• If AVAIL = NULL then
Write “Availability stack underflow”
Return(First)
2. [Obtain address of next free node]
• NEWAVAIL
3. [Remove free node from availability stack]
• AVAILLINK (AVAIL)
These steps are used to create New Node in every
insertion algorithm of linked list.

C Code: struct Node* new_node = (struct Node*)


malloc (sizeof (struct Node));
Insert at Last Position
• FUNCTION: CIR_LINK_INSERT_END (X, FIRST,
LAST)
• A new element is X
• FIRST and LAST a pointer to the first and last
element of a linked linear list respectively whose
typical node contains INFO and LINK fields.
• AVAIL is a pointer to the top element of the
availability stack
• NEW is a temporary points variable.
• This function inserts X. It is required that X
precedes the node whose address is given by
FIRST.
Insert at Last Position
1. [Create New Empty Node]
• NEW NODE*
2. [Initialize fields of new node and its link to the list]
• If FIRST = NULL
then LINK (NEW) NEW
FIRST LAST  NEW
return(FIRST)
• else
LINK(NEW) FIRST
LINK(LAST)  NEW
LAST  NEW
return(FIRST)
Insert in Ordered Linked
List
• CIR_LINK_INSERT_ORDER (X, FIRST, LAST)
• A new element is X
• FIRST and LAST a pointer to the first and last
element of a linked linear list respectively whose
typical node contains INFO and LINK fields.
• AVAIL is a pointer to the top element of the
availability stack
• NEW and SAVE are temporary points variables.
• It is required that X be inserted so that it
preserves the ordering of the terms in increasing
order of their INFO field.
Insert in Ordered Linked
List
1. [Create New Empty Node]
• NEW  NODE*
2. [Copy information content into new node]
• INFO (NEW)  X
3. [Is Linked List is empty?]
• If FIRST = NULL
then LINK (NEW)  NEW
FIRST  LAST  NEW
return(FIRST)
4. [Does new node precedes all other nodes in List?]
• If INFO (NEW) <= INFO (FIRST)
then LINK (NEW)  FIRST
LINK (LAST)  NEW
FIRST  NEW
return(FIRST)
5. [Initialize Temporary Pointer]
• SAVE FIRST
6. [Search for Predecessor of new node]
• Repeat while SAVE ≠ LAST and INFO(NEW) <=
INFO(LINK(SAVE))
SAVE  LINK(SAVE)
7. [Set link field of NEW node and its Predecessor]
• LINK(NEW) LINK(SAVE)
• LINK(SAVE) NEW
• If SAVE = LAST
then LAST  NEW
8. [Return first node address]
• return(FIRST)
Delete Element
• CIR_LINK_DELETE (X, FIRST, LAST)
• An element which we want to delete is X
• FIRST and LAST a pointer to the first and last element
of a linked linear list respectively whose typical node
contains INFO and LINK fields.
• AVAIL is a pointer to the top element of the availability
stack
• This procedure deletes the node whose address is
given by X.
• TEMP is used to find the desired node, and PRED keeps
track of the predecessor of TEMP.
• Note that FIRST is changed only when X is the first
element of the list.
Delete Element
1. [Is Empty List?]
• If FIRST = NULL
then write (‘Linked List is Empty’)
return
2. [Initialize Search for X]
• TEMP  FIRST
3. [Find X]
• Repeat thru step 5 while SAVE ≠ X and SAVE ≠ LAST
4. [Update predecessor marker]
• PRED  SAVE
5. [Move to next node]
• SAVE  LINK (SAVE)
6. [End of Linked List]
• If SAVE ≠ X
then write(‘Node not found’)
return
7. [Delete X]
• If X = FIRST
then FIRST  LINK (FIRST)
LINK (LAST)  FIRST
• else
LINK (PRED)  LINK(X)
If X = LAST
then LAST  PRED
8. [Free Deleted Node]
• Free (X)
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.
DOUBLY LINKED
LIST
node
in doubly linked list
• DOUBLEINS (L, R, M, X)
• This function inserts an element into double
linked list.
• L is a pointer which contains address of left most
node in the list.
• R is a pointer which contains address of right
most node in the list.
• The insertion is to be performed to the left of
specified node with its address given by pointer
variable M.
• X contains the value to be inserted in to the list
node
in doubly linked list
• 1.[Create new node]
• If AVAIL = NULL then
Write “Availability stack underflow”
• Else
NEWAVAIL
AVAILLINK (AVAIL)
2. [Copy information field] Null X null
• INFO (NEW) X
3. [insertion into an empty list]
• If R = NULL then
LPTR (NEW) RPTR (NEW) NULL
LRNEW
Return
4. [left most insertion]
• If M = L then
LPTR (NEW) NULL
RPTR (NEW) M
LPTR (M) NEW
LNEW
Return
5. [insert in middle]
• LPTR (NEW) LPTR (M)
• RPTR (NEW) M
• LPTR (M) NEW
• RPTR (LPTR (NEW)) NEW
• Return
6. [Finished]
2000 30 3000

5000

20 5000
5000 40

2000
3000

LPTR (NEW) LPTR (M)


RPTR (NEW) M
LPTR (M) NEW
RPTR (LPTR (NEW)) NEW
Return
doubly
linked list
DOUBINS_ORD
(L, R, M, X)
• Given a doubly link list whose left most and right
most nodes addressed are given by the pointer
variables L and R respectively.
• It is required to insert a node whose address is
given by the pointer variable NEW.
• The left and right links of nodes are denoted by
LPTR and RPTR respectively.
• The information field of a node is denoted by
variable INFO.
• The name of an element of the list is NODE. The
insertion is to be performed in ascending order of
info part.
• The information to be entered in the node is
contained in X.
DOUBINS_ORD
(L, R, M, X)
1. [Create New Empty Node]
• NEW  NODE*
2. [ Copy information field]
• INFO (NEW)  X
3. [Insert into an empty list]
• If R = NULL
then LPTR (NEW)  RPTR (NULL)  NULL
L  R  NEW
return
4. [Does the new node precedes all other nodes in List? ]
• If INFO(NEW)  INFO(L)
then RPTR (NEW)  L
LPTR(NEW) NULL
LPTR (L)  NEW
L  NEW
return
5. [ Initialize top Pointer]
• SAVE  L
6. [Search for predecessor of New node]
• Repeat while RPTR(SAVE) ≠ NULL and INFO(NEW) 
INFO(RPTR(SAVE))
SAVE  RPTR (SAVE)
7. [Set link field of new node and its
predecessor]
• RPTR (NEW)  RPTR(SAVE)
• LPTR (RPTR(SAVE))  NEW
• RPTR (SAVE)  NEW
• LPTR (NEW)  SAVE
• If SAVE = R
then RPTR(SAVE)  NEW
from
doubly linked list
• DOUBLEDEL(L, R,OLD)
• This function delete an element into double linked list.
• L is a pointer which contains address of left most node
in the list.
• R is a pointer which contains address of right most
node in the list.
• OLD contains address of node which we want to
delete.
1. [Underflow]
• If R = NULL then
Write “Underflow”
Return
2. [Delete Node]
• If L = R then
LRNULL
• Else if OLD = L then
LRPTR (L)
LPTR (L) NULL
• Else if OLD = R then
RLPTR(R)
RPTR(R) NULL
• Else
RPTR (LPTR (OLD)) RPTR (OLD)
LPTR (RPTR (OLD)) LPTR (OLD)
3. [Return Delete node]
• Restore (OLD)
• Return
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.
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.
LINKED
LIST
Doubly Circular linked
list
• A doubly linked list in which left link of the
left most node points to the right most node
and right link of the right most node points
to the left most node is known as Doubly
circular linked list.
• Representation of doubly circular linked list
shown below:
Doubly Circular linked
list
• Advantage:
• Every node is accessible from given node.
• Disadvantage:
• Without some care in processing it is possible to
get into the infinite loop. So we must able to
detect end of the list.
• To detect the end of the list in doubly circular
linked list we use one special node called the
HEAD node.
list or
in an empty list
• Empty List (start = NULL): A node(Say N) is
inserted with data = 5, so previous pointer
of N points to N and next pointer of N also
points to N. But now start pointer points to
the first node the list.
list or
in an empty list
• List initially contain some nodes, start
points to first node of the List: A node(Say
M) is inserted with data = 7, so previous
pointer of M points to last node, next
pointer of M points to first node and last
node’s next pointer points to this M node
and first node’s previous pointer points to
this M node.
// Function to insert at the end
void insertEnd(struct Node** start, int value)
{
// If the list is empty, create a single node
// circular and doubly list
if (*start == NULL)
{
struct Node* new_node = new Node;
new_node->data = value;
new_node->next = new_node->prev = new_node;
*start = new_node;
return;
}
// If list is not empty
/* Find last node */
Node *last = (*start)->prev;
// Create Node dynamically
struct Node* new_node = new Node;
new_node->data = value;
// Start is going to be next of new_node
new_node->next = *start;
// Make new node previous of start
(*start)->prev = new_node;
// Make last preivous of new node
new_node->prev = last;
// Make new node next of old last
last->next = new_node; }
beginning of
the list:
• To insert a node at the beginning of the list, create a
node(Say T) with data = 5, T next pointer points to
first node of the list, T previous pointer points to last
node the list, last node’s next pointer points to this T
node, first node’s previous pointer also points this T
node and at last don’t forget to shift ‘Start’ pointer
to this T node.
// Function to insert Node at the beginning of the List,
void insertBegin(struct Node** start, int value)
{ // Pointer points to last Node
struct Node *last = (*start)->prev;
struct Node* new_node = new Node;
new_node->data = value; // Inserting the data
// setting up previous and next of new node
new_node->next = *start;
new_node->prev = last;
// Update next and previous pointers of start and last.
last->next = (*start)->prev = new_node;
// Update start pointer
*start = new_node;
• }
Insertion in between the
nodes of the list:
• To insert a node in between the list, two
data values are required one after which
new node will be inserted and another is
the data of the new node.
// Function to insert node with value as value1. The new node is
inserted after the node with with value2
void insertAfter(struct Node** start, int value1, int value2)
{ struct Node* new_node = new Node;
new_node->data = value1; // Inserting the data
// Find node having value2 and next node of it
struct Node *temp = *start;
while (temp->data != value2)
temp = temp->next;
struct Node *next = temp->next;
// insert new_node between temp and next.
temp->next = new_node;
new_node->prev = temp;
new_node->next = next;
next->prev = new_node; }
Doubly Circular linked
list
Advantages:
• List can be traversed from both the directions i.e.
from head to tail or from tail to head.
• Jumping from head to tail or from tail to head is done
in constant time O(1).
• Circular Doubly Linked Lists are used for
implementation
• of advanced data structures like Fibonacci Heap.
Disadvantages:
• It takes slightly extra memory in each node to
accommodate previous pointer.
• Lots of pointers involved while implementing or doing
operations on a list. So, pointers should be handled
carefully otherwise data of the list may get lost.
doubly
linked list
• Managing songs playlist in media player
applications.
• Managing shopping cart in online shopping
Application of Linked
list
• Linked lists are used as a building block for many other data
structures, such as stacks, queues and their variations.
• The "data" field of a node can be another linked list. By this
device, one can construct many linked data structures with lists;
this practice originated in the Lisp programming language, where
linked lists are a primary (though by no means the only) data
structure, and is now a common feature of the functional
programming style.
• Sometimes, linked lists are used to implement associative arrays,
and are in this context called association lists. There is very little
good to be said about this use of linked lists; they are easily
outperformed by other data structures such as self-balancing
binary search trees even on small data sets. However, sometimes
a linked list is dynamically created out of a subset of nodes in such
a tree, and used to more efficiently traverse that set.
• Polynomial manipulation
• Linked dictionary
• Multiple precision arithmetic

You might also like