0% found this document useful (0 votes)
3 views21 pages

Link List Solution 1st Level

The document contains multiple C code snippets that implement various operations on linked lists, such as creating nodes, copying lists, counting nodes, searching for elements, creating alternating lists, splitting lists, merging sorted lists, concatenating lists, comparing lists, counting occurrences, and reversing lists. Each code snippet includes a main function that demonstrates the functionality of the respective operation. The code snippets are structured with proper function definitions and utility functions for printing linked lists.

Uploaded by

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

Link List Solution 1st Level

The document contains multiple C code snippets that implement various operations on linked lists, such as creating nodes, copying lists, counting nodes, searching for elements, creating alternating lists, splitting lists, merging sorted lists, concatenating lists, comparing lists, counting occurrences, and reversing lists. Each code snippet includes a main function that demonstrates the functionality of the respective operation. The code snippets are structured with proper function definitions and utility functions for printing linked lists.

Uploaded by

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

1.#include <stdio.

h>

#include <stdlib.h>

Struct Node {

Int data;

Struct Node* next;

};

// Function to create a new node

Struct Node* createNode(int data) {

Struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));

newNode->data = data;

newNode->next = NULL;

return newNode;

// Function to make a copy of a linked list

Struct Node* copyList(struct Node* head) {

If (head == NULL) return NULL;

Struct Node* newHead = createNode(head->data);

Struct Node* current = head->next;

Struct Node* copyCurrent = newHead;

While (current != NULL) {

copyCurrent->next = createNode(current->data);

copyCurrent = copyCurrent->next;
current = current->next;

Return newHead;

// Utility function to print a linked list

Void printList(struct Node* head) {

While (head != NULL) {

Printf(“%d -> “, head->data);

Head = head->next;

Printf(“NULL\n”);

Int main() {

// Creating a sample linked list: 1 -> 2 -> 3 -> NULL

Struct Node* head = createNode(1);

Head->next = createNode(2);

Head->next->next = createNode(3);

Printf(“Original list:\n”);

printList(head);

struct Node* copiedList = copyList(head);

printf(“Copied list:\n”);

printList(copiedList);
return 0;

2.#include <stdio.h>

#include <stdlib.h>

Struct Node {

Int data;

Struct Node* next;

};

// Function to create a new node

Struct Node* createNode(int data) {

Struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));

newNode->data = data;

newNode->next = NULL;

return newNode;

// Function to count the number of nodes in a linked list

Int countNodes(struct Node* head) {

Int count = 0;

While (head != NULL) {

Count++;

Head = head->next;

Return count;

}
Int main() {

// Creating a sample linked list: 1 -> 2 -> 3 -> NULL

Struct Node* head = createNode(1);

Head->next = createNode(2);

Head->next->next = createNode(3);

Int nodeCount = countNodes(head);

Printf(“Number of nodes in the linked list: %d\n”, nodeCount);

Return 0;

3.#include <stdio.h>

#include <stdlib.h>

Struct Node {

Int data;

Struct Node* next;

};

// Function to create a new node

Struct Node* createNode(int data) {

Struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));

newNode->data = data;

newNode->next = NULL;

return newNode;

}
// Function to search for an element in a linked list

Int searchElement(struct Node* head, int key) {

Int position = 0;

While (head != NULL) {

If (head->data == key) {

Return position;

Head = head->next;

Position++;

Return -1; // Element not found

Int main() {

// Creating a sample linked list: 1 -> 2 -> 3 -> NULL

Struct Node* head = createNode(1);

Head->next = createNode(2);

Head->next->next = createNode(3);

Int key = 2;

Int position = searchElement(head, key);

If (position != -1) {

Printf(“Element %d found at position %d\n”, key, position);

} else {

Printf(“Element %d not found in the list\n”, key);

}
Return 0;

4.#include <stdio.h>

#include <stdlib.h>

Struct Node {

Int data;

Struct Node* next;

};

// Function to create a new node

Struct Node* createNode(int data) {

Struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));

newNode->data = data;

newNode->next = NULL;

return newNode;

// Function to create a new list using alternating nodes from the given list

Struct Node* createAlternatingList(struct Node* head) {

If (head == NULL) return NULL;

Struct Node* newHead = createNode(head->data);

Struct Node* current = head->next;

Struct Node* newCurrent = newHead;


While (current != NULL && current->next != NULL) {

Current = current->next->next;

If (current != NULL) {

newCurrent->next = createNode(current->data);

newCurrent = newCurrent->next;

Return newHead;

// Utility function to print a linked list

Void printList(struct Node* head) {

While (head != NULL) {

Printf(“%d -> “, head->data);

Head = head->next;

Printf(“NULL\n”);

Int main() {

// Creating a sample linked list: 1 -> 2 -> 3 -> 4 -> 5 -> NULL

Struct Node* head = createNode(1);

Head->next = createNode(2);

Head->next->next = createNode(3);

Head->next->next->next = createNode(4);

Head->next->next->next->next = createNode(5);
Printf(“Original list:\n”);

printList(head);

struct Node* altList = createAlternatingList(head);

printf(“New list with alternating nodes:\n”);

printList(altList);

return 0;

5.#include <stdio.h>

#include <stdlib.h>

Struct Node {

Int data;

Struct Node* next;

};

// Function to create a new node

Struct Node* createNode(int data) {

Struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));

newNode->data = data;

newNode->next = NULL;

return newNode;

// Function to split the linked list at the middle


Void splitList(struct Node* head, struct Node** firstHalf, struct Node**
secondHalf) {

If (head == NULL || head->next == NULL) {

*firstHalf = head;

*secondHalf = NULL;

Return;

Struct Node* slow = head;

Struct Node* fast = head;

Struct Node* prev = NULL;

// Move fast by 2 steps and slow by 1 step

While (fast != NULL && fast->next != NULL) {

Prev = slow;

Slow = slow->next;

Fast = fast->next->next;

// Split the list into two halves

*firstHalf = head;

*secondHalf = slow;

If (prev != NULL) {

Prev->next = NULL;

}
// Utility function to print a linked list

Void printList(struct Node* head) {

While (head != NULL) {

Printf(“%d -> “, head->data);

Head = head->next;

Printf(“NULL\n”);

Int main() {

// Creating a sample linked list: 1 -> 2 -> 3 -> 4 -> 5 -> NULL

Struct Node* head = createNode(1);

Head->next = createNode(2);

Head->next->next = createNode(3);

Head->next->next->next = createNode(4);

Head->next->next->next->next = createNode(5);

Struct Node* firstHalf = NULL;

Struct Node* secondHalf = NULL;

splitList(head, &firstHalf, &secondHalf);

printf(“First half:\n”);

printList(firstHalf);

printf(“Second half:\n”);

printList(secondHalf);
return 0;

6.#include <stdio.h>

#include <stdlib.h>

Struct Node {

Int data;

Struct Node* next;

};

// Function to create a new node

Struct Node* createNode(int data) {

Struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));

newNode->data = data;

newNode->next = NULL;

return newNode;

// Function to merge two sorted linked lists

Struct Node* mergeSortedLists(struct Node* list1, struct Node* list2) {

If (list1 == NULL) return list2;

If (list2 == NULL) return list1;

Struct Node* result = NULL;

If (list1->data <= list2->data) {

Result = list1;
Result->next = mergeSortedLists(list1->next, list2);

} else {

Result = list2;

Result->next = mergeSortedLists(list1, list2->next);

Return result;

// Utility function to print a linked list

Void printList(struct Node* head) {

While (head != NULL) {

Printf(“%d -> “, head->data);

Head = head->next;

Printf(“NULL\n”);

Int main() {

// Creating two sorted linked lists

Struct Node* list1 = createNode(1);

List1->next = createNode(3);

List1->next->next = createNode(5);

Struct Node* list2 = createNode(2);

List2->next = createNode(4);

List2->next->next = createNode(6);
Printf(“List 1:\n”);

printList(list1);

printf(“List 2:\n”);

printList(list2);

struct Node* mergedList = mergeSortedLists(list1, list2);

printf(“Merged sorted list:\n”);

printList(mergedList);

return 0;

7.#include <stdio.h>

#include <stdlib.h>

Struct Node {

Int data;

Struct Node* next;

};

// Function to create a new node

Struct Node* createNode(int data) {

Struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));

newNode->data = data;

newNode->next = NULL;

return newNode;

}
// Function to concatenate two linked lists

Struct Node* concatenateLists(struct Node* list1, struct Node* list2) {

If (list1 == NULL) return list2;

If (list2 == NULL) return list1;

Struct Node* temp = list1;

While (temp->next != NULL) {

Temp = temp->next;

Temp->next = list2;

Return list1;

// Utility function to print a linked list

Void printList(struct Node* head) {

While (head != NULL) {

Printf(“%d -> “, head->data);

Head = head->next;

Printf(“NULL\n”);

Int main() {

// Creating two linked lists

Struct Node* list1 = createNode(1);

List1->next = createNode(2);

List1->next->next = createNode(3);
Struct Node* list2 = createNode(4);

List2->next = createNode(5);

List2->next->next = createNode(6);

Printf(“List 1:\n”);

printList(list1);

printf(“List 2:\n”);

printList(list2);

struct Node* concatenatedList = concatenateLists(list1, list2);

printf(“Concatenated list:\n”);

printList(concatenatedList);

return 0;

8.#include <stdio.h>

#include <stdlib.h>

Struct Node {

Int data;

Struct Node* next;

};

// Function to create a new node

Struct Node* createNode(int data) {

Struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));


newNode->data = data;

newNode->next = NULL;

return newNode;

// Function to compare two linked lists

Int compareLists(struct Node* list1, struct Node* list2) {

While (list1 != NULL && list2 != NULL) {

If (list1->data != list2->data) {

Return 0; // Lists are not equal

List1 = list1->next;

List2 = list2->next;

If (list1 == NULL && list2 == NULL) {

Return 1; // Lists are equal

Return 0; // Lists are not equal

Int main() {

// Creating two linked lists

Struct Node* list1 = createNode(1);

List1->next = createNode(2);

List1->next->next = createNode(3);
Struct Node* list2 = createNode(1);

List2->next = createNode(2);

List2->next->next = createNode(3);

If (compareLists(list1, list2)) {

Printf(“The lists are equal.\n”);

} else {

Printf(“The lists are not equal.\n”);

Return 0;

9.#include <stdio.h>

#include <stdlib.h>

Struct Node {

Int data;

Struct Node* next;

};

// Function to create a new node

Struct Node* createNode(int data) {

Struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));

newNode->data = data;

newNode->next = NULL;

return newNode;

}
// Function to count the occurrences of a given number in the list

Int countOccurrences(struct Node* head, int key) {

Int count = 0;

While (head != NULL) {

If (head->data == key) {

Count++;

Head = head->next;

Return count;

Int main() {

// Creating a sample linked list: 1 -> 2 -> 3 -> 2 -> 5 -> 2 -> NULL

Struct Node* head = createNode(1);

Head->next = createNode(2);

Head->next->next = createNode(3);

Head->next->next->next = createNode(2);

Head->next->next->next->next = createNode(5);

Head->next->next->next->next->next = createNode(2);

Int key = 2;

Int count = countOccurrences(head, key);

Printf(“The number %d occurs %d times in the list.\n”, key, count);

Return 0;
}

10.#include <stdio.h>

#include <stdlib.h>

Struct Node {

Int data;

Struct Node* next;

};

// Function to create a new node

Struct Node* createNode(int data) {

Struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));

newNode->data = data;

newNode->next = NULL;

return newNode;

// Function to reverse the linked list

Struct Node* reverseList(struct Node* head) {

Struct Node* prev = NULL;

Struct Node* current = head;

Struct Node* next = NULL;

While (current != NULL) {

Next = current->next; // Store next node

Current->next = prev; // Reverse the link

Prev = current; // Move prev to current


Current = next; // Move to next node

Return prev;

// Utility function to print a linked list

Void printList(struct Node* head) {

While (head != NULL) {

Printf(“%d -> “, head->data);

Head = head->next;

Printf(“NULL\n”);

Int main() {

// Creating a sample linked list: 1 -> 2 -> 3 -> 4 -> 5 -> NULL

Struct Node* head = createNode(1);

Head->next = createNode(2);

Head->next->next = createNode(3);

Head->next->next->next = createNode(4);

Head->next->next->next->next = createNode(5);

Printf(“Original list:\n”);

printList(head);

head = reverseList(head);
printf(“Reversed list:\n”);

printList(head);

return 0;

You might also like