Skip to content

Instantly share code, notes, and snippets.

@deathlyfrantic
Created May 19, 2016 03:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save deathlyfrantic/ae5e981d16de3c5a1b792cd1c623c549 to your computer and use it in GitHub Desktop.
Save deathlyfrantic/ae5e981d16de3c5a1b792cd1c623c549 to your computer and use it in GitHub Desktop.
trying to learn C so i wrote a linked list
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
typedef struct ListItem {
int value;
struct ListItem *next;
struct ListItem *prev;
} listitem_t;
typedef struct List {
listitem_t *first;
listitem_t *last;
int length;
} list_t;
list_t *create_list()
{
list_t *list = malloc(sizeof(list_t));
list->length = 0;
return list;
}
void push_list(list_t *list, int i)
{
listitem_t *item = malloc(sizeof(listitem_t));
item->value = i;
if (list->first == NULL) {
list->first = item;
list->last = item;
list->length = 1;
} else {
list->last->next = item;
item->prev = list->last;
list->last = item;
list->length++;
}
}
listitem_t pop_list(list_t *list)
{
listitem_t *item = list->last;
listitem_t *prev_item = item->prev;
list->last = prev_item;
prev_item->next = NULL;
list->length--;
item->prev = NULL;
return *item;
}
listitem_t *get_listitem(list_t *list, int idx)
{
listitem_t *item = list->first;
for (int i = 0; i < list->length; i++) {
if (i == idx) {
return item;
}
item = item->next;
}
}
void delete_listitem(list_t *list, int idx)
{
listitem_t *item = get_listitem(list, idx);
listitem_t *next = item->next;
listitem_t *prev = item->prev;
if (next != NULL) {
prev->next = next;
next->prev = prev;
} else {
prev->next = NULL;
}
free(item);
list->length--;
}
void insert_listitem(list_t *list, int idx, int i)
{
listitem_t *item = get_listitem(list, idx);
listitem_t *prev = item->prev;
listitem_t *new = malloc(sizeof(listitem_t));
new->value = i;
new->next = item;
item->prev = new;
if (prev != NULL) {
prev->next = new;
new->prev = prev;
} else {
list->first = new;
}
list->length++;
}
void free_list(list_t *list)
{
listitem_t *current = list->first;
listitem_t *next;
do {
next = current->next;
free(current);
current = next;
} while (current != NULL);
free(list);
}
void print_list(list_t *list)
{
listitem_t *item = list->first;
int randint;
printf("list length is %d, items are: %d", list->length, item->value);
while ((item = item->next) != NULL) {
printf(", %d", item->value);
}
printf("\nrandom items: ");
for (int i = 0; i < 5; i++) {
randint = rand() % list->length;
item = get_listitem(list, randint);
printf("%d -> %d, ", randint, item->value);
}
printf("\n");
}
int main()
{
list_t *my_list = create_list();
srand(time(NULL));
for (int i = 0; i < 10; i++) {
push_list(my_list, rand() % 50);
}
print_list(my_list);
pop_list(my_list);
print_list(my_list);
delete_listitem(my_list, 2);
print_list(my_list);
insert_listitem(my_list, 3, 9999);
print_list(my_list);
free_list(my_list);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment