In the previous two blogs, I used a static array and a dynamic array to simulate the loop queue. But the linear table and the queue is the most spiritual likeness of the list. I also used a lot of space in front of the list to describe the various operations. Today we use a kind of special linked list--non-cyclic bidirectional linked list to implement the queue. The first point here is to build a regular queue, not a loop queue. When we use the array to create a circular queue is to save storage space, and when we come to the list, each node is dynamically applied and released, does not cause a waste of space, so there is no need to use the loop queue. Second, what you see in many books is the use of single linked lists to implement queues, I will use the first node of the first knot of the non-cyclic double linked list, although more than two nodes and pointer field maintenance, but in the end of the link to insert the deletion of the link list does not need to traverse, queue operation has become very convenient. Really implemented only in the end-to-end operation. Code uploaded to Https://github.com/chenyufeng1991/Queue_LinkedList.
The core code is as follows:
(1) Initialization queue
Non-cyclic bidirectional linked list
void Initialqueue (Queue **phead,queue **ptail) {
*phead = (Queue *) malloc (sizeof (queue) for initialization of lead and trailing nodes );
*ptail = (Queue *) malloc (sizeof (queue));
if (*phead = null | | *ptail = NULL) {
printf ("%s function execution, memory allocation failure, initialization of double linked list failed \ \", __function__);
} else{
//This inside is the key, but also the important condition of the short term
(*phead)->next = null;
(*ptail)->prior = NULL;
When the list is empty, the head node and the tail knot are connected
(*phead)->prior = *ptail;
(*ptail)->next = *phead;
printf ("%s function execution, leading node and tail node's bidirectional non-cyclic list initialization succeeded \ n", __function__);
}
(2) Join the team, insert the element at the end node
Team, that is, at the end of the list insert node
void EnQueue (queue *head,queue *tail,int x) {
queue *pinsert;
Pinsert = (Queue *) 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) Out team, delete the node at the head node
Out team, in queue header delete element
void dequeue (Queue *head,queue *tail) {
if (IsEmpty (Head,tail)) {
printf ("Queue is empty, out queue failed \ \ \ n ");
} else{
Queue *pfreenode;
Pfreenode = head->prior;
Head->prior->prior->next = head;
Head->prior = head->prior->prior;
Free (pfreenode);
Pfreenode = NULL;
}
}
(4) Print all nodes
Prints out all element
void PrintQueue (Queue *head,queue *tail) {Queue *pmove from the head of the queue to the tail
;
Pmove = head->prior;
printf (the elements in the current queue are (starting from the head):);
while (Pmove!= tail) {
printf ("%d", pmove->element);
Pmove = pmove->prior;
}
printf ("\ n");
}
(5) To determine whether the queue is empty
Determines whether the queue is empty, returns 1 for null, or returns 0
int isempty (Queue *head,queue *tail) {
if (Head->prior = = tail) {return
1;
return
0;
}
(6) Test code
int main (int argc, const char * argv[]) {
Queue *phead;//header node
queue *ptail;//Tail node
initialqueue (&phead, & Amp;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);D equeue (phead,ptail);D equeue (phead,ptail);
PrintQueue (Phead, ptail);
return 0;
}