Last active
December 14, 2021 14:14
-
-
Save J16N/86893a6994c90f5bea83945067bf2772 to your computer and use it in GitHub Desktop.
Every possible linked list operations except sorting.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <stdio.h> | |
| #include <stdlib.h> | |
| #include <stdbool.h> | |
| typedef struct node { | |
| int data; | |
| struct node *next; | |
| } NODE; | |
| typedef struct lList { | |
| int nodes; | |
| NODE *head; | |
| } List; | |
| void menu(void); | |
| void flush(FILE *); | |
| int len(List *); | |
| void reverseL(List *); | |
| List *initL(void); | |
| void destroy(List *); | |
| void display(List *); | |
| void displayR(List *); | |
| void displayRec(List *); | |
| void displayN(NODE *); | |
| int *search(List *, int); | |
| void append(List *, int); | |
| void prepend(List *, int); | |
| void insert(List *, int, int); | |
| void del(List *, int); | |
| void del_end(List *); | |
| void del_start(List *); | |
| bool isLooped(List *); | |
| int main(void) | |
| { | |
| menu(); | |
| bool quit = false; | |
| List *myList = initL(); | |
| int *result; | |
| int choice, data, pos; | |
| while(!quit) { | |
| printf("\nChoice: "); | |
| scanf("%d", &choice); | |
| switch (choice) { | |
| case 0: | |
| quit = true; | |
| break; | |
| case 1: | |
| display(myList); | |
| break; | |
| case 2: | |
| printf("Data: "); | |
| scanf("%d", &data); | |
| prepend(myList, data); | |
| break; | |
| case 3: | |
| printf("Data: "); | |
| scanf("%d", &data); | |
| append(myList, data); | |
| break; | |
| case 4: | |
| printf("Data: "); | |
| scanf("%d", &data); | |
| printf("Position: "); | |
| scanf("%d", &pos); | |
| insert(myList, data, pos); | |
| break; | |
| case 5: | |
| del_start(myList); | |
| break; | |
| case 6: | |
| del_end(myList); | |
| break; | |
| case 7: | |
| printf("Position; "); | |
| scanf("%d", &pos); | |
| del(myList, pos); | |
| break; | |
| case 8: | |
| reverseL(myList); | |
| break; | |
| case 9: | |
| displayR(myList); | |
| break; | |
| case 10: | |
| displayRec(myList); | |
| break; | |
| case 11: | |
| printf("Length of the list: %d\n", len(myList)); | |
| break; | |
| case 13: | |
| printf("Data to be searched: "); | |
| scanf("%d", &data); | |
| result = search(myList, data); | |
| printf("%s.\n", result ? "Found" : "Not found"); | |
| break; | |
| case 14: | |
| printf("%s\n", isLooped(myList) ? "Looped" : "Not Looped"); | |
| break; | |
| case 15: | |
| printf("\n"); | |
| menu(); | |
| break; | |
| default: | |
| printf("Invalid Choice. Try Again!\n"); | |
| break; | |
| } | |
| flush(stdin); | |
| choice = -1; | |
| } | |
| destroy(myList); | |
| return 0; | |
| } | |
| void menu(void) | |
| { | |
| printf("*************************************************************************\n"); | |
| printf("************************ LINKED LIST OPERATIONS ***********************\n"); | |
| printf("*************************************************************************\n"); | |
| printf("* <01> Display all the data in the list. *\n"); | |
| printf("* <02> Prepend data in the list. *\n"); | |
| printf("* <03> Append data in the list. *\n"); | |
| printf("* <04> Insert data at any position of the list. *\n"); | |
| printf("* <05> Delete data from the beginning of the list. *\n"); | |
| printf("* <06> Delete data from the end of the list. *\n"); | |
| printf("* <07> Delete data from any position of the list. *\n"); | |
| printf("* <08> Reverse the list. *\n"); | |
| printf("* <09> Print data without reversing the list. (Non-recursive version) *\n"); | |
| printf("* <10> Print data without reversing the list. (Recursive version) *\n"); | |
| printf("* <11> Display length of the list. *\n"); | |
| printf("* <12> Sort the list. *\n"); | |
| printf("* <13> Search the list. *\n"); | |
| printf("* <14> Detect loop in the list. *\n"); | |
| printf("* <15> Display this menu. *\n"); | |
| printf("* <00> Exit! *\n"); | |
| printf("*************************************************************************\n"); | |
| } | |
| int len(List *list) | |
| { | |
| return list->nodes; | |
| } | |
| void reverseL(List *list) | |
| { | |
| NODE *prev = NULL, | |
| *next = NULL, | |
| *curr = list->head; | |
| while (curr) { | |
| next = curr->next; | |
| curr->next = prev; | |
| prev = curr; | |
| curr = next; | |
| } | |
| list->head = prev; | |
| } | |
| List *initL(void) | |
| { | |
| List *list = malloc(sizeof(List)); | |
| list->nodes = 0; | |
| list->head = NULL; | |
| return list; | |
| } | |
| void destroy(List *list) | |
| { | |
| NODE *start = list->head; | |
| while (start) { | |
| NODE *temp = start; | |
| start = start->next; | |
| free(temp); | |
| } | |
| free(list); | |
| } | |
| void display(List *list) | |
| { | |
| printf("List: "); | |
| NODE *start = list->head; | |
| while (start) { | |
| printf("%d ", start->data); | |
| start = start->next; | |
| } | |
| printf("\n"); | |
| } | |
| void displayR(List *list) | |
| { | |
| printf("list (Reversed): "); | |
| int length = len(list); | |
| for (int i = length; i > 0; --i) { | |
| NODE *node = list->head; | |
| for (int j = 1; j < i; ++j) | |
| node = node->next; | |
| printf("%d ", node->data); | |
| } | |
| printf("\n"); | |
| } | |
| void displayRec(List *list) | |
| { | |
| printf("List (Reversed): "); | |
| NODE *node = list->head; | |
| displayN(node); | |
| printf("\n"); | |
| } | |
| void displayN(NODE *node) | |
| { | |
| if (node == NULL) return; | |
| displayN(node->next); | |
| printf("%d ", node->data); | |
| } | |
| int *search(List *list, int data) | |
| { | |
| NODE *node = list->head; | |
| while (node && node->data != data) | |
| node = node->next; | |
| if (node) return &(node->data); | |
| return NULL; | |
| } | |
| void append(List *list, int data) | |
| { | |
| insert(list, data, len(list) + 1); | |
| } | |
| void prepend(List *list, int data) | |
| { | |
| insert(list, data, 1); | |
| } | |
| void insert(List *list, int data, int pos) | |
| { | |
| int length = len(list); | |
| if (pos < 1 || pos > length + 1) { | |
| printf("Invalid position.\n"); | |
| return; | |
| } | |
| list->nodes++; | |
| NODE *temp = malloc(sizeof(NODE)); | |
| temp->data = data; | |
| NODE *node = list->head; | |
| for (int i = 1; i < pos - 1; ++i) | |
| node = node->next; | |
| if (pos == 1) { | |
| temp->next = node; | |
| list->head = temp; | |
| return; | |
| } | |
| temp->next = node->next; | |
| node->next = temp; | |
| } | |
| void del_start(List *list) | |
| { | |
| del(list, 1); | |
| } | |
| void del_end(List *list) | |
| { | |
| del(list, len(list)); | |
| } | |
| void del(List *list, int pos) | |
| { | |
| int length = len(list); | |
| if (pos < 1 || pos > length) { | |
| printf("Invalid position.\n"); | |
| return; | |
| } | |
| list->nodes--; | |
| NODE *node = list->head; | |
| for (int i = 1; i < pos - 1; ++i) | |
| node = node->next; | |
| if (pos == 1) { | |
| list->head = node->next; | |
| free(node); | |
| return; | |
| } | |
| NODE *temp = node->next; | |
| node->next = temp->next; | |
| free(temp); | |
| } | |
| bool isLooped(List *list) | |
| { | |
| NODE *fp = list->head, *sp = list->head; | |
| do { | |
| sp = sp->next; | |
| fp = fp->next->next; | |
| } while (fp && fp != sp); | |
| if (fp == sp && fp) return 1; | |
| return 0; | |
| } | |
| void flush(FILE *in) | |
| { | |
| int ch; | |
| clearerr(in); | |
| do ch = getc(in); | |
| while (ch != '\n' && ch != EOF); | |
| clearerr(in); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment