From a39951d8f69209cfea2b7051832b851914e662ef Mon Sep 17 00:00:00 2001 From: rodri Date: Sat, 22 Feb 2020 09:56:09 +0000 Subject: now version controlled. --- que.c | 70 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 que.c (limited to 'que.c') diff --git a/que.c b/que.c new file mode 100644 index 0000000..9ebee71 --- /dev/null +++ b/que.c @@ -0,0 +1,70 @@ +#include +#include + +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); +} -- cgit v1.2.3