contributed by < joey-ycpeng
>
用 linked-list 實做的 queue:
藉由 queue.h
裡的兩個 data structure 實做出一個 string 的 queue
queue_t
:
head
指向第一個 element,初始化為 NULLlist_ele_t
:
value
指向一個 string (array of char
)next
指向下一個 list_ele_tnext
指向 NULLqueue.h
& queue.c
實做出下列 functionq_new
: Create a new, empty queue.q_free
: Free all storage used by a queue.q_insert_head
: Attempt to insert a new element at the head of the queue (LIFO discipline).q_insert_tail
: Attempt to insert a new element at the tail of the queue (FIFO discipline).q_remove_head
: Attempt to remove the element at the head of the queue.q_size
: Compute the number of elements in the queue.q reverse
: Reorder the list so that the queue elements are reversed in order. This function should not allocate or free any list elements (either directly or via calls to other functions that allocate or free list elements.) Instead, it should rearrange the existing elements.q_size
:
queue_t
加入 int size
q_new
:
q->size = 0;
q_free
:
q_insert_head
:
++q->size
,並在 q->size==1
的時候更新 q->tail
q_insert_tail
:
queue_t
裡加入 list_ele_t *tail
++q->size
,並在 q->size==1
的時候更新 q->head
q_remove_head
:
q->head = q->head->next
之前要先把 q->head
存起來準備之後 free 掉q_reverse
:
homework