Professional Documents
Culture Documents
DSA Linked List
DSA Linked List
a 0 1 2 3 4 5 6 index
4000 4004 4008 4012 4016 4020 4024 memory address
11 12 13 14 15 elements
Base Address b 0 1 2 3 4 index
5000 5004 5008 5012 5016 memory address
a 4000 4004 4008 4012 4016 4020 4024 4027 memory address
5 6 13 10 12 Data/value/element
Not possible
11 12 13
0 1 2 3 4
5000 5004 5008 5012 5016
Dr. Pavithra L K, VIT, Chennai Campus
List
• Linear Data Structure
• Size of the linked list data structure is modifiable
during the run time
• Sequential access
• Use memory space effectively (doesnot waste
memory space)
• It contains value/data/element and address to next
element
Node
Element/ Data/ Address to next
Value element
Dr. Pavithra L K, VIT, Chennai Campus
Linked List
• Elements are not stored at contiguous memory locations
• Elements in a linked list are linked using pointers as
shown in the below image
• Last element points to NULL
4000 4012 4024 memory address
4000 5 4012 6 4024 10 Null
4000 4004 4008 4012 4016 4020 4024 4028 memory address
5 6 10 Data/value/element
Dr. Pavithra L K, VIT, Chennai Campus
Array Vs Linked List
• Arrays
• Inserting/deleting an element in ordered manner
• Supports random access
• Number of elements in the array are not
predefined
• Linked lists
• Inserting/deleting an element in Non-ordered
manner
• Sequential access
• Number of elements in the list are not predefined
Head Head
pointer Node
Head Head
pointer Node
node
Previous Data Next_node_addr
_node_address ess
4000 4018 4030
4000 null 12 4018 4000 67 4030 4018 67 null
Head Head
Dr. Pavithra L K, VIT, Chennai Campus
pointer node
Circular Doubly Linked List (C-DLL)
4000 4018 4030
4000 4030 12 4018 4000 67 4030 4018 67 4000
Head
pointer
4032
Update the address field of the newNode
32 4000
1
4032
Update the head pointer
32 4000
2 1
4. if not move temp to next node and check second field is empty or not. (second field
is empty) temp
temp 4032
temp
4000 5 4012 6 4024 10 Null
4. if it is not matched, move the temp to next location and check whether the position of
the temp pointing node is equal to the position detail given by the user.
temp
4000 5 4012 6 4024 10 Null
4032 Update newNode node pointer value to
32 4012 temp pointer pointing node address
temp
4000 5 4012 6 4024
Dr. Pavithra L K, VIT, Chennai Campus
10 Null
Insertion at particular location SLL
Update newNode node address to the second field of the node which is present
before the position given by the user
4032
32 4012
temp
4000 5 4032 6 4024 10 Null
Update the
head pointer free(temp)
4032 5 4032 32 4012 6 4024 10 Null
Check whether it reached pos-1; if reached pos-1 assign the second field of pos-1 node
to nextnode.(i.e: nextnode= temp->next_adr ).
temp nextnode
4000 5 4032 32 4012 6 4024 10 Null
update temp pointing node’s second field with nextnode’s second field and free the
nextnode. temp free(nextnode)
4000 5 4032 32 4024 6 4024 10 Null
4044
Head Update temp node’s prev pointer 4000 48
pointer
Head
pointer
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.
Insertion
• At beginning
• At end
• At particular location
Deletion
Display
6 4 40 5 3 40 2 2 40 7 0 Null
45 65 32
4000 4045 4065 4032
head2 temp2
5 5 50 12 3 30 3 1 50 1 0 Null
45 05 30
5010 3005Campus
5045Dr. Pavithra L K, VIT, Chennai 5030
Polynomial Addition
• 6𝑋 4 + 5𝑋 3 + 2𝑋 2 +7
• 5𝑋 5 + 12𝑋 3 + 3𝑋 + 1
head1 temp1
6 4 40 5 3 40 2 2 40 7 0 Null
45 65 32
4000 4045 4065 4032
5𝑋 5
head2 temp2
5 5 50 12 3 30 3 1 50 1 0 Null
45 05 30
5010 3005Campus
5045Dr. Pavithra L K, VIT, Chennai 5030
Polynomial Addition
• 6𝑋 4 + 5𝑋 3 + 2𝑋 2 +7
• 5𝑋 5 + 12𝑋 3 + 3𝑋 + 1
head1 temp1
6 4 40 5 3 40 2 2 40 7 0 Null
45 65 32
4000 4045 4065 4032
5𝑋 5 + 6𝑋 4 + 15𝑋 3
head2 temp2
5 5 50 12 3 30 3 1 50 1 0 Null
45 05 30
5010 3005Campus
5045Dr. Pavithra L K, VIT, Chennai 5030
Polynomial Addition
• 6𝑋 4 + 5𝑋 3 + 2𝑋 2 +7
• 5𝑋 5 + 12𝑋 3 + 3𝑋 + 1
head1 temp1
6 4 40 5 3 40 2 2 40 7 0 Null
45 65 32
4000 4045 4065 4032
5𝑋 5 + 6𝑋 4
head2 temp2
5 5 50 12 3 30 3 1 50 1 0 Null
45 05 30
5010 3005Campus
5045Dr. Pavithra L K, VIT, Chennai 5030
Polynomial Addition
• 6𝑋 4 + 5𝑋 3 + 2𝑋 2 +7
• 5𝑋 5 + 12𝑋 3 + 3𝑋 + 1
head1 temp1
6 4 40 5 3 40 2 2 40 7 0 Null
45 65 32
4000 4045 4065 4032
5𝑋 5 + 6𝑋 4 + 15𝑋 3 + 2𝑋 2+3𝑋 1 + 8
head2 temp2
5 5 50 12 3 30 3 1 50 1 0 Null
45 05 30
5010 3005Campus
5045Dr. Pavithra L K, VIT, Chennai 5030
Linked list intersection
• Consider two linked list L1 and L2
• Initialize result list as NULL. Traverse list1 (L1) and look for its each element in
list2 (L2), if the element is present in list2, then add the element to result.
• L1
4000 temp1 compare the temp1 data and temp2 data. If not
increment temp2 new location
6 4045 7 4027 3 4032 5 null
4000 4045 4027 4032
• L2
5000 temp2
• L1
4000 temp1
• L1
4000 temp1
• L1
4000 temp1
• L1
4000 temp1
• L1
4000 temp1
• L1
4000 temp1
• L1
4000 temp1
• L1
4000 temp1
• L1
4000 temp1
• L1
4000 temp1
• L3 5021 3 null
Dr. Pavithra L K, VIT, Chennai Campus
Linked list intersection
• Compare the temp1 data and temp2 data. If not matched then move temp2 to next
node location. If it is matched then put that node to new list.
• Compare until it reach the end of the list.
• L1
4000 temp1
• L3 5021 3 null
Dr. Pavithra L K, VIT, Chennai Campus
Linked list intersection
• Compare the temp1 data and temp2 data. If not matched then move temp2 to next
node location. If it is matched then put that node to new list.
• Compare until it reach the end of the list.
• L1
4000 temp1
• L3 5021 3 null
Dr. Pavithra L K, VIT, Chennai Campus
Linked list intersection
• Compare the temp1 data and temp2 data. If not matched then move temp2 to next
node location. If it is matched then put that node to new list.
• Compare until it reach the end of the list.
• L1
4000 temp1
• L3 5021 3 null
Dr. Pavithra L K, VIT, Chennai Campus
Linked list intersection
• Compare the temp1 data and temp2 data. If not matched then move temp2 to next
node location. If it is matched then put that node to new list.
• Compare until it reach the end of the list.
• L1
4000 temp1
• L3 5021 3 null
Dr. Pavithra L K, VIT, Chennai Campus
Linked list intersection
• Compare the temp1 data and temp2 data. If not matched then move temp2 to next
node location. If it is matched then put that node to new list.
• Compare until it reach the end of the list.
• L1
4000 temp1
• L3 5021 3 null
Dr. Pavithra L K, VIT, Chennai Campus
Linked list intersection
Linked list intersection output
5021 3 null