Added a text file for the editor race for UNIX 102.
This commit is contained in:
parent
ec3049e02b
commit
711b990949
|
@ -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
|
||||
|
|
|
@ -12,6 +12,9 @@
|
|||
as well as some extracurricular content for you to review in your free
|
||||
time.
|
||||
</p>
|
||||
<p>
|
||||
The text file for the great editor race is available <a href="queue">here</a>.
|
||||
</p>
|
||||
<p>
|
||||
To begin with the first exercise, you will need the following file.
|
||||
To download it, execute the following command:
|
||||
|
|
|
@ -0,0 +1,100 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
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;
|
||||
}
|
Loading…
Reference in New Issue