PDS Tutorial LinkedLists
PDS Tutorial LinkedLists
Q2a. Write a C program to delete an existing node after k nodes of a single linked list.
int main()
{
struct Node* head = NULL;
return 0;
}
https://www.geeksforgeeks.org/insertion-in-linked-list/
Addition to the front of a Single Linked List
void push(struct Node** head_ref, int new_data)
{
/* 1. allocate node */
struct Node* new_node = (struct Node*) malloc(sizeof(struct Node));
/* 2. put in the data */
new_node->data = new_data;
/* 3. Make next of new node as head */
new_node->next = (*head_ref);
/* 4. move the head to point to the new node */
(*head_ref) = new_node;
}
Addition to the end of a Single Linked List
void push(struct Node** head_ref, int new_data)
{
/* 1. allocate node */
struct Node* new_node = (struct Node*) malloc(sizeof(struct Node));
struct Node *last = *head_ref; /* used in step 5*/
/* 2. put in the data */
new_node->data = new_data;
/* 3. This new node is going to be the last node, so make next of it as NULL*/
new_node->next = NULL;
...
Addition to the end of a Single Linked List
...
/* 4. If the Linked List is empty, then make the new node as head */
if (*head_ref == NULL)
{
*head_ref = new_node;
return;
}
/* 5. Else traverse till the last node */
while (last->next != NULL)
last = last->next;
/* 6. Change the next of last node */
last->next = new_node;
return;
}
Deletion from k-th position of a Single Linked List
void deleteN(Node** head, int position)
{
Node* temp;
Node* prev;
temp = *head;
prev = *head;
for (int i = 0; i < position; i++) {
if (i == 0 && position == 1) {
*head = (*head)->next;
free(temp);
}
https://www.geeksforgeeks.org/deletion-in-linked-list/
Deletion from k-th position of a Single Linked List
else {
if (i == position - 1 && temp) {
prev->next = temp->next;
free(temp);
}
else {
prev = temp;
// Position was greater than number of nodes in the list
if (prev == NULL)
break;
temp = temp->next;
}}}}
Reversing a Single Linked List
void reverse(struct Node** head_ref)
{
struct Node* prev = NULL;
struct Node* current = *head_ref;
struct Node* next = NULL;
while (current != NULL) {
// Store next
next = current->next;
// Reverse current node's pointer
current->next = prev;
// Move pointers one position ahead.
prev = current;
current = next;
}
*head_ref = prev;
} https://www.geeksforgeeks.org/reverse-a-linked-list/
Identifying a loop in a Single Linked List
int detectLoop(struct Node* list)
{
struct Node *slow_p = list, *fast_p = list;
while (slow_p && fast_p && fast_p->next) {
slow_p = slow_p->next;
fast_p = fast_p->next->next;
if (slow_p == fast_p) {
return 1;
}
}
return 0;
}
Removing the loop in a Single Linked List
/* If loop exists */
if (slow == fast) {
slow = head;
if (slow == fast)
while (fast->next != slow)
fast = fast->next;
else {
while (slow->next != fast->next) {
slow = slow->next;
fast = fast->next;
}}
fast->next = NULL;
}
Some more questions
Q. Print the k-th node from the end of a singly linked list