Created
September 1, 2021 16:00
-
-
Save 7h3730B/58d30244913115f9d3fedf79cf5a293e 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" | |
typedef struct node { | |
int val; | |
struct node * next; | |
} node_t; | |
int addNode(node_t * head, int val); | |
int getNodeValue(node_t * head, int position); | |
void printLinkedList(node_t * head); | |
int addNodeAtLastItem(node_t * head, int val); | |
int addNodeAsFirstItem(node_t ** head, int val); | |
int removeFirstNode(node_t ** head); | |
int removeLastNode(node_t * head); | |
int freeCompleteList(node_t ** head); | |
int getListLength(node_t * head); | |
int main() { | |
printf("Press anything to start the list\n"); | |
getchar(); | |
for (int i = 0; i < 10000; i++) { | |
node_t * head = (node_t *) malloc(sizeof(node_t)); | |
head->val = 1; | |
head->next = NULL; | |
node_t * lastNode = head; | |
for (int i = 2; i < 99999999; i++) { | |
if (addNode(lastNode, i) == 1) { | |
return 1; | |
} | |
lastNode = lastNode->next; | |
} | |
if (removeLastNode(head) == 1) { | |
return 1; | |
} | |
if (addNodeAtLastItem(head, 12) == 1) { | |
return 1; | |
} | |
removeFirstNode(&head); | |
removeFirstNode(&head); | |
if (addNodeAsFirstItem(&head, 0) == 1) { | |
return 1; | |
} | |
printf("The List is %d nodes long\n", getListLength(head)); | |
// printLinkedList(head); | |
freeCompleteList(&head); | |
printf("The List is %d nodes long\n", getListLength(head)); | |
} | |
getchar(); | |
return 0; | |
} | |
int addNode(node_t * head, int val) { | |
head->next = (node_t *) malloc(sizeof(node_t)); | |
if (head->next == NULL) { | |
return 1; | |
} | |
head->next->val = val; | |
head->next->next = NULL; | |
return 0; | |
} | |
void printLinkedList(node_t * head) { | |
node_t * current = head; | |
while (current != NULL) { | |
printf("%d ", current->val); | |
current = current->next; | |
} | |
} | |
int addNodeAtLastItem(node_t * head, int val) { | |
node_t * current = head; | |
while (current->next != NULL) { | |
current = current->next; | |
} | |
current->next = (node_t *) malloc(sizeof(node_t)); | |
if (current->next == NULL) { | |
return 1; | |
} | |
current->next->val = val; | |
current->next->next = NULL; | |
return 0; | |
} | |
int addNodeAsFirstItem(node_t ** head, int val) { | |
node_t * new_node = (node_t *) malloc(sizeof(node_t)); | |
if (new_node == NULL) { | |
return 1; | |
} | |
new_node->val = val; | |
new_node->next = *head; | |
*head = new_node; | |
return 0; | |
} | |
int removeFirstNode(node_t ** head) { | |
node_t * new_head = (*head)->next; | |
free(*head); | |
*head = new_head; | |
return 0; | |
} | |
int removeLastNode(node_t * head) { | |
node_t * current = head; | |
if (current->next == NULL) { | |
return 1; | |
} | |
while (current->next->next != NULL) { | |
current = current->next; | |
} | |
free(current->next); | |
current->next = NULL; | |
return 0; | |
} | |
int freeCompleteList(node_t ** head) { | |
node_t * current = *head; | |
node_t * temp = current; | |
while (current->next != NULL) { | |
temp = current; | |
current = current->next; | |
free(temp); | |
} | |
free(current->next); | |
*head = NULL; | |
return 0; | |
} | |
int getListLength(node_t * head) { | |
if (head == NULL) { | |
return 0; | |
} | |
node_t * current = head; | |
int length = 1; | |
while (current->next != NULL) { | |
current = current->next; | |
length++; | |
} | |
return length; | |
} | |
int getNodeValue(node_t *head, int position) { | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment