---
# System prepended metadata

title: 2021q1 Homework1 (lab0)
tags: [Linux Kernel Course]

---

---
title:  2021q1 Homework1 (lab0)
tags: Linux Kernel Course
---
contributed by <[Cancer-of-Japan](https://github.com/Cancer-of-Japan/lab0-c/blob/lab0-c_fork/queue.c)>

Meet the requirements below
- [ ] **q_new:** Create a new, empty queue.
- [ ] **q_free:** Free all list being used.
- [ ] **q_insert_head:** Insert a new element at the head of the list.
- [ ] **q_insert_tail:** same as q_insert_head, but insert at the end of the list.
- [ ] **q_remove_head:** Remove the element at the head of the list.
- [ ] **q_size:** Tells how many elements are in the list.
- [ ] **q_sort:** Sort elements in ascending oreder.

### Setup
- Install Package...
`$ sudo apt install build-essential git-core valgrind cppcheck clang-format aspell colordiff`
- Pull the [forked GitHub repository](https://github.com/Cancer-of-Japan/lab0-c): 
 `git clone https://github.com/Cancer-of-Japan/lab0-c.git`
- Build:
 `make clean`
 `make `

#### **q_new**
- Checks for NULL before initializing queue.

#### **q_free**
- Frees value and queue element internally.

#### **q_insert_head**
- First it checks whether queue is not NULL and new queue allocation before performing insertion.
- If "tail" is NULL, it means element is the first item in the queue, which implies both 'head' and 'tail' should point to the element.

#### q_insert_tail
- Logic is same as "q_insert_head".
- Make 'head' and 'tail' point to element when it is the first element.

#### q_remove_head
- Make sure queue and 'head' exsists before any operation.
- Check 'sp' variable before copying 'head -> value', it is to show which element is being removed on the 'qtest' console.
- If the element is happens to be only element in the list, make sure both 'head' and 'tail' are set to NULL after removing the element.
- Free memory at last.

#### q_size
- Checks whether it's NULL or not, and the if not, returns corresponding number for size.

#### q_reverse
- Use 3 variables to perform reversing process.

#### q_sort
- Use merge sort to sort list in ascending order.
- -> it's the part I could not finish.













