summaryrefslogtreecommitdiff
path: root/que.c
diff options
context:
space:
mode:
authorrodri <rgl@antares-labs.eu>2020-02-22 09:56:09 +0000
committerrodri <rgl@antares-labs.eu>2020-02-22 09:56:09 +0000
commita39951d8f69209cfea2b7051832b851914e662ef (patch)
treee4cd1c32e5d6f531b523f6fda558cc3a5f603547 /que.c
downloadbrokentoys-a39951d8f69209cfea2b7051832b851914e662ef.tar.gz
brokentoys-a39951d8f69209cfea2b7051832b851914e662ef.tar.bz2
brokentoys-a39951d8f69209cfea2b7051832b851914e662ef.zip
now version controlled.
Diffstat (limited to 'que.c')
-rw-r--r--que.c70
1 files changed, 70 insertions, 0 deletions
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 <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);
+}