Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
4 views7 pages

Assignment 6

nice
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)
4 views7 pages

Assignment 6

nice
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/ 7

Assignment 5.

Q1. Implement Singly Linked List and perform the operations like insertion, deletion, display.

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

typedef struct Node {


int data;
struct Node* next;
} Node;

Node* insert(Node* head, int data) {


Node* newNode = (Node*)malloc(sizeof(Node));
if (!newNode) {
printf("Memory allocation failed\n");
return head;
}
newNode->data = data;
newNode->next = NULL;

if (head == NULL) {
return newNode;
}

Node* temp = head;


while (temp->next != NULL)
temp = temp->next;
temp->next = newNode;

return head;
}

Node* deleteNode(Node* head, int data) {


if (head == NULL) return NULL;

Node* temp = head;


Node* prev = NULL;

if (temp->data == data) {
head = temp->next;
free(temp);
return head;
}

while (temp != NULL && temp->data != data) {


prev = temp;
temp = temp->next;
}

if (temp == NULL) {
printf("Element %d not found\n", data);
return head;
}

prev->next = temp->next;
free(temp);
return head;
}
void display(Node* head) {
if (head == NULL) {
printf("List is empty\n");
return;
}
Node* temp = head;
printf("List: ");
while (temp != NULL) {
printf("%d -> ", temp->data);
temp = temp->next;
}
printf("NULL\n");
}

int main() {
Node* head = NULL;
int choice, val;

while (1) {
printf("\n1.Insert\n2.Delete\n3.Display\n4.Exit\nEnter choice: ");
scanf("%d", &choice);

switch (choice) {
case 1:
printf("Enter value to insert: ");
scanf("%d", &val);
head = insert(head, val);
break;
case 2:
printf("Enter value to delete: ");
scanf("%d", &val);
head = deleteNode(head, val);
break;
case 3:
display(head);
break;
case 4:

while (head != NULL) {


Node* temp = head;
head = head->next;
free(temp);
}
exit(0);
default:
printf("Invalid choice\n");
}
}

return 0;
}
Q.2) Implement Singly Linked List and perform reverse and sorting operations .

Ans—

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

typedef struct Node {


int data;
struct Node* next;
} Node;

void insert(Node** head_ref, int data) {


Node* new_node = (Node*)malloc(sizeof(Node));
if (!new_node) {
printf("Memory allocation failed\n");
return;
}
new_node->data = data;
new_node->next = NULL;

if (*head_ref == NULL) {
*head_ref = new_node;
return;
}

Node* temp = *head_ref;


while (temp->next)
temp = temp->next;
temp->next = new_node;
}

void reverse(Node** head_ref) {


Node* prev = NULL;
Node* current = *head_ref;
Node* next = NULL;

while (current != NULL) {


next = current->next;
current->next = prev;
prev = current;
current = next;
}
*head_ref = prev;
}

Node* get_middle(Node* head) {


if (head == NULL)
return head;

Node* slow = head;


Node* fast = head->next;

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


slow = slow->next;
fast = fast->next->next;
}
return slow;
}

Node* sorted_merge(Node* a, Node* b) {


if (a == NULL)
return b;
if (b == NULL)
return a;

Node* result = NULL;

if (a->data <= b->data) {


result = a;
result->next = sorted_merge(a->next, b);
} else {
result = b;
result->next = sorted_merge(a, b->next);
}
return result;
}

Node* merge_sort(Node* head) {


if (head == NULL || head->next == NULL)
return head;

Node* middle = get_middle(head);


Node* next_to_middle = middle->next;

middle->next = NULL;

Node* left = merge_sort(head);


Node* right = merge_sort(next_to_middle);

Node* sorted_list = sorted_merge(left, right);


return sorted_list;
}
void display(Node* head) {
if (!head) {
printf("List is empty.\n");
return;
}
Node* temp = head;
while (temp != NULL) {
printf("%d", temp->data);
if (temp->next)
printf(" -> ");
temp = temp->next;
}
printf("\n");
}

void free_list(Node** head_ref) {


Node* current = *head_ref;
Node* next;

while (current != NULL) {


next = current->next;
free(current);
current = next;
}
*head_ref = NULL;
}

int main() {
Node* head = NULL;
int choice, val;

while (1) {
printf("\nMenu:\n1. Insert\n2. Reverse\n3. Sort\n4. Display\n5. Exit\nEnter choice: ");
if (scanf("%d", &choice) != 1) break;

switch (choice) {
case 1:
printf("Enter value to insert: ");
if (scanf("%d", &val) != 1) {
printf("Invalid input\n");
return 1;
}
insert(&head, val);
break;

case 2:
reverse(&head);
printf("List reversed.\n");
break;

case 3:
head = merge_sort(head);
printf("List sorted.\n");
break;

case 4:
printf("Current List: ");
display(head);
break;

case 5:
free_list(&head);
printf("Exiting...\n");
return 0;

default:
printf("Invalid choice. Try again.\n");
}
}
free_list(&head);
return 0;
}

You might also like