0% found this document useful (0 votes)
31 views36 pages

Linked List

A linked list is a dynamic data structure consisting of nodes that contain data and pointers to the next node, allowing for efficient memory usage and flexible size. It overcomes the limitations of arrays, such as fixed size and contiguous memory allocation, but has drawbacks like lack of random access and additional memory for pointers. Basic operations include insertion, deletion, and searching, with variations for singly and doubly linked lists, each having its own advantages and disadvantages.

Uploaded by

tachbirdewan.bd
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)
31 views36 pages

Linked List

A linked list is a dynamic data structure consisting of nodes that contain data and pointers to the next node, allowing for efficient memory usage and flexible size. It overcomes the limitations of arrays, such as fixed size and contiguous memory allocation, but has drawbacks like lack of random access and additional memory for pointers. Basic operations include insertion, deletion, and searching, with variations for singly and doubly linked lists, each having its own advantages and disadvantages.

Uploaded by

tachbirdewan.bd
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/ 36

LINKED LIST

A linked list is a linear collection of data elements (called


nodes), where the linear order is given by means of
pointers.

Each node is divided into 2 parts:


1st part contains the information of the element.
2nd part is called the link field or next pointer field which
contains the address of the next node in the list.
USES OF LINKED LIST
 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.


 Empty node can not be present in the linked

list.
 We can store values of primitive types or

objects in the singly linked list.


WHY USE LINKED LIST OVER ARRAY?

Array contains following limitations:


 The size of array must be known in advance

before using it in the program.


 Increasing size of the array is a time taking

process. It is almost impossible to expand the


size of the array at run time.
 All the elements in the array need to be

contiguously stored in the memory. Inserting


any element in the array needs shifting of all
its predecessors.
USEFUL
Linked list is the data structure which can
overcome all the limitations of an array.
Using linked list is useful because,
 It allocates the memory dynamically. All the

nodes of linked list are non-contiguously


stored in the memory and linked together
with the help of pointers.
 Sizing is no longer a problem since we do not

need to define its size at the time of


declaration. List grows as per the program's
demand and limited to the available memory
space.
ADVANTAGES OVER ARRAYS
 1) Dynamic size
2) Ease of insertion/deletion
DRAWBACKS:
1) 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 efficiently with
its default implementation.

2) Extra memory space for a pointer is required with


each element of the list.

3) Not cache friendly. Since array elements are


contiguous locations, there is locality of reference which
is not there in case of linked lists.
BASIC OPERATIONS
 Insert: Add a new node in the first, last or
interior of the list.
 Delete: Delete a node from the first, last or

interior of the list.


 Search: Search a node containing particular

value in the linked list.


 A node can be added in three ways
1) At the front of the linked list
2) After a given node.
3) At the end of the linked list.
INSERT FIRST
 To add a new node to the head of the linear
linked list, we need to construct a new node
that is pointed by pointer newitem.
 Assume there is a global variable head

which points to the first node in the list.


 The new node points to the first node in the

list. The head is then set to point to the new


node.
INSERT FIRST
 To add a new node to the head of the linear linked list, we need to
construct a new node that is pointed by pointer newitem.
 Assume there is a global variable head which points to the first
node in the list.
 The new node points to the first node in the list. The head is then
set to point to the new node.
Step 1. Allocate the space for the new node and store
data into the data part of the node.
Step2. Make the link part of the new node pointing to
the existing first node of the list.
Sep3. At the last, we need to make the new node as
the first node of the list.
Algorithm
Step 1: IF PTR = NULL
Write OVERFLOW
Go to Step 7
[END OF IF]
Step 2: SET NEW_NODE = PTR
Step 3: SET PTR = PTR → NEXT
Step 4: SET NEW_NODE → DATA = VAL
Step 5: SET NEW_NODE → NEXT = HEAD
Step 6: SET HEAD = NEW_NODE
Step 7: EXIT
Algorithm
Step 1: IF PTR = NULL
Write OVERFLOW
Go to Step 7
[END OF IF]
Step 2: SET NEW_NODE = PTR
Step 3: SET PTR = PTR → NEXT
Step 4: SET NEW_NODE → DATA = VAL
Step 5: SET NEW_NODE → NEXT = HEAD
Step 6: SET HEAD = NEW_NODE
Step 7: EXIT
INSERTION IN SINGLY LINKED LIST AT THE END
 we need to declare a temporary pointer temp in
order to traverse through the list. temp is made
to point the first node of the list.
 Then, traverse through the entire linked list using

the statements:
 At the end of the loop, the temp will be pointing

to the last node of the list. Now, allocate the


