Wednesday, December 7, 2011

Some C because I'm bored

So it was C programming day in IRC. I wrote a linked list:



#ifndef _LIST
#define _LIST
// List of things

#include
#include
#define null NULL
typedef struct Node Node;
typedef struct List List;
struct Node{
// A node in the list
Node* next;
Node* prev;
void* val;
};

struct List {
// A list, has a front and an end
// Maintains a pointer into the list
Node* current;
Node* front;
Node* end;
size_t size;
};

Node* append(List *list, void* val) {
// Adds an element to the end of the list
Node* new = malloc(sizeof(Node));
list->end->prev->next = new;
new->val = val;
new->next = list->end;
new->prev = list->end->prev;
list->end->prev = new;
list->size++;
return new;
}

Node* preprend(List* list, void* val) {
// Adds an element before the list
Node* new = malloc(sizeof(Node));
list->front->next->prev = new;
new->val = val;
new->prev = list->front;
new->next = list->front->next;
list->front->next = new;
list->size++;
return new;
}

Node* insert_after_current(List* list, void* val) {
Node* new = malloc(sizeof(Node));
new->val = val;
new->next = list->current->next;
new->prev = list->current;
list->current->next->prev = new;
list->current->next = new;
return new;
}

List* delete_current(List* list) {
// Removes the current node in the list
Node* t1 = list->current->prev;
Node* t2 = list->current->next;
free(list->current);
t1->next = t2;
t2->prev = t1;
list->size--;
return list;
}

void* pop(List* list) {
// Pops the element off the end of the list
void* x = list->end->val;
Node* temp = list->end->prev;
free(list->end);
list->end = temp;
list->size--;
return x;
}

Node* get_val(List* list) {
// Returns the current node
if(list->current->val == NULL)
list->current = list->current->next;
return list->current;
}

int is_front(List* list) {
// Tests if current is at front of list
return (list->current->prev == list->front);
}

int is_end(List* list) {
// Tests if current is at end of list
return (list->current->next == list->end);
}

Node* to_front(List* list) {
// Set current to front of list
return (list->current = list->front);
}

Node* to_end(List* list) {
// For reverse iteration
return (list->current = list->end);
}
Node* next(List* list) {
// Increments pointer to next value in list
// will not go off end of list
if(list->current->next)
list->current = list->current->next;
return list->current;
}
Node* prev(List* list) {
// Set pointer to previous value in list
// will not go off end of list
if(list->current->prev)
list->current = list->current->prev;
return list->current;
}

size_t count(List* list) {
return list->size;
}

List* init_list() {
List* list = malloc(sizeof(List));
list->size = 0;
list->front = malloc(sizeof(Node));
list->end = malloc(sizeof(Node));
int x = 0;
list->front->val = list->end->val = NULL;
list->front->prev = NULL;
list->front->next = list->end;
list->end->next = NULL;
list->end->prev = list->front;
list->current = list->front;
return list;
}

#endif


And here is the code to use it:


#include "list.c"
#include
#include
int main() {
List* l = init_list();
int nums[] = {1,2,3,4,5,5,6};
int i;
for(i = 0;i < 7;i++) {
printf("Adding to list: %d\n",nums[i]);
append(l,(void*) &(nums[i]));
}
printf("List size: %d\n",count(l));
while(!is_end(l)) {
next(l);
printf("%d\n",*(int*)(get_val(l)->val));
}
return 0;
}

No comments:

Post a Comment