Page 531 - Computer science 868 Class 12
P. 531
5. Write an algorithm/Java code to delete a node from the beginning of an existing linked list using method:
void deleteBeg(Node start) where start refers to the start pointer.
header 1001 1156
29 1098 NULL 18 1156 35 NULL
1001 1098 1156
Ans. Algorithm
Step 1: Start
Step 2: Create a new Node ptr and assign it to start
Step 3: Set start = ptr.next
Step 4: Assign ptr.next=null
Java Code
void deletebeg(Node start)
{
Node ptr=start;
start=ptr.next; // change the start pointer to the next node
ptr.next=null; // break the link of the current node
}
6. Write an algorithm/Java code to delete a node from the middle of an existing linked list using method:
void deleteMid(Node start, int pos) where start refers to the start pointer and pos the position of deletion.
header 1001
29 1098 1156 18 1156 NULL 35 NULL
1001 1098 1156
Ans. Algorithm
Step 1: Start.
Step 2: Create new Nodes ptr and temp and initialise them to start
Step 3: Initialise c to 0
Step 4: Repeat Step 5 to Step 7 while c<pos-1
Step 5: Assign temp = ptr
Step 6: Increase c by 1
Step 7: Assign ptr = ptr.next
Step 8: Set temp.next= ptr.next
Step 9: Set ptr.next=null
Step 10: Stop.
529
Data Structures 529

