Skip to content

Instantly share code, notes, and snippets.

@reagent
Last active December 13, 2015 22:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save reagent/4988867 to your computer and use it in GitHub Desktop.
Save reagent/4988867 to your computer and use it in GitHub Desktop.
Just puttin' it out there
#include <stdlib.h>
#include <stdio.h>
typedef struct Node {
int value;
struct Node *next;
} Node;
typedef struct List {
Node *head;
int count;
} List;
Node *
node_create(int value)
{
Node *node = calloc(1, sizeof(Node));
node->value = value;
node->next = NULL;
return node;
}
List *
list_create()
{
List *list = calloc(1, sizeof(List));
list->head = NULL;
list->count = 0;
return list;
}
Node *
list_push(List *list, int value)
{
Node *cur = NULL,
*tail = NULL,
*new = node_create(value);
if (!list->head) {
// No elements in the list, just make this one the head.
list->head = new;
} else {
for (cur = list->head; cur != NULL; cur = cur->next) {
tail = cur;
}
tail->next = new;
}
list->count++;
return new;
}
void
list_remove_value(List *list, int value)
{
Node *cur = list->head,
*prev = cur;
while (cur != NULL) {
if (cur->value == value) {
if (list->head == cur) {
list->head = cur->next;
} else {
prev->next = cur->next;
}
free(cur);
return;
}
prev = cur;
cur = cur->next;
}
}
int
list_sum(List *list)
{
int sum = 0;
Node *cur = NULL;
for (cur = list->head; cur != NULL; cur = cur->next) {
sum = sum + cur->value;
}
return sum;
}
void
list_free(List *list)
{
Node *current,
*next = list->head;
while (next != NULL) {
current = next;
next = current->next;
free(current);
};
free(list);
}
void
list_print(List *list)
{
Node *cur = NULL;
int i = 0;
fprintf(stderr, "List: [");
for (cur = list->head; cur != NULL; cur = cur->next) {
fprintf(stderr, "%s%d", ((i == 0) ? "" : ", "), cur->value);
i++;
}
fprintf(stderr, "]\n");
}
void
list_node_swap(List *list, Node *prev)
{
Node *cur = NULL,
*next = NULL;
if (!prev) {
cur = list->head;
} else {
cur = prev->next;
}
if (cur) { next = cur->next; }
if (cur && next) {
cur->next = next->next;
next->next = cur;
}
if (!prev) {
if (cur && next) { list->head = next; }
} else {
if (cur && next) { prev->next = next; }
}
}
void
list_sort(List *list)
{
Node *prev = NULL,
*cur = NULL,
*next = NULL;
int swapped = 0;
do {
swapped = 0;
for (cur = list->head; cur != NULL;) {
next = cur->next;
if (next && next->value < cur->value) {
list_node_swap(list, prev);
swapped = 1;
} else {
cur = cur->next;
}
if (prev) {
prev = prev->next;
} else {
prev = list->head;
}
}
} while (swapped);
}
int
main()
{
List *list = list_create();
int i = 0;
for (i = 10; i > 0; i--) { list_push(list, i); }
for (i = 11; i <=20; i++) { list_push(list, i); }
list_print(list);
list_sort(list);
list_print(list);
list_free(list);
return 0;
}
CFLAGS=-g -Wall -Wextra
all: main
clean:
rm -rf main *.dSYM
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment