diff --git a/insertion_in_a_linked_list.cpp b/insertion_in_a_linked_list.cpp new file mode 100644 index 00000000..391c4038 --- /dev/null +++ b/insertion_in_a_linked_list.cpp @@ -0,0 +1,103 @@ +#include +#include + +struct Node +{ + int data; + struct Node * next; +}; + +void linkedListTraversal(struct Node *ptr) +{ + while(ptr != NULL){ + printf("Element: %d\n", ptr->data); + ptr = ptr->next; + } +} + +struct Node * insertAtFirst(struct Node *head, int data){ + struct Node * ptr = (struct Node *)malloc(sizeof(struct Node)); + ptr->next = head; + ptr->data = data; + return ptr; +} + +struct Node * insertAfterNode(struct Node *head, struct Node * prevNode, int data){ + struct Node * ptr = (struct Node *)malloc(sizeof(struct Node)); + ptr->data = data; + ptr->next = prevNode->next; + prevNode->next = ptr; + return head; +} + +struct Node * insertAtIndex(struct Node *head, int data, int index){ + struct Node * ptr = (struct Node *)malloc(sizeof(struct Node)); + struct Node * p = head; + int i = 0; + + while (i != index-1) + { + p = p->next; + i++; + } + ptr->data = data; + ptr->next = p->next; + p->next = ptr; + return head; +} + +struct Node * insertAtEnd(struct Node * head, int data){ + struct Node * ptr = (struct Node *)malloc(sizeof(struct Node)); + ptr->data = data; + struct Node * p = head; + + while (p->next != NULL) + { + p = p->next; + } + p->next = ptr; + ptr->next = NULL; + return head; +} + + + +int main(){ + struct Node *head; + struct Node *second; + struct Node *third; + struct Node *fourth; + + // Allocate memory for nodes in the linkedlist. + head = (struct Node*)malloc(sizeof(struct Node)); + second = (struct Node*)malloc(sizeof(struct Node)); + third = (struct Node*)malloc(sizeof(struct Node)); + fourth = (struct Node*)malloc(sizeof(struct Node)); + + // Link first and second nodes + head->data = 7; + head->next = second; + + // Link second and third nodes + second->data = 11; + second->next = third; + + // Link third and fourth nodes + third->data = 41; + third->next = fourth; + + // Terminate the list at the third node + fourth->data = 66; + fourth->next = NULL; + + printf("Linked List before insertion\n"); + linkedListTraversal(head); + printf("\n"); + printf("Linked List after insertion\n"); + // head = insertAtFirst(head, 56); + // head = insertAtIndex(head, 56, 2); + head = insertAtEnd(head, 56); + // head = insertAfterNode(head, second, 45); + linkedListTraversal(head); + return 0; +}