Doubly linked list C Program

#include #include #include struct dll { int info; struct dll *next; struct dll *prev; }; struct dll *start = NULL; int x = 0, c = 0; void create_node(); void display(); void insertion_info(); void split(); void deletion_before(); void deletion_after(); void deletion_info(); void insertion_before(); void insertion_after(); void insertion_after() { struct dll *newnode, *p; int data, b, i; if (start != NULL) { printf("nthe total no. of nodes are= %d", c); printf("nenter the no. of node after which the newnode has to be inserted"); scanf("%d", &b); if (b > c) { printf("wrong choice entered"); goto z; } } printf("nenter the information of the newnode"); scanf("%d", &data); newnode = (struct dll *) malloc(sizeof(struct dll)); c++; newnode->info = data; p = start; if (start == NULL) printf("nlinked list is empty"); else { for (i = 1; i < b; i++) p = p->next; if (p->next == NULL) { newnode->next = p->next; p->next = newnode; newnode->prev = p; } else { newnode->next = p->next; p->next->prev = newnode; p->next = newnode; newnode->prev = p; } } z:} void create_node() { int m, i; struct dll *newnode; printf("nenter the no. of nodes to be inserted"); scanf("%d", &x); if (x < 1) printf("nentered a wrong no."); else { for (i = 1; i <= x; i++) { printf("nenter the information of the %d node", i); fflush(stdin); scanf("%d", &m); newnode = (struct dll *) malloc(sizeof(struct dll)); newnode->info = m; if (start == NULL) { newnode->prev = NULL; newnode->next = start; start = newnode; } else { newnode->next = start; start = newnode; newnode->next->prev = newnode; } } c = c + x; } } void display() { struct dll *p; p = start; if (p == NULL) printf("link list is empty"); else { while (p != NULL) { printf("t%d", p->info); p = p->next; } } } void insertion_before() { struct dll *p, *newnode; int n, s, i; if (start != NULL) { printf("nthe total no. of nodes are= %d", c); printf("nenter the no. before which the node has to be inserted"); scanf("%d", &n); if (n > c) { printf("wrong choice"); goto x; } } printf("nenter the information of the inserted node"); scanf("%d", &s); p = start; newnode = (struct dll *) malloc(sizeof(struct dll)); c++; newnode->info = s; if (start == NULL) printf("linked list is empty"); else if (n == 1) { newnode->next = start; start->prev = newnode; start = newnode; } else { for (i = 1; i < n - 1; i++) p = p->next; newnode->next = p->next; p->next->prev = newnode; p->next = newnode; newnode->prev = p; } x:} void insertion_info() { struct dll *newnode, *p; int a, s, m = 0; newnode = (struct dll *) malloc(sizeof(struct dll)); c++; newnode->info = s; p = start; if (start == NULL) printf("nlinked list is empty"); else { printf("nenter the information to be inserted"); scanf("%d", &s); printf("nenter the search data"); scanf("%d", &a); newnode->info = s; while (p->info != a) { if (p == NULL) { printf("ndata is not present in the list"); m = 1; break; } p = p->next; } if (m == 0) { if (p->next == NULL) { newnode->next = p->next; p->next = newnode; newnode->prev = p; } else { newnode->next = p->next; p->next->prev = newnode; p->next = newnode; newnode->prev = p; } } } } void split() { struct dll *p; int n, i; p = start; printf("nenter the node no. after which the linked list is to be splitted"); scanf("%d", &n); if (start == NULL) printf("nthe linked list is empty"); else { printf("nthe splitted linked lists are->"); for (i = 1; i <= n; i++) { printf("%dt", p->info); p = p->next; } p->prev = NULL; printf("nt"); while (p != NULL) { printf("t%d", p->info); p = p->next; } } } void deletion_before() { struct dll *p; int j, i; p = start; if (start != NULL) { printf("nenter the no. of node before which the node has to be deleted"); scanf("%d", &j); c--; } if (start == NULL) printf("nlinked list is empty"); else if (j == 1) printf("nwrong choice"); else if (j == 2) { start = p->next; free(p); } else { for (i = 1; i < j - 1; i++) p = p->next; p->prev->next = p->next; p->next->prev = p->prev; free(p); } } void deletion_after() { struct dll *p; int y, i; p = start; if (start != NULL) { printf("nenter the no. of node after which the node has to be deleted"); scanf("%d", &y); } if (start == NULL) printf("nlinked list is empty"); else { for (i = 1; i <= y; i++) p = p->next; if (p == NULL) printf("nwrong choice"); else if (p->next == NULL) { p->prev->next = p->next; free(p); } else { p->next->prev = p->prev; p->prev->next = p->next; free(p); } c--; } } void deletion_info() { int s, w = 0; struct dll *p; p = start; if (p == NULL) printf("nlinked list is empty"); else { printf("nenter the information to be deleted"); scanf("%d", &s); while (p->info != s) { if (p == NULL) { printf("ninformation is not present in the linked list"); w = 1; break; } p = p->next; } if (p == start) { start = p->next; free(p); } else if (p->next == NULL && w == 0) { p->prev->next = p->next; free(p); } else if (w == 0) { p->next->prev = p->prev; p->prev->next = p->next; free(p); } c--; } } void main() { int ch; clrscr(); while (1) { printf("nenter 1. for creation of doubly link list node"); printf("nenter 2. for display of linked list"); printf("nenter 3. for insertion before a node"); printf("nenter 4. for insertion after a node"); printf("nenter 5. for insertion according to information of the node"); printf("nenter 6. for splitting the linked list"); printf("nenter 7. for deletion before a node"); printf("nenter 8. for deletion after a node"); printf("nenter 9. for deletion according to information"); printf("nenter 10. for exit"); printf("nenter your choice"); fflush(stdin); scanf("%d", &ch); switch (ch) { case 1: create_node(); break; case 2: display(); break; case 3: insertion_before(); break; case 4: insertion_after(); break; case 5: insertion_info(); break; case 6: split(); break; case 7: deletion_before(); break; case 8: deletion_after(); break; case 9: deletion_info(); break; case 10: exit(0); default: printf("nentered a wrong choice"); } } getch(); }