Untitled
unknown
plain_text
2 months ago
2.3 kB
19
Indexable
#include<stdio.h> #include<stdlib.h> struct node { int data1; char data2; struct node* next; }; struct node* head; void createlist() { struct node* n1,*n2,*n3; n1 = (struct node*)malloc(sizeof(struct node)); n2 = (struct node*)malloc(sizeof(struct node)); n3 = (struct node*)malloc(sizeof(struct node)); n1->data1 = 10,n1->data2 = 'a'; n2->data1 = 20,n2->data2 = 'b'; n3->data1 = 30,n3->data2 = 'c'; n1->next = n2; n2->next = n3; n3->next = NULL; head = n1; } void display() { struct node* temp; temp = head; while(temp != NULL) { printf("%d %c\n",temp->data1,temp->data2); temp = temp->next; } } void insert_beg() { struct node *newnode; newnode = (struct node*)malloc(sizeof(struct node)); printf("Enter Data of the node to insert at beginning\n"); scanf("%d %c",&newnode->data1,&newnode->data2); newnode->next = head; head = newnode; } void insert_end() { struct node* tail = head; while(tail->next != NULL) { tail = tail->next; } struct node *newnode; newnode = (struct node*)malloc(sizeof(struct node)); printf("Enter Data of the node to insert at End\n"); scanf("%d %c",&newnode->data1,&newnode->data2); newnode->next = NULL; tail->next = newnode; tail = newnode; } void insert_at_middle() { int position; struct node* newnode,*temp; newnode = (struct node*) malloc(sizeof(struct node)); printf("Enter position to insert a node\n"); scanf("%d",&position); printf("Enter data to insert at that position\n"); scanf("%d %c",&newnode->data1,&newnode->data2); temp = head; for(int i = 1;i<position-1;i++) { temp = temp->next; } newnode->next = temp->next; temp->next = newnode; } int main() { createlist(); printf("Your Linked List is given below\n"); display(); insert_beg(); printf("After inserting at begeinning Linked list given below\n"); display(); insert_end(); printf("After Inserting at middle linked list given below\n"); display(); insert_at_middle(); printf("After Inserting at middle linked list given below\n"); display(); return 0; }
Editor is loading...
Leave a Comment