Untitled
unknown
plain_text
2 years ago
1.9 kB
0
Indexable
Never
LINKED LIST #include <stdio.h> #include <stdlib.h> struct node { int data; struct node *next; }; struct node * insert(struct node *head, int data) { struct node *newnode = (struct node *)malloc(sizeof(struct node *)); newnode->data = data; newnode->next = head; return newnode; } void traversal(struct node *ptr) { while (ptr != NULL) { printf("%d", ptr->next); ptr = ptr->next; } } struct node * insertmid(struct node *head, int data,int index) { struct node *newnode = (struct node *)malloc(sizeof(struct node *)); struct node * ptr=NULL; ptr=head; for (int i = 0; i < (index-1); i++) { ptr=ptr->next; } newnode->data=data; newnode->next=ptr->next; ptr->next=newnode; return head; } void printdata(struct node*head) { if (head==NULL) { printf("linkedlist is empty"); } struct node*ptr=NULL; ptr=head; while (ptr != NULL) { printf("%d\n",ptr->data); ptr=ptr->next; } } int main() { struct node *head; struct node *secound; struct node *third; struct node *fourth; head=(struct node *)malloc(sizeof(struct node)); secound=(struct node *)malloc(sizeof(struct node)); third=(struct node *)malloc(sizeof(struct node)); fourth=(struct node *)malloc(sizeof(struct node)); head->data=40; head->next=secound; secound->data=56; secound->next=third; third->data=90; third->next=fourth; fourth->data=69; fourth->next=NULL; printf("before insertion\n"); printdata(head); printf("after insertion\n"); head=insert(head,56); printdata(head); printf("after insertionin middle\n"); head=insertmid(head,100,2); printdata(head); return 0; }