add trivial linked list example
This commit is contained in:
parent
727938dbcf
commit
f6ef53c59e
2 changed files with 49 additions and 2 deletions
7
Makefile
7
Makefile
|
@ -1,7 +1,10 @@
|
||||||
CFLAGS=-std=c99
|
CFLAGS=-std=c99 -Wall -g
|
||||||
INDENTOPTS=-kr --no-tabs --braces-on-func-def-line --indent-level2
|
INDENTOPTS=-kr --no-tabs --braces-on-func-def-line --indent-level2
|
||||||
|
|
||||||
all: approximate-pi
|
all: approximate-pi linked-list
|
||||||
|
|
||||||
|
clean:
|
||||||
|
rm -f approximate-pi linked-list
|
||||||
|
|
||||||
.PHONY: indent
|
.PHONY: indent
|
||||||
indent:
|
indent:
|
||||||
|
|
44
linked-list.c
Normal file
44
linked-list.c
Normal file
|
@ -0,0 +1,44 @@
|
||||||
|
#include <stdio.h>
|
||||||
|
#include "assert.h"
|
||||||
|
|
||||||
|
struct list_node {
|
||||||
|
int data;
|
||||||
|
struct list_node *next;
|
||||||
|
};
|
||||||
|
typedef struct list_node list_node_t;
|
||||||
|
|
||||||
|
int list_length(list_node_t * list) {
|
||||||
|
list_node_t *cur;
|
||||||
|
int count = 0;
|
||||||
|
|
||||||
|
cur = list;
|
||||||
|
while (cur != NULL) {
|
||||||
|
count++;
|
||||||
|
cur = cur->next;
|
||||||
|
}
|
||||||
|
|
||||||
|
return count;
|
||||||
|
}
|
||||||
|
|
||||||
|
void list_print(list_node_t * list) {
|
||||||
|
list_node_t *cur;
|
||||||
|
|
||||||
|
cur = list;
|
||||||
|
while (cur != NULL) {
|
||||||
|
printf("%d ", cur->data);
|
||||||
|
cur = cur->next;
|
||||||
|
}
|
||||||
|
printf("\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(int argc, char *argv[]) {
|
||||||
|
list_node_t *emptylist = NULL;
|
||||||
|
assert(list_length(emptylist) == 0);
|
||||||
|
list_print(emptylist);
|
||||||
|
printf("list_length = %d\n", list_length(emptylist));
|
||||||
|
|
||||||
|
list_node_t foo = { 1, NULL };
|
||||||
|
assert(list_length(&foo) == 1);
|
||||||
|
list_print(&foo);
|
||||||
|
printf("list_length = %d\n", list_length(&foo));
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue