From a59ee1f312fa66416d40bed8b3ded7df4afd42f8 Mon Sep 17 00:00:00 2001 From: Arkadyuti Date: Thu, 1 Oct 2020 11:51:24 +0530 Subject: [PATCH] Added code for linked list reversal --- .../Data Structures/Linked_list_reversal.c | 61 +++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 CODE in C/Data Structures/Linked_list_reversal.c diff --git a/CODE in C/Data Structures/Linked_list_reversal.c b/CODE in C/Data Structures/Linked_list_reversal.c new file mode 100644 index 0000000..048e9cd --- /dev/null +++ b/CODE in C/Data Structures/Linked_list_reversal.c @@ -0,0 +1,61 @@ +#include +#include + +struct Node { + int data; + struct Node* next; +}; + +static void reverse(struct Node** head) +{ + struct Node* prev = NULL; + struct Node* curr = *head; + struct Node* next = NULL; + while (curr != NULL) { + next = curr->next; + curr->next = prev; + prev = curr; + curr = next; + } + *head = prev; +} + +void push(struct Node** head, int new_data) +{ + struct Node* new_node = (struct Node*)malloc(sizeof(struct Node)); + new_node->data = new_data; + new_node->next = (*head); + (*head) = new_node; +} + +void printList(struct Node* head) +{ + struct Node* temp = head; + while (temp != NULL) { + printf("%d ", temp->data); + temp = temp->next; + } +} + +int main() +{ + struct Node* head = NULL; + int n=0,p=0; + while(1) + { + printf("1.Append data to linked list\n2.Reverse the linked list\n3.Display the linked list\nAny other number to exit"); + scanf("%d",&n); + switch(n) + { + case 1 : printf("Enter the data\n"); + scanf("%d",&p); + push(&head,p); + break; + case 2 : reverse(&head); + break; + case 3 : printList(head); + break; + default: exit(0); + } + } +} \ No newline at end of file