Last active
November 7, 2020 18:18
-
-
Save hoefler02/3cedafab899ffb3740566b4ddd842ce6 to your computer and use it in GitHub Desktop.
This file contains 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 <time.h> | |
// November 7 2020 | |
// Data Structure Fun | |
// Linked List Implementation | |
typedef struct node { | |
int val; | |
struct node* next; | |
} node; | |
void append_node(node**, int); | |
void append_node_end(node**, int); | |
void remove_node(node**, int); | |
void display_list(node*); | |
int main(void) { | |
node* head = NULL; | |
int inp, val; | |
printf("\nSimple Linked List Implementation.\n"); | |
printf("Select An Action...\n\n"); | |
printf("1) Add to List\n"); | |
printf("2) Remove From List\n"); | |
printf("3) Display List\n\n"); | |
// add some random values to the list | |
time_t t; | |
srand((unsigned) time(&t)); | |
for (int i = 0; i < 10; i++) { | |
append_node_end(&head, rand() % 100); | |
} | |
for (;;) { // menu loop | |
printf("$ "); | |
scanf("%d", &inp); | |
switch (inp) { | |
case 1: | |
printf("What Value Would You Like To Add?\n"); | |
printf("> "); | |
scanf("%d", &val); | |
append_node_end(&head, val); | |
break; | |
case 2: | |
printf("Which Item Would You Like To Remove?\n"); | |
printf("> "); | |
scanf("%d", &val); | |
remove_node(&head, val); | |
break; | |
case 3: | |
display_list(head); | |
break; | |
default: | |
printf("Unknown Option.\n"); | |
break; | |
} | |
} | |
} | |
void append_node(node** head, int val) { | |
node* new = (node*)malloc(sizeof(node)); | |
new->val = val; | |
new->next = NULL; | |
if (*head == NULL) { | |
*head = new; | |
return; | |
} | |
new->next = *head; | |
*head = new; | |
} | |
void append_node_end(node** head, int val) { | |
node* new = (node*)malloc(sizeof(node)); | |
new->val = val; | |
new->next = NULL; | |
if (*head == NULL) { | |
*head = new; | |
return; | |
} | |
node* temp = *head; | |
while (temp->next != NULL) { | |
temp = temp->next; | |
} | |
temp->next = new; | |
} | |
void remove_node(node** head, int index) { | |
node *temp = *head, *prev = *head; | |
if (index == 0) { | |
if (temp->next != NULL) { | |
*head = temp->next; | |
free(temp); | |
return; | |
} else { | |
printf("Error: Can't Delete Entire List.\n"); | |
return; | |
} | |
} | |
for (int i = 0; i < index; i++) { | |
if (temp == NULL) { | |
printf("Error: Index Out Of Bounds!\n"); | |
return; | |
} | |
prev = temp; | |
temp = temp->next; | |
} | |
prev->next = temp->next; | |
free(temp); | |
} | |
void display_list(node* head) { | |
int counter = 0; | |
while (head != NULL) { | |
printf("Item #%d Has Value %d\n", counter, head->val); | |
head = head->next; | |
counter += 1; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment