In this topic, you will learn about, C Program To Demo Single Link List Operations Version 2.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 |
/* --------------------------------------------------------- */ /* Program To Demo. Single Link List Operations. Version - 2 */ /* --------------------------------------------------------- */ //---Including Required Header Files. #include<alloc.h> #include<ctype.h> #include<conio.h> #include<stdio.h> #include<stdlib.h> //--- Declaring Struccture Of Link List Node. struct LinkList { int info; struct LinkList *NEXT; }; //--- Method To Display Message With Specified Argument. void Pause(char *msg) { printf("\n\n%s", msg); printf("\nPress Any Key To Continue..."); fflush(stdin); getch(); } //--- Searching An Node. LinkList * SearchNode(LinkList **H, int src) { LinkList *curNode = *H; while(curNode != NULL) { if(curNode->info == src) break; curNode = curNode->NEXT; } return curNode; } //--- Insert Node At Begin. void AddBegin(LinkList **H, int x) { LinkList *newNode = (LinkList *) malloc(sizeof(LinkList)); newNode->info = x; newNode->NEXT = *H; *H = newNode; } //--- Insert Node At End. void AddEnd(LinkList **H, int x) { LinkList *newNode = (LinkList *) malloc(sizeof(LinkList)); newNode->info = x; if(*H == NULL) { newNode->NEXT = *H; *H = newNode; } else { LinkList *curNode = *H; while(curNode->NEXT != NULL) curNode = curNode->NEXT; newNode->NEXT = curNode->NEXT; curNode->NEXT = newNode; } } //--- Insert Node After Any Specified Node. int AddAfter(LinkList **H, int src, int x) { LinkList *curNode = SearchNode(H, src); if(curNode != NULL) { LinkList *newNode = (LinkList *) malloc(sizeof(LinkList)); newNode->info = x; newNode->NEXT = curNode->NEXT; curNode->NEXT = newNode; return 1; } else return 0; } //--- Traversal Of Link List. void ViewAll(LinkList **H) { clrscr(); printf("\nTRAVERSAL OF LINK LIST:"); printf("\n~~~~~~~~~~~~~~~~~~~~~~"); LinkList *Node = *H; printf("\nHEAD = %6u", *H); printf("\n/--------------------------\\"); printf("\n| Addr | Info | NEXT |"); printf("\n|--------|--------|--------|"); while(Node != NULL) { printf("\n| %-6u | %6d | %-6u |", Node, Node->info, Node->NEXT); Node = Node->NEXT; } printf("\n\\--------------------------/"); Pause(""); } //--- Delete Begin Node. void DeleteBegin(LinkList **H) { if(*H == NULL) return; LinkList *curNode = *H; *H = curNode->NEXT; free(curNode); } //--- Delete End Node. void DeleteEnd(LinkList **H) { if(*H == NULL) return; LinkList *curNode = *H; if(curNode->NEXT == NULL) { *H = curNode->NEXT; free(curNode); } else { while(curNode->NEXT->NEXT != NULL) curNode = curNode->NEXT; LinkList *Node = curNode->NEXT; curNode->NEXT = curNode->NEXT->NEXT; free(Node); } } //--- Delete After Specified Node. void DeleteAfter(LinkList **H, int src) { if(*H == NULL) return; LinkList *curNode; curNode = SearchNode(H, src); if(curNode == NULL || curNode->NEXT == NULL) return; LinkList *Node = curNode->NEXT; curNode->NEXT = curNode->NEXT->NEXT; free(Node); } //--- Delete All Nodes. void DeleteAll(LinkList **H) { if(*H == NULL) return; LinkList *curNode = *H; while(*H != NULL) { *H = curNode->NEXT; free(curNode); curNode = *H; } free(curNode); } //--- Main Program Body To Show Menu Driven Choice For Operations. void main(void) { int flag, src, info; LinkList *HEAD = NULL; char choice; do { clrscr(); printf("\n/--------------------------------\\"); printf("\n| SINGLE LINK LIST MENU |"); printf("\n|--------------------------------|"); printf("\n| 1. Insert Node At Begin. |"); printf("\n| 2. Insert Node At End. |"); printf("\n| 3. Insert Node After Any Node. |"); printf("\n| 4. Traversal Of Link List. |"); printf("\n| 5. Search Any Info In List. |"); printf("\n| 6. Delete Begin Node. |"); printf("\n| 7. Delete End Node. |"); printf("\n| 8. Delete After Any Node. |"); printf("\n| 9. Delete All Nodes. |"); printf("\n| 0. Exit / Quit. |"); printf("\n|--------------------------------|"); printf("\n| * Enter Your Choice (0-9): |"); printf("\n\\--------------------------------/"); gotoxy(30, 16); fflush(stdin); choice = getche(); gotoxy(1, 19); switch(choice) { case '1': printf("Enter Info.: "); scanf("%d", &info); AddBegin(&HEAD, info); break; case '2': printf("Enter Info.: "); scanf("%d", &info); AddEnd(&HEAD, info); break; case '3': printf("Enter Info. After Which Node Is To Be Created: "); scanf("%d", &src); printf("Enter Info. For New Node: "); scanf("%d", &info); flag = AddAfter(&HEAD, src, info); if(flag == 0) Pause("Either List Is Empty Or Node Does Not Exist, Try Again."); break; case '4': ViewAll(&HEAD); break; case '5': printf("Enter Info. To Be Searched: "); scanf("%d", &src); LinkList *curNode = SearchNode(&HEAD, src); clrscr(); printf("\nHEAD = %6u", HEAD); printf("\n/--------------------------\\"); printf("\n| Addr | Info | NEXT |"); printf("\n|--------|--------|--------|"); printf("\n| %-6u | %6d | %-6u |", curNode, curNode->info, curNode->NEXT); printf("\n\\--------------------------/"); Pause(""); break; case '6': DeleteBegin(&HEAD); break; case '7': DeleteEnd(&HEAD); break; case '8': printf("Enter Info. After Which Node Is To Be Deleted: "); scanf("%d", &src); DeleteAfter(&HEAD, src); break; case '9': DeleteAll(&HEAD); break; case '0': Pause("Thankyou For Using This Demo Version. TATA, BYE-BYE."); break; default: Pause("INVALID CHOICE, Try Between A And M Only."); } }while(choice != '0'); } //--- End Of File (EOF). |
If you found any mistake in the above C Program To Demo Single Link List Operations Version 2. then please mention it by commenting below.