diff options
author | rodri <rgl@antares-labs.eu> | 2020-02-22 09:56:09 +0000 |
---|---|---|
committer | rodri <rgl@antares-labs.eu> | 2020-02-22 09:56:09 +0000 |
commit | a39951d8f69209cfea2b7051832b851914e662ef (patch) | |
tree | e4cd1c32e5d6f531b523f6fda558cc3a5f603547 /que.c | |
download | brokentoys-a39951d8f69209cfea2b7051832b851914e662ef.tar.gz brokentoys-a39951d8f69209cfea2b7051832b851914e662ef.tar.bz2 brokentoys-a39951d8f69209cfea2b7051832b851914e662ef.zip |
now version controlled.
Diffstat (limited to 'que.c')
-rw-r--r-- | que.c | 70 |
1 files changed, 70 insertions, 0 deletions
@@ -0,0 +1,70 @@ +#include <u.h> +#include <libc.h> + +typedef struct Node Node; +struct Node { + int n; + Node *next; +}; + +Node *head, *tail; + +void * +emalloc(ulong n) +{ + void *p; + + p = malloc(n); + if(p == nil) + sysfatal("malloc: %r"); + memset(p, 0, n); + setmalloctag(p, getcallerpc(&n)); + return p; +} + +void +put(int n) +{ + if(tail == nil){ + tail = emalloc(sizeof(Node)); + tail->n = n; + head = tail; + return; + } + tail->next = emalloc(sizeof(Node)); + tail = tail->next; + tail->n = n; +} + +int +get(void) +{ + Node *nn; + int n; + + if(head == nil) + return -1; + nn = head->next; + n = head->n; + free(head); + head = nn; + return n; +} + +int +isemtpy(void) +{ + return head == nil; +} + +void +main() +{ + int i; + + for(i = 0; i < 10; i++) + put(i*3); + while(!isemtpy()) + print("%d\n", get()); + exits(nil); +} |