Commits

Anonymous committed 1acd662

add demo.c with vector and list demos

Comments (0)

Files changed (1)

+#include <stdio.h>
+#include "vector.h"
+#include "list.h"
+
+vector_def(Stack, int) // use vector as stack
+list_def  (Listn, int) // use list node as list
+
+int main(void)
+{
+    /* Vector demo */
+    Stack *s = Stack_new(4); // minimum capacity 4
+    int   i = 20;
+    while (i--)
+        Stack_push(s, i);
+    while (s->size)
+        printf("%d\n", Stack_pop(s));
+    Stack_del(s);
+
+    /* List demo */
+    // dummy node for use as head, not strictly necessary
+    // but I like that way it works out better.
+    Listn *head = Listn_new(-1);
+    i = 5;
+    while (i--)
+        Listn_insert(head, i);
+    for (Listn *p = head->next; p != head; p = p->next)
+        printf("%d\n", p->data);
+    Listn_del(head->prev);
+    for (Listn *p = head->prev; p != head; p = p->prev)
+        printf("%d\n", p->data);
+    Listn_del_all(head);
+
+    return 0;
+}