From 8cabe00549e9d9e82d5afb66d753ef413316b144 Mon Sep 17 00:00:00 2001 From: ronitsrma16 <43707140+ronitsrma16@users.noreply.github.com> Date: Wed, 21 Aug 2024 01:03:03 +0530 Subject: [PATCH 1/2] Create linked list and traversal of the linked list --- ronit/c/linked_list.c | 59 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 ronit/c/linked_list.c diff --git a/ronit/c/linked_list.c b/ronit/c/linked_list.c new file mode 100644 index 0000000..2ace660 --- /dev/null +++ b/ronit/c/linked_list.c @@ -0,0 +1,59 @@ +#include +#include + +typedef struct{ + int data; + struct Node *next_address; +}Node; + + +void print_linkedList(Node *head_pointer); +int main() +{ + int run_again = 0; + + + Node *Head = (Node*)malloc(sizeof(Node)); + Node *first_node = (Node*)malloc(sizeof(Node)); + Node *second_node = (Node*)malloc(sizeof(Node)); + + // Initialize head Node + Head->next_address = (struct Node*)first_node; + // Initialize first Node + first_node->data = 1; + first_node->next_address = (struct Node*)second_node; + // Initialize second Node + second_node->data = 2; + second_node->next_address = 0; + + do{ + + print_linkedList(Head); + + printf("\n"); + printf("Do you run program again\r\n"); + scanf("%d",&run_again); + }while(run_again); + + return 0; +} + +// Traversal Of the Nodes +void print_linkedList(Node *head_pointer) +{ + int counter = 1; + // To avoid printing of the head Node data + if(head_pointer->next_address == 0) + { + return; + } + else + { + head_pointer = (Node*)head_pointer->next_address; + } + while(head_pointer != 0) + { + printf("Node %d value - %d\r\n", counter++, head_pointer->data); + head_pointer = (Node*)head_pointer->next_address; + } +} From ce3297def1275551339b7f79ba61770146c2a903 Mon Sep 17 00:00:00 2001 From: ronitsrma16 <43707140+ronitsrma16@users.noreply.github.com> Date: Mon, 26 Aug 2024 01:08:47 +0530 Subject: [PATCH 2/2] ronit/linked_list - wap to insert/delete node in linked list Created a function to insert, delete, and traverse the linked list --- ronit/c/linked_list.c | 64 +++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 62 insertions(+), 2 deletions(-) diff --git a/ronit/c/linked_list.c b/ronit/c/linked_list.c index 2ace660..5e934de 100644 --- a/ronit/c/linked_list.c +++ b/ronit/c/linked_list.c @@ -1,3 +1,9 @@ + +/* + Brief - This program is to create a linked list + Here I'm counting Head as 0th index which don't have any value/data + Linked list actual Node counting will start from 1. +*/ #include #include @@ -7,7 +13,11 @@ typedef struct{ }Node; -void print_linkedList(Node *head_pointer); +void print_linkedList(Node *head_pointer); +Node* Insert_Node(Node *Head, int position, int data); +void print_Head(Node *Head_); +Node* Delete_Node(Node *Head, int position); + int main() { int run_again = 0; @@ -29,7 +39,14 @@ int main() do{ print_linkedList(Head); - + Head = (Node*)Insert_Node(Head, 1, 11); + Head = ( Node*)Insert_Node(Head, 2, 12); + Head = ( Node*)Insert_Node(Head, 3, 13); + Head = ( Node*)Insert_Node(Head, 4, 14); + Head = ( Node*)Insert_Node(Head, 5, 15); + Head = Delete_Node(Head, 3); + printf("Linked List After insertion\r\n"); + print_linkedList(Head); printf("\n"); printf("Do you run program again\r\n"); scanf("%d",&run_again); @@ -38,6 +55,49 @@ int main() return 0; } +Node* Delete_Node(Node *Head, int position) +{ + if(position < 1) + return 0; + + Node *tempHead = Head; + int loop = 1; + while(loop != position) + { + tempHead = tempHead->next_address; + loop++; + } + Node *del_node = tempHead->next_address; + tempHead->next_address = del_node->next_address; + + return Head; + +} + +Node* Insert_Node(Node *Head, int position, int data) +{ + /* since we count Node positions from 1 */ + if(position < 1) + return 0; + Node *newNode = (Node*)malloc(sizeof(Node)); + Node *tempHead = Head; + + + newNode->data = data; + int loop = 1; + while(loop != position) + { + tempHead = (Node*)tempHead->next_address; + loop++; + } + + newNode->next_address = tempHead->next_address; + tempHead->next_address = (struct Node*)newNode; + + + return Head; +} + // Traversal Of the Nodes void print_linkedList(Node *head_pointer) {