Last active
August 29, 2015 14:04
-
-
Save mfurquimdev/b89494ee0f4aff6a9e41 to your computer and use it in GitHub Desktop.
A linked list implemented in c
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 <assert.h> | |
#include <stdlib.h> | |
typedef struct Node_t | |
{ | |
int data; | |
struct Node_t* next; | |
}node_t; | |
typedef struct List_t | |
{ | |
node_t* head; | |
node_t* tail; | |
}list_t; | |
void list_init(list_t*); | |
void list_print(list_t*); | |
void list_free(list_t*); | |
void list_populate(list_t*); | |
void list_insert(list_t*,int); | |
int list_remove(list_t*); | |
int list_empty(list_t*); | |
int main (void) | |
{ | |
list_t list; | |
list_init (&list); | |
printf( list_empty(&list) ? "Empty\n" : "Not Empty\n"); | |
list_populate (&list); | |
int i; | |
for (i = 0; i < 6; ++i) | |
{ | |
printf("Element removed: %d\n", list_remove(&list)); | |
printf( list_empty(&list) ? "Empty\n" : "Not Empty\n"); | |
} | |
list_populate (&list); | |
printf("Element removed: %d\n", list_remove(&list)); | |
printf( list_empty(&list) ? "Empty\n" : "Not Empty\n"); | |
list_print (&list); | |
list_free (&list); | |
return 0; | |
} | |
int list_empty(list_t* list) | |
{ | |
return list->head == NULL; | |
} | |
int list_remove (list_t* list) | |
{ | |
node_t* temp; | |
int data; | |
assert (list != NULL); | |
if (list->head == NULL) | |
{ | |
fprintf(stderr, "Trying to remove a node_t from an empty list!\n"); | |
return -1; | |
} | |
temp = list->head; | |
list->head = list->head->next; | |
data = temp->data; | |
free (temp); | |
return data; | |
} | |
void list_insert (list_t* list, int data) | |
{ | |
node_t* newNode; | |
char errStr [44]; | |
assert (list != NULL); | |
newNode = (node_t*) malloc (sizeof(node_t)); | |
if (newNode == NULL) | |
{ | |
sprintf (errStr, "malloc failure in list_insert(list*,%d):", data); | |
perror(errStr); | |
exit (0); | |
} | |
newNode->data = data; | |
newNode->next = NULL; | |
if (list->head == NULL) | |
{ | |
list->head = newNode; | |
list->tail = newNode; | |
} | |
else | |
{ | |
list->tail->next = newNode; | |
list->tail = newNode; | |
} | |
return ; | |
} | |
void list_populate (list_t* list) | |
{ | |
assert (list != NULL); | |
printf("Populating List\n"); | |
list_insert(list,4); | |
list_insert(list,3); | |
list_insert(list,2); | |
list_insert(list,1); | |
list_insert(list,0); | |
return ; | |
} | |
void list_init (list_t* list) | |
{ | |
list->head = NULL; | |
list->tail = NULL; | |
return ; | |
} | |
void list_print (list_t* list) | |
{ | |
node_t* next; | |
assert (list != NULL); | |
next = list->head; | |
while (next != NULL) | |
{ | |
printf("%d\n", next->data); | |
next = next->next; | |
} | |
return ; | |
} | |
void list_free (list_t* list) | |
{ | |
node_t* next; | |
assert (list != NULL); | |
next = list->head; | |
while (next != NULL) | |
{ | |
list->head = list->head->next; | |
free (next); | |
next = list->head; | |
} | |
return ; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment