// Wrie a program in c to implement Insertion ,Deletion and display #include #include struct node { int data; struct node *next; }; struct node *head; void insert(int x) { struct node *temp = (struct node *)malloc(sizeof(struct node)); temp->data = x; temp->next = head; head = temp; } void print() { struct node *temp = head; printf(" list is : "); while (temp != NULL) { printf("%d ", temp->data); temp = temp->next; } printf("\n"); } void deletion() { int index; printf("Enter the index which's you want to remove\n"); scanf("%d", &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; } int main() { int ch; head = NULL; printf("Enter the size of linked list \n"); int n, i, x; while (1) { scanf("%d", &n); printf("select any\n"); printf("1 for print\n"); printf("2 for insertion\n"); printf("3 for deletion\n"); printf("4 to exit\n"); scanf("%d", &ch); switch (ch) { case 1: print(); break; case 2: printf(" Enter the element of linked list\n"); for (i = 0; i < n; i++) { scanf("%d", &x); insert(x); print(); } break; case 3: deletion(); print(); break; case 4: exit(1); break; default: printf("You have entered wrong key"); break; } } return 0; }
Enter the size of linked list 5 select any 1 for print 2 for insertion 3 for deletion 4 to exit 2 Enter the element of linked list 55 list is : 55 44 list is : 44 55 33 list is : 33 44 55 22 list is : 22 33 44 55 11 list is : 11 22 33 44 55 select any 1 for print 2 for insertion 3 for deletion 4 to exit 3 Enter the index which's you want to remove 2 list is : 11 22 44 55 select any 1 for print 2 for insertion 3 for deletion 4 to exit 3 Enter the index which's you want to remove 2 list is : 11 22 55 select any 1 for print 2 for insertion 3 for deletion 4 to exit 4
0 Comments