From 1594ea602fcd34c81ecce5ab296901b3a28a6004 Mon Sep 17 00:00:00 2001 From: VaibhavTiw Date: Tue, 4 Oct 2022 00:12:35 +0530 Subject: [PATCH] Creation of Singly Linked List using diff functions. --- data_structures/linked_list/SinglyCreation.c | 74 ++++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 data_structures/linked_list/SinglyCreation.c diff --git a/data_structures/linked_list/SinglyCreation.c b/data_structures/linked_list/SinglyCreation.c new file mode 100644 index 0000000000..c4525bbe93 --- /dev/null +++ b/data_structures/linked_list/SinglyCreation.c @@ -0,0 +1,74 @@ +#include +#include + +struct node +{ + int data; + struct node* next; +}; +struct node* head; +void insertAtHead(int x) +{ + struct node* temp; + temp = (struct node*)malloc(sizeof(struct node)); + temp->data = x; + temp->next = head; + head = temp; +} + +void insertAtNode(int x, int n) +{ + struct node* p = head; + struct node* pnext = head->next; + struct node* temp = (struct node*)malloc(sizeof(struct node)); + for (int i = 1; i < n - 1; i++) + { + p = p->next; + pnext = pnext->next; + } + temp->data = x; + p->next = temp; + temp->next = pnext; +} + +void insertAtLast(int x) +{ + struct node* p = head; + struct node* temp = (struct node*)malloc(sizeof(struct node)); + temp->data = x; + while (p->next != NULL) + { + p = p->next; + } + p->next = temp; + temp->next = NULL; +} + +void display() +{ + struct node* temp = head; + while (temp != NULL) + { + printf("%d ", temp->data); + temp = temp->next; + } + printf("\n"); +} + +int main() +{ + insertAtHead(1); + insertAtHead(2); + insertAtHead(3); + display(); + printf("\n"); + insertAtNode(11, 2); + insertAtNode(11, 3); + display(); + printf("\n"); + insertAtLast(5); + insertAtLast(6); + display(); + printf("\n"); + return 0; +}