0% found this document useful (0 votes)
5 views11 pages

Adsa Assignment 1.1

The document contains code implementations for various linked list operations in C, including reversing a linked list without extra space, reversing nodes between two points, and finding the middle value of both odd and even length linked lists. Each section includes the necessary functions, main function examples, and output statements for testing. The code is structured with appropriate comments and uses dynamic memory allocation for node creation.

Uploaded by

raghav.ty
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)
5 views11 pages

Adsa Assignment 1.1

The document contains code implementations for various linked list operations in C, including reversing a linked list without extra space, reversing nodes between two points, and finding the middle value of both odd and even length linked lists. Each section includes the necessary functions, main function examples, and output statements for testing. The code is structured with appropriate comments and uses dynamic memory allocation for node creation.

Uploaded by

raghav.ty
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/ 11

ADSA

ASSIGNMENT 1

Name-Raghvender Tyagi
Roll No-2024PAI7329
1. Reverse linked list without using extra space.

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

struct Node {
int data;
struct Node* next;
};

struct Node* reverseList(struct Node* head) {

struct Node *curr = head, *prev = NULL, *next;

while (curr != NULL) {

next = curr->next;

curr->next = prev;

prev = curr;
curr = next;
}

return prev;
}

void printList(struct Node* node) {


while (node != NULL) {
printf(" %d", node->data);
node = node->next;
}
}

struct Node* createNode(int new_data) {


struct Node* new_node
= (struct Node*)malloc(sizeof(struct Node));
new_node->data = new_data;
new_node->next = NULL;
return new_node;
}

int main() {

struct Node* head = createNode(5);


head->next = createNode(2);
head->next->next = createNode(3);
head->next->next->next = createNode(6);
head->next->next->next->next = createNode(8);

printf("Given Linked list:");


printList(head);

head = reverseList(head);

printf("\nReversed Linked List:");


printList(head);

return 0;
}

OUTPUT:
2. Reverse the nodes in a linked list present between some
random points P and Q in a linked list.
#include <stdio.h>
#include <stdlib.h>

struct Node {
int data;
struct Node *next;
};

struct Node *reverse(struct Node *head) {


struct Node *prevNode = NULL;
struct Node *currNode = head;
while (currNode) {
struct Node *nextNode = currNode->next;
currNode->next = prevNode;
prevNode = currNode;
currNode = nextNode;
}
return prevNode;
}

struct Node *reverseBetween(struct Node *head, int m, int n) {


if (m == n)
return head;

struct Node *revs = NULL, *revs_prev = NULL;


struct Node *revend = NULL, *revend_next = NULL;

int i = 1;
struct Node *currNode = head;
while (currNode && i <= n) {

if (i < m)
revs_prev = currNode;

if (i == m)
revs = currNode;

if (i == n) {
revend = currNode;
revend_next = currNode->next;
}
currNode = currNode->next;
i++;
}

revend->next = NULL;

revend = reverse(revs);

if (revs_prev)
revs_prev->next = revend;
else
head = revend;

revs->next = revend_next;

return head;
}

void print(struct Node *head) {


while (head != NULL) {
printf("%d ", head->data);
head = head->next;
}
printf("NULL\n");
}

struct Node *createNode(int new_data) {


struct Node *new_node =
(struct Node *)malloc(sizeof(struct Node));
new_node->data = new_data;
new_node->next = NULL;
return new_node;
}

int main() {

struct Node *head = createNode(71);


head->next = createNode(12);
head->next->next = createNode(23);
head->next->next->next = createNode(49);
head->next->next->next->next = createNode(34);
head->next->next->next->next->next = createNode(56);
head->next->next->next->next->next->next = createNode(87);
printf("Original list: ");
print(head);

head = reverseBetween(head, 3, 6);

printf("Modified list: ");


print(head);

return 0;
}

OUTPUT:
3.Find the middle of the linked list

a) ODD LENGTH

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

struct Node {
int data;
struct Node* next;
};

struct Node* createNode(int new_data) {


struct Node* newNode
= (struct Node*)malloc(sizeof(struct Node));
newNode->data = new_data;
newNode->next = NULL;
return newNode;
}

int getMiddle(struct Node* head) {

int list_values[100];
int size = 0;

while (head != NULL) {


list_values[size++] = head->data;
head = head->next;
}
int middle_idx = size / 2;

int middle_value = list_values[middle_idx];


return middle_value;
}

int main() {

struct Node* head = createNode(25);


head->next = createNode(83);
head->next->next = createNode(29);
head->next->next->next = createNode(69);
head->next->next->next->next = createNode(52);
head->next->next->next->next->next = createNode(43);
head->next->next->next->next->next->next=
createNode(75);

printf("Middle Value Of Linked List is: %d\n",


getMiddle(head));

return 0;

}
OUTPUT:
b) EVEN LENGTH

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

struct Node {
int data;
struct Node* next;
};

struct Node* createNode(int new_data) {


struct Node* newNode
= (struct Node*)malloc(sizeof(struct Node));
newNode->data = new_data;
newNode->next = NULL;
return newNode;
}
int getMiddle(struct Node* head) {

int list_values[100];
int size = 0;

while (head != NULL) {


list_values[size++] = head->data;
head = head->next;
}

int middle_idx = size / 2;

int middle_value = list_values[middle_idx];


return middle_value;
}

int main() {

struct Node* head = createNode(15);


head->next = createNode(23);
head->next->next = createNode(38);
head->next->next->next = createNode(49);
head->next->next->next->next = createNode(57);
head->next->next->next->next->next = createNode(69);
printf("Middle Value Of Linked List is: %d\n",
getMiddle(head));

return 0;
}

OUTPUT:

You might also like