0% found this document useful (0 votes)
22 views17 pages

Chapter 2 Ds Notes

The document provides a comprehensive overview of linked lists, detailing insertion and deletion operations for singly linked lists, circular singly linked lists, and doubly linked lists. It outlines algorithms for inserting and deleting nodes at the beginning, end, and specified positions within these lists. Additionally, it touches on header linked lists and the concept of splitting a linked list into two separate lists.

Uploaded by

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

Chapter 2 Ds Notes

The document provides a comprehensive overview of linked lists, detailing insertion and deletion operations for singly linked lists, circular singly linked lists, and doubly linked lists. It outlines algorithms for inserting and deleting nodes at the beginning, end, and specified positions within these lists. Additionally, it touches on header linked lists and the concept of splitting a linked list into two separate lists.

Uploaded by

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

UNIT-2

Linked List
 Inserting an element into the linked list
There are 3 ways to insert an element into the linked list.
In linked list the first node is called as head node.
1)Insertion at the beginning
2)Insertion at the end
3)Insertion at the middle

1)Insertion at the beginning:

Inserting a new element into a singly linked list at beginning is quite


simple. We just need to make a few adjustments in the node links. There
are the following steps which need to be followed in order to insert a
new node in the list at beginning.

o Allocate the space for the new node and store data into the data
part of the node. This will be done by the following statements.

1. ptr = (struct node *) malloc(sizeof(struct node *));


2. ptr → data = item

o Make the link part of the new node pointing to the existing first
node of the list. This will be done by using the following
statement.

1. ptr->next = head;
o At the last, we need to make the new node as the first node of the
list this will be done by using the following statement.

head = ptr;

Algorithm:

o Step 1: IF PTR = NULL

Write OVERFLOW
Go to Step 7
[END OF IF]

o Step 2: SET NEW_NODE = PTR


o Step 3: SET PTR = PTR → NEXT
o Step 4: SET NEW_NODE → DATA = VAL
o Step 5: SET NEW_NODE → NEXT = HEAD
o Step 6: SET HEAD = NEW_NODE
o Step 7: EXIT

2)Insertion in singly linked list at the end

In order to insert a node at the last, there are two following


scenarios which need to be mentioned.

1. The node is being added to an empty list


2. The node is being added to the end of the linked list

in the first case,

o The condition (head == NULL) gets satisfied. Hence, we


just need to allocate the space for the node by using
malloc statement in C. Data and the link part of the node
are set up by using the following statements.
o ptr->data = item;
ptr -> next = NULL;
o Since, ptr is the only node that will be inserted in the list
hence, we need to make this node pointed by the head
pointer of the list. This will be done by using the following
Statements.

Head = ptr
In the second case,

o The condition Head = NULL would fail, since Head is not


null. Now, 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.
o Temp = head
o Then, traverse through the entire linked list using the
statements

1. while (temp→ next != NULL)


2. temp = temp → next;

o 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) .

1. temp = head;
2. while (temp -> next != NULL)
3. {
4. temp = temp -> next;
5. }
6. temp->next = ptr;
7. ptr->next = NULL;

Algorithm

o Step 1: IF PTR = NULL Write OVERFLOW


Go to Step 1
[END OF IF]
o Step 2: SET NEW_NODE = PTR
o Step 3: SET PTR = PTR - > NEXT
o Step 4: SET NEW_NODE - > DATA = VAL
o Step 5: SET NEW_NODE - > NEXT = NULL
o Step 6: SET PTR = HEAD
o Step 7: Repeat Step 8 while PTR - > NEXT != NULL
o Step 8: SET PTR = PTR - > NEXT
[END OF LOOP]
o Step 9: SET PTR - > NEXT = NEW_NODE
o Step 10: EXIT

3.Insertion in singly linked list after specified Node

o In order to insert an element after the specified number of


nodes into the linked list, we need to skip the desired
number of elements in the list to move the pointer at the
position after which the node will be inserted. This will be
done by using the following statements.

1. temp=head;
2. for(i=0;i<loc;i++)
3. {
4. temp = temp->next;
5. if(temp == NULL)
6. {
7. return;
8. }
9.
10. }

o
o
o
o Allocate the space for the new node and add the item to
the data part of it. This will be done by using the following
statements.

1. ptr = (struct node *) malloc (sizeof(struct node));


2. ptr->data = item;

o 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). This
will be done by using the following statements.
ptr→ next = temp → next

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

o STEP 1: IF PTR = NULL

WRITE OVERFLOW
GOTO STEP 12
END OF IF

o STEP 2: SET NEW_NODE = PTR


o STEP 3: NEW_NODE → DATA = VAL
o STEP 4: SET TEMP = HEAD
o STEP 5: SET I = 0
o STEP 6: REPEAT STEP 5 AND 6 UNTIL I<loc< li=""></loc<>
o STEP 7: TEMP = TEMP → NEXT
o STEP 8: IF TEMP = NULL

WRITE "DESIRED NODE NOT PRESENT"


GOTO STEP 12
END OF IF
END OF LOOP

o STEP 9: PTR → NEXT = TEMP → NEXT


o STEP 10: TEMP → NEXT = PTR
o STEP 11: SET PTR = NEW_NODE
o STEP 12: EXIT

Delete a node from linked list:


In deletion also we can delete a node using 3 ways.
1)Delete a node at the beginning
2)Delete a node at the end
3)Delete a node a specified position
1) Delete a node at the 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. This will be done by using the
following statements.

1. ptr = head;
2. head = ptr->next;
Now, free the pointer ptr which was pointing to the head node of the
list. This will be done by using the following statement.
free(ptr)

Algorithm

o Step 1: IF HEAD = NULL

