The chain list does not limit the length of elements, can be dynamically allocated elements and add, in addition to frequent additions and deletions is better than other data structure of the list of characteristics.
Today we use a linked list to implement a queue.
LinkList.h
#include <stdio.h>#include<stdlib.h>#include<sys/types.h>#include<sys/stat.h>#include<fcntl.h>#include<unistd.h>#defineNew (type) (type *) malloc (sizeof (type))#defineFree (p)if(P! =NULL) { Free(P); P=NULL; }typedefstructnode{intdata; structNode *Next;} ListNode,*Plistnode;typedefstruct_queue{intsize; Plistnode Headlink; Plistnode Taillink;} Queue,*pqueue;pqueue Createdqueue (void);p ListNode CreateNode (intvalue);p ListNode popqueue (pqueue);voidpushqueue (pqueue queue, Plistnode node);voidDestroyqueue (Pqueue *queue);voidDestroylistnode (Plistnode *node);intlengthofqueue (pqueue queue);voidShowqueue (pqueue queue);
This introduces a size to count the queue,
The API does not determine empty or full, directly with this size.
Linklist.c
#include <stdio.h>#include<stdlib.h>#include<sys/types.h>#include<sys/stat.h>#include<fcntl.h>#include<unistd.h>#include<string.h>#include<assert.h>#include"linkList.h"//When you create a queue, the Kinsoku pointer points to a node with a data domain of 0.Pqueue Createdqueue (void) {Pqueue PQ=New(Queue); ASSERT (PQ!=NULL); //Plistnode pn = createnode (0); //assert (pn! = NULL);Pq->size =0; PQ->headlink = NULL;//PN;Pq->taillink = NULL;//PN; returnPQ;} Plistnode CreateNode (intvalue) {Plistnode pn=New(ListNode); ASSERT (PN!=NULL); PN->data =value; PN->next =NULL; returnPN;}//Deleting a node is removing the node that Headlink points to, changing the Headlink pointplistnode popqueue (pqueue queue) {assert (queue!=NULL); if(Queue->size = =0) returnNULL; Plistnode PN= queue->Headlink; Queue->headlink = pn->Next; PN->next =NULL; Queue->size--; if(Queue->size = =0) Queue->taillink =NULL; returnPN;}//Add the nodes at the end of the line, change the Taillink point, adding the first element Headlink and Taillink all point to this nodevoidpushqueue (pqueue queue, Plistnode node) {assert (queue!=NULL); ASSERT (Node!=NULL); if(Queue->size = =0) {Queue->headlink =node; Queue->taillink =node; } Else{Queue->taillink->next =node; Queue->taillink =node; } Queue->size++;}voidDestroyqueue (Pqueue *queue) {Assert (*queue! =NULL); while((*queue)->size--! =0){//Clear All nodesPlistnode pn = Popqueue (*queue); Destroylistnode (&PN); } //Free (Queue->headlink); //Free (queue->taillink);Free (*queue);}voidDestroylistnode (Plistnode *node) {Assert (*node! =NULL); (*node)->next =NULL; Free (*node);}intlengthofqueue (pqueue queue) {assert (queue!=NULL); ASSERT (Queue->size = =0|| Queue->size >0); returnQueue->size;}voidshowqueue (pqueue queue) {Plistnode pn= queue->Headlink; if(PN = =NULL)return ; printf ("Showqueue Order"); intLength = queue->size; while(Length--! =0) {printf ("[%d]", pn->data); PN= pn->Next; } printf ("\ n");}
The main function of the test program MAIN.C
#include <stdio.h>#include<sys/types.h>#include<sys/stat.h>#include<fcntl.h>#include<unistd.h>#include<string.h>#include"linkList.h"intMain () {Pqueue PQ=Createdqueue (); printf ("Push circularqueue 1,2,3,4,5,6,7..\n"); CreateNode (1); Plistnode PN= CreateNode (1); Destroylistnode (&PN); PN= CreateNode (2); Pushqueue (PQ, PN); Showqueue (PQ); PN= CreateNode (3); Pushqueue (PQ, PN); PN= CreateNode (4); Pushqueue (PQ, PN); PN= CreateNode (5); Pushqueue (PQ, PN); PN= CreateNode (6); Pushqueue (PQ, PN); Showqueue (PQ); Popqueue (PQ); Showqueue (PQ); Popqueue (PQ); Showqueue (PQ); Popqueue (PQ); Showqueue (PQ); Popqueue (PQ); Showqueue (PQ); Popqueue (PQ); Showqueue (PQ); Popqueue (PQ); Showqueue (PQ); Popqueue (PQ); Showqueue (PQ); Destroyqueue (&PQ); PQ=Createdqueue (); printf ("Push circularqueue 1,2,3,4,5,6,7..\n"); CreateNode (1); PN= CreateNode (1); Destroylistnode (&PN); PN= CreateNode (2); Pushqueue (PQ, PN); Showqueue (PQ); Popqueue (PQ); Showqueue (PQ); PN= CreateNode (3); Pushqueue (PQ, PN); Showqueue (PQ); PN= CreateNode (4); Pushqueue (PQ, PN); Showqueue (PQ); return 0;}
View Code
The output results are as follows:
Push Circularqueue1,2,3,4,5,6,7.. Showqueue Order [2]showqueue Order [2] [3] [4] [5] [6]showqueue Order [3] [4] [5] [6]showqueue Order [4] [5] [6]showqueue Order [5] [6]showqueue Order [6]push Circularqueue1,2,3,4,5,6,7.. Showqueue Order [2]showqueue Order [3]showqueue Order [3] [4]
Using a linked list to implement the functions of a queue