Implement Queue in C Singly Linked List
This series is written for the purpose of supporting my self learning journey to become professional web developer. The complete article structure could be found in Technical Article Structure on Medium.
Introduction
The concept of Queue and Singly Linked List were covered in previous post.
- Introduction to Queue could be find in Implement Queue in C Array
- Introduction to Singly Linked List could be found in Implement Stack in C Singly Linked List
Implementation
There are 9 functions we need to implement, one of which is for debugging purpose.
- declare a node structure
- create newNode() to create a function to dynamically allocate memory space for a new node
- create enQueue() to append node to the linked list
- create deQueue() to remove front node from the list
- create countQueue() to get current size of linked list
- create deleteNode() to remove node with specified value
- create rear() to return the value for the last node
- create freeList() to free up memory
- (optional) create printList() to debug
Note: In the gist example, you’ll notice that even after we free up memory for the linked list, we are still able to print the linked list. The reason behind is that free() allows memory to be reused later, but not to erase the content. Although, it may work in one computer, it is not a guarantee that reference to original head will work in the future