0% found this document useful (0 votes)
28 views9 pages

Assignment 2 1484

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

Assignment 2 1484

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

ASSIGNMENT-2

(ADVANCED CODING)
Kishan das
VU21CSEN0101071

1. Reverse a linked list: Given a linked list, reverse the linked lst and return the
pointer to the reversed list.

Code:

#include <stdio.h>
#include <stdlib.h>

// Defini5on for singly-linked list.


struct ListNode { int value;
struct ListNode* next;
};

// Func5on to reverse the linked list


struct ListNode* reverseLinkedList(struct ListNode* head) {
struct ListNode* prev = NULL; struct ListNode* current =
head;
struct ListNode* next_node = NULL;

while (current != NULL) {


next_node = current->next; // Save the next node
current->next = prev; // Reverse the link prev =
current; // Move prev to this node current =
next_node; // Move to the next node
}

return prev; // prev is the new head of the reversed list


}
// Helper func5on to print the linked list void
printList(struct ListNode* head) { struct
ListNode* temp = head; while
(temp != NULL) {
prinS("%d -> ", temp->value);
temp = temp->next;
}
prinS("NULL\n");
}

// Helper func5on to create a new node struct ListNode* createNode(int value) { struct
ListNode* newNode = (struct ListNode*)malloc(sizeof(struct ListNode)); newNode-
>value = value; newNode->next = NULL;
return newNode;
}

int main() {
// Crea5ng the linked list 1 -> 2 -> 3 -> 4 -> NULL
struct ListNode* head = createNode(1); head>next
= createNode(2); head->next->next =
createNode(3);
head->next->next->next = createNode(4);

prinS("Original List:\n");
printList(head);

// Reversing the linked list struct ListNode*


reversedHead = reverseLinkedList(head);

prinS("Reversed List:\n");
printList(reversedHead);

return 0;
}

Output:
2)Remove fullstops from a string: Given a string with
alphanumeric characters and fullstops, write a program
to remove the fullstops from the string.

Code:

#include <stdio.h>
#include <string.h>

// Function to remove full stops from a string void


removeFullStops(char* str) {
int i, j = 0; int len = strlen(str);

for (i = 0; i < len; i++)


{ if (str[i] != '.')
{ str[j++] = str[i]; //
Copy the character if it's not a full
stop
}
}
str[j] = '\0'; // Null-terminate the modified string
}

int main() { char


str[] =
"Hello.World.This.is.a.test.string.";

printf("Original String: %s\n", str);


removeFullStops(str);
printf("String after removing full stops:
%s\n", str);

return 0;
}

Output:

3) Middle of a linked list: Given a singly linked list, find


the element at the middle of the linked list.

Code:
#include <stdio.h>
#include <stdlib.h>

// Definition for singly-linked list node.


struct ListNode { int
value; struct ListNode* next;
};
// Function to find the middle of the linked list struct
ListNode* findMiddle(struct ListNode* head) { struct
ListNode *slow = head; struct ListNode *fast = head;

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


slow = slow->next; // Move slow pointer by 1 step
fast = fast->next->next; // Move fast pointer by 2 steps
}

return slow; // slow will be at the middle when fast reaches the
end
}

// Helper function to print the linked list void


printList(struct ListNode* head) { struct
ListNode* temp = head; while (temp !=
NULL) { printf("%d -> ", temp->value); temp
= temp->next;
}
printf("NULL\n");
}

// Helper function to create a new node struct


ListNode* createNode(int value) { struct
ListNode* newNode = (struct
ListNode*)malloc(sizeof(struct ListNode));
newNode->value = value; newNode->next = NULL;
return newNode;
}

int main() {
// Creating the linked list 1 -> 2 -> 3 -> 4 -> 5 -> NULL struct
ListNode* 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("Linked List:\n"); printList(head);

// Finding the middle of the linked list struct ListNode*


middle = findMiddle(head);

if (middle != NULL) { printf("Middle of the linked list is: %d\n",


middle-
>value);
} else { printf("The linked list is empty. \
n");
}

return 0;
}

Output:

4.) Maximum odd-even sum of a linked list: Given a


linkedlist, find the maximum sum sum of pairs of
alternate elements.

Code:
#include <stdio.h>
#include <stdlib.h>

// Definition for singly-linked list node.


struct ListNode { int
value; struct ListNode* next;
};

// Function to find the maximum odd-even sum int


maxOddEvenSum(struct ListNode* head) { int oddSum =
0, evenSum = 0; int index = 1; // Start with 1 to indicate
the first position is odd

struct ListNode* current = head;

while (current != NULL) { if


(index % 2 != 0) { // Odd index
oddSum += current->value;
} else {
// Even index evenSum += current-
>value;
}
current = current->next; index++;
}

return (oddSum > evenSum) ? oddSum :


evenSum;
}

// Helper function to print the linked list void


printList(struct ListNode* head) { struct ListNode*
temp = head; while (temp !=
NULL) { printf("%d -> ", temp->value);
temp = temp->next;
}
printf("NULL\n");
}

// Helper function to create a new node struct


ListNode* createNode(int value) { struct
ListNode* newNode = (struct
ListNode*)malloc(sizeof(struct ListNode));
newNode->value = value; newNode->next =
NULL; return newNode;
}

int main() {
// Creating the linked list 1 -> 2 -> 3 -> 4 -> 5 ->
NULL
struct ListNode* 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("Linked List:\n"); printList(head);

// Finding the maximum odd-even sum int


maxSum = maxOddEvenSum(head);

printf("Maximum Odd-Even Sum is: %d\n", maxSum);

return 0;
}

Output:

You might also like