Chapter 2 Ds Notes
Chapter 2 Ds Notes
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
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.
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:
Write OVERFLOW
Go to Step 7
[END OF IF]
Head = ptr
In the second case,
1. temp = head;
2. while (temp -> next != NULL)
3. {
4. temp = temp -> next;
5. }
6. temp->next = ptr;
7. ptr->next = NULL;
Algorithm
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.
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
WRITE OVERFLOW
GOTO STEP 12
END OF IF
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
Write UNDERFLOW
Go to Step 5
[END OF IF]
Algorithm
Write UNDERFLOW
Go to Step 8
[END OF IF]
Algorithm
WRITE UNDERFLOW
GOTO STEP 10
END OF IF
STEP 3: SET I = 0
STEP 8: I = I+1
END OF LOOP
Algorithm
o Step 1: IF PTR = NULL
Write OVERFLOW
Go to Step 11
[END OF IF]
[END OF LOOP]
Algorithm
o Step 1: IF PTR = NULL
Write OVERFLOW
Go to Step 1
[END OF IF]
[END OF LOOP]
Algorithm
o Step 1: IF HEAD = NULL
Write UNDERFLOW
Go to Step 8
[END OF IF]
[END OF LOOP]
Algorithm
o Step 1: IF HEAD = NULL
Write UNDERFLOW
Go to Step 8
[END OF IF]
[END OF LOOP]
Algorithm
o STEP 1: SET PTR = HEAD
o STEP 2: IF PTR = NULL
[END OF LOOP]
Algorithm :
o Step 1: IF ptr = NULL
Write OVERFLOW
Go to Step 9
[END OF IF]
Algorithm
o Step 1: IF PTR = NULL
Write OVERFLOW
Go to Step 11
[END OF IF]
[END OF LOOP]