From 711b99094921c3f68905413100564d3a9feb5d61 Mon Sep 17 00:00:00 2001 From: Elana Hashman Date: Thu, 8 Mar 2012 17:08:37 -0500 Subject: [PATCH] Added a text file for the editor race for UNIX 102. --- unix102/Makefile | 2 +- unix102/index.xml | 3 ++ unix102/queue.txt | 100 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 104 insertions(+), 1 deletion(-) create mode 100644 unix102/queue.txt diff --git a/unix102/Makefile b/unix102/Makefile index aa97189..c2a58c8 100644 --- a/unix102/Makefile +++ b/unix102/Makefile @@ -1,3 +1,3 @@ -FILES = index.html cheatsheet.pdf unix101.pdf vim_exercise.tar appender.sh tickets_email.sh +FILES = index.html cheatsheet.pdf unix101.pdf vim_exercise.tar appender.sh tickets_email.sh queue.txt RELDIR = unix102/ include ../common.mk diff --git a/unix102/index.xml b/unix102/index.xml index 201105b..a8424df 100644 --- a/unix102/index.xml +++ b/unix102/index.xml @@ -12,6 +12,9 @@ as well as some extracurricular content for you to review in your free time.

+

+ The text file for the great editor race is available here. +

To begin with the first exercise, you will need the following file. To download it, execute the following command: diff --git a/unix102/queue.txt b/unix102/queue.txt new file mode 100644 index 0000000..5f1656c --- /dev/null +++ b/unix102/queue.txt @@ -0,0 +1,100 @@ +#include +#include + +struct list { + int head; + struct list *tail; +}; + +typedef struct list list; + +typedef struct { + list *front; + list *back; +} queue; + +void list_free(list *lst) { + list *p; + + while (lst != NULL) { + p = lst; + lst = lst->tail; + free(p); + } +} + +list * cons(int val, list *lst) { + list *new = malloc(sizeof(list)); + if (new == NULL) abort(); + + new->head = val; + new->tail = lst; + + return new; +} + +void list_print(list *lst) { + list *p = lst; + + while (p != NULL) { + printf("%d ", p->head); + p = p->tail; + } +} + +queue * make_queue() { + queue *new = malloc(sizeof(queue)); + if (new == NULL) abort(); + + new->front = NULL; + new->back = NULL; + + return new; +} + +void push(int val, queue *q) { + if (q->front == NULL) { // empty queue + q->front = cons(val, NULL); + q->back = q->front; + } + + else { + list *new = cons(val, NULL); + (q->back)->tail = new; + q->back = new; + } +} + +int pop(queue *q) { + int val = (q->front)->head; + list *newhead = (q->front)->tail; + (q->front)->tail = NULL; + list_free(q->front); + q->front = newhead; + + return val; +} + +void queue_print(queue *q) { + printf("back "); + list_print(q->front); + printf("front\n"); +} + +void queue_free(queue *q) { + list_free(q->front); + free(q); +} + +int main() { + queue *q = make_queue(); + push(5, q); + push(3, q); + push(1, q); + queue_print(q); + printf("popped %d!\n", pop(q)); + queue_print(q); + queue_free(q); + + return 0; +}