Write UNDERFLOW
Go to Step 5
[END OF IF]

o Step 2: SET PTR = HEAD


o Step 3: SET HEAD = HEAD -> NEXT
o Step 4: FREE PTR
o Step 5: EXIT

2) Delete a node at the end

Algorithm

o Step 1: IF HEAD = NULL

Write UNDERFLOW
Go to Step 8
[END OF IF]

o Step 2: SET PTR = HEAD


o Step 3: Repeat Steps 4 and 5 while PTR -> NEXT!= NULL
o Step 4: SET PREPTR = PTR
o Step 5: SET PTR = PTR -> NEXT
[END OF LOOP]

o Step 6: SET PREPTR -> NEXT = NULL


o Step 7: FREE PTR
o Step 8: EXIT

3) Deletion in singly linked list 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

 Circular Singly linked list


1.Insertion at the Beginning

Algorithm
o Step 1: IF PTR = NULL

Write OVERFLOW
Go to Step 11
[END OF IF]

o Step 2: SET NEW_NODE = PTR


o Step 3: SET PTR = PTR -> NEXT
o Step 4: SET NEW_NODE -> DATA = VAL
o Step 5: SET TEMP = HEAD
o Step 6: Repeat Step 8 while TEMP -> NEXT != HEAD
o Step 7: SET TEMP = TEMP -> NEXT

[END OF LOOP]

o Step 8: SET NEW_NODE -> NEXT = HEAD


o Step 9: SET TEMP → NEXT = NEW_NODE
o Step 10: SET HEAD = NEW_NODE
o Step 11: EXIT

2.Insertion at the End

Algorithm
o Step 1: IF PTR = NULL

Write OVERFLOW
Go to Step 1
[END OF IF]

o Step 2: SET NEW_NODE = PTR


o Step 3: SET PTR = PTR -> NEXT
o Step 4: SET NEW_NODE -> DATA = VAL
o Step 5: SET NEW_NODE -> NEXT = HEAD
o Step 6: SET TEMP = HEAD
o Step 7: Repeat Step 8 while TEMP -> NEXT != HEAD
o Step 8: SET TEMP = TEMP -> NEXT

[END OF LOOP]

o Step 9: SET TEMP -> NEXT = NEW_NODE


o Step 10: EXIT
Deleting a Node from the List
1. Deletion in circular singly linked list at beginning

Algorithm
o Step 1: IF HEAD = NULL

Write UNDERFLOW
Go to Step 8
[END OF IF]

o Step 2: SET PTR = HEAD


o Step 3: Repeat Step 4 while PTR → NEXT != HEAD
o Step 4: SET PTR = PTR → next

[END OF LOOP]

o Step 5: SET PTR → NEXT = HEAD → NEXT


o Step 6: FREE HEAD
o Step 7: SET HEAD = PTR → NEXT
o Step 8: EXIT

2. Deletion in Circular singly linked list at the end

Algorithm
o Step 1: IF HEAD = NULL

Write UNDERFLOW
Go to Step 8
[END OF IF]

o Step 2: SET PTR = HEAD


o Step 3: Repeat Steps 4 and 5 while PTR -> NEXT != HEAD
o Step 4: SET PREPTR = PTR
o Step 5: SET PTR = PTR -> NEXT

[END OF LOOP]

o Step 6: SET PREPTR -> NEXT = HEAD


o Step 7: FREE PTR
o Step 8: EXIT
Q: What is circular linked list? How to traverse a circular linked list?
Traversing in circular singly linked list can be done through a loop. Initialize the
temporary pointer variable temp to head pointer and run the while loop until the next
pointer of temp becomes head. The algorithm and the c function implementing the
algorithm is described as follows.

Algorithm
o STEP 1: SET PTR = HEAD
o STEP 2: IF PTR = NULL

WRITE "EMPTY LIST"


GOTO STEP 8
END OF IF

o STEP 4: REPEAT STEP 5 AND 6 UNTIL PTR → NEXT != HEAD


o STEP 5: PRINT PTR → DATA
o STEP 6: PTR = PTR → NEXT

[END OF LOOP]

o STEP 7: PRINT PTR→ DATA


o STEP 8: EXIT
 Two way linked list
Insertion Operation
1. Insertion in doubly linked list at beginning

Algorithm :
o Step 1: IF ptr = NULL

Write OVERFLOW
Go to Step 9
[END OF IF]

o Step 2: SET NEW_NODE = ptr


o Step 3: SET ptr = ptr -> NEXT
o Step 4: SET NEW_NODE -> DATA = VAL
o Step 5: SET NEW_NODE -> PREV = NULL
o Step 6: SET NEW_NODE -> NEXT = Head
o Step 7: SET Head -> PREV = NEW_NODE
o Step 8: SET Head = NEW_NODE
o Step 9: EXIT

2. Insertion in doubly linked list at end

Algorithm
o Step 1: IF PTR = NULL

Write OVERFLOW
Go to Step 11
[END OF IF]

o Step 2: SET NEW_NODE = PTR


o Step 3: SET PTR = PTR -> NEXT
o Step 4: SET NEW_NODE -> DATA = VAL
o Step 5: SET NEW_NODE -> NEXT = NULL
o Step 6: SET TEMP = START
o Step 7: Repeat Step 8 while TEMP -> NEXT != NULL
o Step 8: SET TEMP = TEMP -> NEXT

[END OF LOOP]

o Step 9: SET TEMP -> NEXT = NEW_NODE


o Step 10C: SET NEW_NODE -> PREV = TEMP
o Step 11: EXIT
 Header linked list:
Write a short note on header linked list.
 Split a linked list
Q Write and explain an algorithm to split a link list into two linked lists. APR-18

You might also like