space for the new node, and assign the item to
its data part. Since, the new node is going to be
the last node of the list hence, the next part of
this node needs to be pointing to the null. We
need to make the next part of the temp node
(which is currently the last node of the list) point
to the new node (ptr) .
Algorithm
Step 1: IF PTR = NULL Write OVERFLOW
Go to Step 1
[END OF IF]
Step 2: SET NEW_NODE = PTR
Step 3: SET PTR = PTR - > NEXT
Step 4: SET NEW_NODE - > DATA = VAL
Step 5: SET NEW_NODE - > NEXT = NULL
Step 6: SET PTR = HEAD
Step 7: Repeat Step 8 while PTR - > NEXT != NULL
Step 8: SET PTR = PTR - > NEXT
[END OF LOOP]
Step 9: SET PTR - > NEXT = NEW_NODE
Step 10: EXIT
INSERTION AFTER SPECIFIED NODE
INSERTION AFTER SPECIFIED NODE

 Allocate the space for the new node and add the
item to the data part of it.
 Now, we just need to make a few more link

adjustments and our node at will be inserted at


the specified position. Since, at the end of the
loop, the loop pointer temp would be pointing to
the node after which the new node will be
inserted. Therefore, the next part of the new node
ptr must contain the address of the next part of
the temp (since, ptr will be in between temp and
the next of the temp).
 now, we just need to make the next part of the

temp, point to the new node ptr. This will insert


the new node ptr, at the specified position.
Algorithm
STEP 1: IF PTR = NULL
WRITE OVERFLOW
GOTO STEP 12
END OF IF
STEP 2: SET NEW_NODE = PTR
STEP 3: NEW_NODE → DATA = VAL
STEP 4: SET TEMP = HEAD
STEP 5: SET I = 0
STEP 6: REPEAT STEP 5 AND 6 UNTIL I<loc< li=""></loc<>
STEP 7: TEMP = TEMP → NEXT
STEP 8: IF TEMP = NULL
WRITE "DESIRED NODE NOT PRESENT"
GOTO STEP 12
END OF IF
END OF LOOP
STEP 9: PTR → NEXT = TEMP → NEXT
STEP 10: TEMP → NEXT = PTR
STEP 11: SET PTR = NEW_NODE
STEP 12: EXIT
DELETION AT BEGINNING
 Deleting a node from the beginning of the list
is the simplest operation of all. It just need a
few adjustments in the node pointers. Since
the first node of the list is to be deleted,
therefore, we just need to make the head,
point to the next of the head.
Algorithm
Step 1: IF HEAD = NULL
Write UNDERFLOW
Go to Step 5
[END OF IF]
Step 2: SET PTR = HEAD
Step 3: SET HEAD = HEAD -> NEXT
Step 4: FREE PTR
Step 5: EXIT
DELETION IN SINGLY LINKED LIST AT THE END
Algorithm
Step 1: IF HEAD = NULL
Write UNDERFLOW
Go to Step 8
[END OF IF]
Step 2: SET PTR = HEAD
Step 3: Repeat Steps 4 and 5 while PTR -> NEXT!=
NULL
Step 4: SET PREPTR = PTR
Step 5: SET PTR = PTR -> NEXT
[END OF LOOP]
Step 6: SET PREPTR -> NEXT = NULL
Step 7: FREE PTR
Step 8: EXIT
DELETION AFTER THE SPECIFIED NODE :
Algorithm
STEP 1: IF HEAD = NULL
WRITE UNDERFLOW
GOTO STEP 10
END OF IF
STEP 2: SET TEMP = HEAD
STEP 3: SET I = 0
STEP 4: REPEAT STEP 5 TO 8 UNTIL I<loc< li=""></loc<>
STEP 5: TEMP1 = TEMP
STEP 6: TEMP = TEMP → NEXT
STEP 7: IF TEMP = NULL
WRITE "DESIRED NODE NOT PRESENT"
GOTO STEP 12
END OF IF
STEP 8: I = I+1
END OF LOOP
STEP 9: TEMP1 → NEXT = TEMP → NEXT
STEP 10: FREE TEMP
STEP 11: EXIT
SEARCHING IN SINGLY LINKED LIST

Searching is performed in order to find the


location of a particular element in the list.
Searching any element in the list needs
traversing through the list and make the
comparison of every element of the list with
the specified element. If the element is
matched with any of the list element then
the location of the element is returned from
the function.
 Algorithm
 Step 1: SET PTR = HEAD
 Step 2: Set I = 0
 STEP 3: IF PTR = NULL
 WRITE "EMPTY LIST"
GOTO STEP 8
END OF IF
 STEP 4: REPEAT STEP 5 TO 7 UNTIL PTR != NULL
 STEP 5: if ptr → data = item
 write i+1
End of IF
 STEP 6: I = I + 1
 STEP 7: PTR = PTR → NEXT
 [END OF LOOP]
 STEP 8: EXIT
DOUBLY LINKED LIST

Doubly linked list is a complex type of linked


list in which a node contains a pointer to the
previous as well as the next node in the
sequence. Therefore, in a doubly linked list, a
node consists of three parts: node data,
pointer to the next node in sequence (next
pointer) , pointer to the previous node
(previous pointer). A sample node in a doubly
linked list is shown in the figure.
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
(See this and this).
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.

You might also like