0% found this document useful (0 votes)
53 views

PDS Tutorial LinkedLists

The document discusses various operations that can be performed on single linked lists including adding and deleting nodes. It provides code snippets in C to add a node after k nodes, delete a node after k nodes, add to the front and end of a linked list, delete from the k-th position, reverse a linked list, detect and remove loops, and print the k-th node from the end. It also lists some additional linked list problems.

Uploaded by

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

PDS Tutorial LinkedLists

The document discusses various operations that can be performed on single linked lists including adding and deleting nodes. It provides code snippets in C to add a node after k nodes, delete a node after k nodes, add to the front and end of a linked list, delete from the k-th position, reverse a linked list, detect and remove loops, and print the k-th node from the end. It also lists some additional linked list problems.

Uploaded by

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

PDS (Theory) Tutorial

Single Linked Lists


Modifying a Single Linked List- Addition / Deletion
Q1a. Write a C program to add a new node after k nodes of a single linked list.

Q1b. What if k = n? What if k = 0?

Q2a. Write a C program to delete an existing node after k nodes of a single linked list.

Q2b. What if k = n? What if k = 0?


First let us define a single linked list
struct Node
{
int data;
struct Node *next;
};

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

Q. Reverse a Linked List in groups of given size

Q. Merge a linked list into another linked list at alternate positions

Q. Delete N nodes after M nodes of a linked list

You might also like