/* 7 . Write a Program to Implement a Single Link List using following Operation: 1. Insert 2. Insend 3. Insord 4. Delete 5. Dispaly 6. Search 7. Exit */ #include <stdio.h> #include <conio.h> #include <alloc.h> #include <dos.h> #include <process.h> struct node { int no; struct node *next; } *head, *p, *temp, *q; void newlist(); void display(); void inbefore(); void inafter(); void inmid(); void delfirst(); void dellast(); void delmid(); void ascending(); void descending(); void search(); void invalid(); void tanya(); void main() { int ch; clrscr(); while (ch != 6) { again: display(); printf("\n1.Create New List\n2.Insert\n3.Delete Node\n4.Sorting\n5.Search\n6.exit\n"); printf("\nEnter Your Choice : "); scanf("%d", &ch); if (ch > 6 || ch < 0) { invalid(); goto again; } switch (ch) { case 1: newlist(); break; case 2: int a = 0; clrscr(); while (a != 4) { again2: display(); printf("\n0.Display\n1.Insert Before Node\n2.Insert After Node\n3.Insert Some Where else\n4.Back\n"); printf("\nEnter Your Choice : "); scanf("%d", &a); if (a > 4 || a < 0) { invalid(); goto again2; } switch (a) { case 0: tanya(); break; case 1: inbefore(); break; case 2: inafter(); break; case 3: inmid(); break; case 4: goto end; } } end: clrscr(); break; case 3: int b = 0; clrscr(); while (b != 4) { again3: display(); printf("\n1.Delete First Node\n2.Delete Last Node\n3.Delete Other Node\n4.Back\n"); printf("\nEnter Your Choice : "); scanf("%d", &b); if (b > 4 || b < 0) { invalid(); goto again3; } switch (b) { case 1: delfirst(); break; case 2: dellast(); break; case 3: delmid(); break; case 4: goto end2; } } end2: clrscr(); break; case 4: int c = 0; clrscr(); while (c != 3) { again4: display(); printf("\n1.Ascending Order\n2.Descending Order\n3.Back\n"); printf("\nEnter Yoyr Choice : "); scanf("%d", &c); if (c > 3 || c < 0) { invalid(); goto again3; } switch (c) { case 1: ascending(); break; case 2: descending(); break; case 3: goto end3; } } end3: clrscr(); break; case 5: clrscr(); search(); break; case 6: exit(0); } } getch(); } void newlist() { int sel; int i, n, l; clrscr(); head = (struct node *)malloc(sizeof(struct node *)); printf("\nEnter Value For Head : "); scanf("%d", &n); head->no = n; head->next = 0; p = head; while (sel != 1) { i = 1; printf("\nPress 1 for exit or 2 for continue : "); scanf("%d", &sel); if (sel == 1) { break; } else { temp = (struct node *)malloc(sizeof(struct node)); printf("\nEnter Number [%d] : ", i + 1); scanf("%d", &n); temp->no = n; temp->next = 0; p->next = temp; p = p->next; i++; } } clrscr(); } void display() { p = head; printf("\n\t\t\t\tCreated List"); printf("\n\t\t\t\t------------"); printf("\n"); if (p == 0) { printf("\n\t* --> List Not Created\n"); goto e; } while (p != 0) { printf(" %d -> ", p->no); p = p->next; } printf("end Of list"); printf("\n"); e: } void inbefore() { int no; temp = (struct node *)malloc(sizeof(struct node *)); printf("\nEnter Value : "); scanf("%d", &no); temp->no = no; temp->next = head; head = temp; clrscr(); } void inafter() { int no; p = head; temp = (struct node *)malloc(sizeof(struct node *)); printf("\nEnter Value : "); scanf("%d", &no); clrscr(); temp->no = no; temp->next = 0; while (p->next != 0) { p = p->next; } p->next = temp; clrscr(); } void inmid() { int no, x, f = 0; printf("\nEnter The Location Where You Want To put Your New Number : "); scanf("%d", &x); p = head; temp = (struct node *)malloc(sizeof(struct node *)); printf("\nEnter New Number : "); scanf("%d", &no); clrscr(); temp->no = no; while (p != 0) { if (x == p->no) { f = 1; break; } p = p->next; } if (f == 1) { temp->next = p->next; p->next = temp; } clrscr(); } void delfirst() { clrscr(); p = head->next; free(head); head = p; clrscr(); } void dellast() { clrscr(); p = head; q = p->next; while (q->next != 0) { p = p->next; q = q->next; } p->next = 0; free(q); clrscr(); } void delmid() { int x2, f2 = 0; clrscr(); p = head; q = p->next; printf("\nEnter The No Which You Want To Delete : "); scanf("%d", &x2); clrscr(); while (q != 0) { if (x2 == q->no) { f2 = 1; break; } p = p->next; q = q->next; } if (f2 == 1) { p->next = q->next; } clrscr(); } void search() { int f3 = 0, find_no; printf("\n\nWhich No You Want To Search : "); scanf("%d", &find_no); p = head; while (p != 0) { if (p->no == find_no) { f3 = 1; break; } p = p->next; } if (f3 == 1) { tanya(); printf("\nYup.. The Number Is In the List"); getch(); clrscr(); } else { tanya(); printf("\nNup.. The Number Is Not the List"); getch(); clrscr(); } clrscr(); } void ascending() { int z; p = head; for (p = head; p != 0; p = p->next) { for (q = p->next; q != NULL; q = q->next) { if (p->no > q->no) { z = p->no; p->no = q->no; q->no = z; } } } tanya(); printf("\nData are Sorted In Ascending Order"); getch(); clrscr(); } void descending() { int z; p = head; for (p = head; p != 0; p = p->next) { for (q = p->next; q != NULL; q = q->next) { if (p->no < q->no) { z = p->no; p->no = q->no; q->no = z; } } } tanya(); printf("\nData are Sorted In Descending Order"); getch(); clrscr(); } void invalid() { printf("Invalid Choice"); delay(5000); clrscr(); } void tanya() { clrscr(); sound(1000); printf("Please Wait.."); delay(1000); nosound(); clrscr(); } /* OUTPUT Created List ------------ * --> List Not Created 1.Create New List 2.Insert 3.Delete Node 4.Sorting 5.Search 6.exit Enter Your Choice :1 Enter Value For Head : 59 Press 1 for exit or 2 for continue : 2 Enter Number [2] : 32 Press 1 for exit or 2 for continue : 2 Enter Number [2] : 64 Press 1 for exit or 2 for continue : 2 Enter Number [2] : 99 Press 1 for exit or 2 for continue : 2 Enter Number [2] : 47 Press 1 for exit or 2 for continue : 2 Enter Number [2] : 21 Press 1 for exit or 2 for continue : 1 Created List ------------ 59 -> 32 -> 64 -> 99 -> 47 -> 21 -> end Of list 1.Create New List 2.Insert 3.Delete Node 4.Sorting 5.Search 6.exit Enter Your Choice :2 Created List ------------ 59 -> 32 -> 64 -> 99 -> 47 -> 21 -> end Of list 0.Display 1.Insert Before Node 2.Insert After Node 3.Insert Some Where else 4.Back Enter Your Choice :0 59 -> 32 -> 64 -> 99 -> 47 -> 21 -> end Of list 0.Display 1.Insert Before Node 2.Insert After Node 3.Insert Some Where else 4.Back Enter Your Choice :1 Enter Value : 99 Created List ------------ 99 -> 59 -> 32 -> 64 -> 99 -> 47 -> 21 -> end Of list 0.Display 1.Insert Before Node 2.Insert After Node 3.Insert Some Where else 4.Back Enter Your Choice :2 Created List ------------ 99 -> 59 -> 32 -> 64 -> 99 -> 47 -> 21 -> end Of list 0.Display 1.Insert Before Node 2.Insert After Node 3.Insert Some Where else 4.Back Enter Your Choice : 2 Enter Value : 45 Created List ------------ 99 -> 59 -> 32 -> 64 -> 99 -> 47 -> 21 -> 45 -> end Of list 0.Display 1.Insert Before Node 2.Insert After Node 3.Insert Some Where else 4.Back Enter Your Choice :3 Created List ------------ 99 -> 59 -> 32 -> 64 -> 99 -> 47 -> 21 -> 45 -> end Of list 0.Display 1.Insert Before Node 2.Insert After Node 3.Insert Some Where else 4.Back Enter Your Choice : 3 Enter The Location Where You Want To put Your New Number : 32 Enter New Number : 11 Created List ------------ 99 -> 59 -> 32 -> 11 -> 64 -> 99 -> 47 -> 21 -> 45 -> end Of list 0.Display 1.Insert Before Node 2.Insert After Node 3.Insert Some Where else 4.Back Enter Your Choice : 4 Created List ------------ 99 -> 59 -> 32 -> 11 -> 64 -> 99 -> 47 -> 21 -> 45 -> end Of list 1.Create New List 2.Insert 3.Delete Node 4.Sorting 5.Search 6.exit Enter Your Choice :3 Created List ------------ 99 -> 59 -> 32 -> 11 -> 64 -> 99 -> 47 -> 21 -> 45 -> end Of list 1.Delete First Node 2.Delete Last Node 3.Delete Other Node 4.Back Enter Your Choice :1 Created List ------------ 59 -> 32 -> 11 -> 64 -> 99 -> 47 -> 21 -> 45 -> end Of list 1.Delete First Node 2.Delete Last Node 3.Delete Other Node 4.Back Enter Your Choice :2 59 -> 32 -> 11 -> 64 -> 99 -> 47 -> 21 -> end Of list 1.Delete First Node 2.Delete Last Node 3.Delete Other Node 4.Back Enter Your Choice :3 enter which node you want to deleate : 11 Created List ------------ 59 -> 32 -> 64 -> 99 -> 47 -> 21 -> end Of list 1.Delete First Node 2.Delete Last Node 3.Delete Other Node 4.Back Enter Your Choice :4 Created List ------------ 59 -> 32 -> 64 -> 99 -> 47 -> 21 -> end Of list 1.Create New List 2.Insert 3.Delete Node 4.Sorting 5.Search 6.exit Enter Your Choice :4 Created List ------------ 59 -> 32 -> 64 -> 99 -> 47 -> 21 -> end Of list 1.Ascending Order 2.Descending Order 3.Back Enter Your Choice :1 Created List ------------ 21 -> 32 -> 47 -> 59 -> 64 -> 99 -> end Of list 1.Ascending Order 2.Descending Order 3.Back Enter Your Choice :2 Created List ------------ 99 -> 64 -> 59 -> 47 -> 32 -> 21 -> end Of list 1.Ascending Order 2.Descending Order 3.Back Enter Your Choice :3 Created List ------------ 99 -> 64 -> 59 -> 47 -> 32 -> 21 -> end Of list 1.Create New List 2.Insert 3.Delete Node 4.Sorting 5.Search 6.exit Enter Your Choice :5 Which No You Want To Search : 47 Yup.. The Number Is In the List 99 -> 64 -> 59 -> 47 -> 32 -> 21 -> end Of list 1.Create New List 2.Insert 3.Delete Node 4.Sorting 5.Search 6.exit Enter Your Choice :5 Which No You Want To Search : 55 */
0 Comments
Do not make spam in comment box.