50% found this document useful (2 votes)
851 views3 pages

DS Lab Manual R23

Uploaded by

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

DS Lab Manual R23

Uploaded by

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

1.

ITERATIVE PROCESS

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

// Node structure
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 iteratively


struct Node* reverseIterative(struct Node* head) {
struct Node *prev = NULL, *current = head, *next = NULL;

while (current != NULL) {


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

return prev;
}

// Function to print the linked list


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

// Driver code
int main() {
// Create a linked list: 1 -> 2 -> 3 -> 4 -> 5
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: ");


printList(head);

// Reverse the linked list


head = reverseIterative(head);

printf("Reversed list: ");


printList(head);

return 0;
}

2. Recursive Approach:

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

// Node structure
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 recursively


struct Node* reverseRecursive(struct Node* head) {
if (head == NULL || head->next == NULL) {
return head;
}

struct Node* rest = reverseRecursive(head->next);


head->next->next = head;
head->next = NULL;
return rest;
}

// Function to print the linked list


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

// Driver code
int main() {
// Create a linked list: 1 -> 2 -> 3 -> 4 -> 5
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: ");


printList(head);

// Reverse the linked list


head = reverseRecursive(head);

printf("Reversed list: ");


printList(head);

return 0;
}

You might also like