#include<stdio.h> #include<stdlib.h> struct Node{ int data; struct Node *next; }; void traversal(struct Node *ptr){ while (ptr != NULL) { printf("Element : %d\n", ptr->data); ptr = ptr->next; } } // Case 1 struct Node * insertAtFirst(struct Node *head, int data){ struct Node * ptr = (struct Node *) malloc(sizeof(struct Node)); ptr->data = data; ptr->next = head; return ptr; } // Case 2 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; } // Case 3 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; } // Case 4 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 * insertbeginning(struct Node *head ,int data){ struct Node *ptr = (struct Node *)malloc(sizeof(struct Node)); ptr->data = data; ptr->next = head; return ptr; } struct Node * insertbetween(struct Node *head, int data ,int index) { struct Node *ptr=(struct Node *)malloc(sizeof(struct Node)); struct Node * p = head; ptr->data=data; int i =0; while ( i !=index-1) { p = p->next; i++; } ptr->next = p->next; return head; } // Case 1: Deleting the first element from the linked list struct Node * deleteFirst(struct Node * head){ struct Node * ptr = head; head = head->next; free(ptr); return head; } // Case 2: Deleting the element at a given index from the linked list struct Node * deleteAtIndex(struct Node * head, int index){ struct Node *p = head; struct Node *q = head->next; for (int i = 0; i < index-1; i++) { p = p->next; q = q->next; } p->next = q->next; free(q); return head; } // Case 3: Deleting the last element struct Node * deleteAtLast(struct Node * head){ struct Node *p = head; struct Node *q = head->next; while(q->next !=NULL) { p = p->next; q = q->next; } p->next = NULL; free(q); return head; } int main(){ struct Node *head; struct Node *second; struct Node *third; struct Node *fourth; 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)); head ->data=3; head ->next=second; second ->data=9; second ->next=third; third ->data=7; third->next=fourth; fourth->data=34; fourth->next=NULL; //traversal(head); //printf("-----------\n"); // head = insertbeginning(head,17); // printf("-----------\n"); //traversal(head); //printf("-----------\n"); insertbetween(head,45,2); //printf("-----------\n"); traversal(head); return 0; }
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter