Queue using non-cyclic two-way linked list in C Language
In the previous two blogs, I used static arrays and dynamic arrays to simulate cyclic queues. However, in a linear table, the most important thing in a queue is a linked list. I used a lot of space to talk about the operations of the linked list. Today, we use a special linked list-non-cyclic two-way linked list to implement queues. First of all, it indicates that a common queue is built instead of a cyclic queue. When we use arrays, we create a cyclic queue to save storage space. When we come to the linked list, every node dynamically applies for and releases, without wasting space, therefore, you do not need to use cyclic queues. Second, we can see in many books that a single-chain table is used to implement a queue. Here I will use the non-cyclic double-chain table at the end of the leading node, although two nodes and pointer fields are maintained, you do not need to traverse the table when inserting or deleting the beginning and end of the linked list, which makes queue operations very convenient. Only the first and last operations are actually implemented.
The core code is as follows:
(1) initialize the queue
// Initialize void InitialQueue (Queue ** pHead, Queue ** pTail) {* pHead = (Queue *) malloc (sizeof (Queue); * pTail = (Queue *) malloc (sizeof (Queue); if (* pHead = NULL | * pTail = NULL) {printf ("% s FUNCTION execution, memory allocation failure, failed to initialize double-stranded table \ n" ,__ FUNCTION _);} else {// This is the key, it is also an important condition for NULL determination (* pHead)-> next = NULL; (* pTail)-> prior = NULL; // when the linked list is empty, link the header and tail nodes (* pHead)-> prior = * pTail; (* pTail)-> next = * pHead; printf ("% s FUNCTION execution, two-way non-cyclic linked list initialization of the lead node and the End Node is successful \ n" ,__ FUNCTION __);}}
(2) join and insert elements at the End Node
// Enter the Queue, that is, insert the node void EnQueue (Queue * head, Queue * tail, int x) {Queue * pInsert; pInsert = (Queue *) at the end of the linked list *) malloc (sizeof (Queue); memset (pInsert, 0, sizeof (Queue); pInsert-> next = NULL; pInsert-> prior = NULL; pInsert-> element = x; tail-> next-> prior = pInsert; pInsert-> next = tail-> next; tail-> next = pInsert; pInsert-> prior = tail ;}
(3) leave the team and delete the node at the header Node
// Leaves the Queue. Delete the element void DeQueue (Queue * head, Queue * tail) {if (IsEmpty (head, tail) {printf ("the Queue is empty, outbound Queue failed \ n ");} else {Queue * pFreeNode; pFreeNode = head-> prior; head-> prior-> next = head; head-> prior = head-> prior; free (pFreeNode); pFreeNode = NULL ;}}
(4) print all nodes
// Print out all the elements from the Queue header to the tail void PrintQueue (Queue * head, Queue * tail) {Queue * pMove; pMove = head-> prior; printf ("the element in the current queue is (starting from the header):"); while (pMove! = Tail) {printf ("% d", pMove-> element); pMove = pMove-> prior;} printf ("\ n ");}
(5) Determine whether the queue is empty
// Determine whether the Queue is empty. if it is null, 1 is returned. Otherwise, 0int IsEmpty (Queue * head, Queue * tail) {if (head-> prior = tail) is returned) {return 1 ;}return 0 ;}
(6) test code
Int main (int argc, const char * argv []) {Queue * pHead; // header node Queue * pTail; // InitialQueue (& pHead, & pTail ); enQueue (pHead, pTail, 2); EnQueue (pHead, pTail, 1); EnQueue (pHead, pTail, 9); EnQueue (pHead, pTail, 3); EnQueue (pHead, pTail, 4); PrintQueue (pHead, pTail); DeQueue (pHead, pTail); PrintQueue (pHead, pTail ); return 0 ;}