contributed by < as200188
>
使用虛擬機器
$ gcc --version
gcc (Ubuntu 9.3.0-17ubuntu1~20.04) 9.3.0
$ lscpu
架構: x86_64
CPU 作業模式: 32-bit, 64-bit
Byte Order: Little Endian
Address sizes: 46 bits physical, 48 bits virtual
CPU(s): 4
On-line CPU(s) list: 0-3
每核心執行緒數: 1
每通訊端核心數: 4
Socket(s): 1
NUMA 節點: 1
供應商識別號: GenuineIntel
CPU 家族: 6
型號: 151
Model name: 12th Gen Intel(R) Core(TM) i5-12600K
製程: 2
CPU MHz: 3686.406
BogoMIPS: 7372.81
Hypervisor 供應商: KVM
虛擬型態: 全部
L1d 快取: 192 KiB
L1i 快取: 128 KiB
L2 快取: 5 MiB
L3 快取: 80 MiB
NUMA node0 CPU(s): 0-3
Create a new, empty queue.
struct list_head *q_new()
{
element_t *q = malloc(sizeof(element_t));
if (q == NULL)
return NULL;
}
INIT_LIST_HEAD(&q->list);
return q->list;
}
After allocating queue memory (element_t), use linux/list.h macro INIT_LIST_HEAD to initialize list_head member element.
Free all storage used by a queue.
void q_free(queue_t* q)
{
list_ele_t* prev = q->head;
while (q->head != q->tail) {
q->head = q->head->next;
free(prev);
prev = q->head;
}
/* Free queue structure */
free(q)
}
Attempt to insert a new element at the head of the queue (LIFO discipline).
/*
* Attempt to insert element at head of queue.
* Return true if successful.
* Return false if q is NULL or could not allocate space.
* Argument s points to the string to be stored.
* The function must explicitly allocate space and copy the string into it.
*/
bool q_insert_head(queue_t *q, char *s)
{
list_ele_t *newh;
/* TODO: What should you do if the q is NULL? */
newh = malloc(sizeof(list_ele_t));
if (newh == NULL) {
fprintf(stderr, "Failed to allocate memory for inserting head");
return false;
}
/* Don't forget to allocate space for the string and copy it */
/* What if either call to malloc returns NULL? */
newh->value = malloc(sizeof(char)* (strlen(s)+1));
if (newh->value == NULL) {
fprintf(stderr, "Failed to allocate memory of char array for inserting head");
return false;
}
newh->next = q->head;
q->head = newh;
return true;
}
Attempt to insert a new element at the tail of the queue (FIFO discipline).
1. Allocate memory for list element and char array.
2. Insert it to the end of list
3. Increment the queue size counter.
/*
* Attempt to insert element at head of queue.
* Return true if successful.
* Return false if q is NULL or could not allocate space.
* Argument s points to the string to be stored.
* The function must explicitly allocate space and copy the string into it.
*/
bool q_insert_head(queue_t *q, char *s)
{
list_ele_t *newh;
/* TODO: What should you do if the q is NULL? */
newh = malloc(sizeof(list_ele_t));
if (newh == NULL) {
fprintf(stderr, "Failed to allocate memory for inserting head");
return false;
}
/* Don't forget to allocate space for the string and copy it */
/* What if either call to malloc returns NULL? */
newh->value = malloc(sizeof(char)* (strlen(s)+1));
if (newh->value == NULL) {
fprintf(stderr, "Failed to allocate memory of char array for inserting head");
return false;
}
newh->next = q->head;
q->head = newh;
return true;
}
Attempt to remove the element at the head of the queue.
/*
* Attempt to remove element from head of queue.
* Return true if successful.
* Return false if queue is NULL or empty.
* If sp is non-NULL and an element is removed, copy the removed string to *sp
* (up to a maximum of bufsize-1 characters, plus a null terminator.)
* The space used by the list element and the string should be freed.
*/
bool q_remove_head(queue_t *q, char *sp, size_t bufsize)
{
if (q == NULL || q->head == NULL) {
return false;
}
sp = strdup(q->head->value);
list_ele_t* prev = q->head;
q->head = q->head->next;
free(prev);
q->size--;
return true;
}
Compute the number of elements in the queue.
/*
* Return number of elements in queue.
* Return 0 if q is NULL or empty
*/
int q_size(queue_t *q)
{
/* TODO: You need to write the code for this function */
/* Remember: It should operate in O(1) time */
return q->size;
}
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.