Data structure and algorithm analysis-queue (single-linked list implementation)
#include <Stdio.H>#include <Stdlib.H>typedef int ELEMENTTYPE;TYPEDEF struct HEADNODE*Queue; typedef struct NODE*Position;struct Node {ElementTypeData; Position next;}; struct headnode{int size; Position Front; position rear;};//Headnode is a use-head mark, next point-to-follow node, rear point to the end node, size declaration the length of whole queue.QueueInit_queue (void);voidDelete_queue (QueueQ); int IsEmpty (QueueQ); ElementType dequeue (QueueQ);voidEnqueue (ElementTypeData,QueueQ);QueueInit_queue (void){QueueQ; Q=(Queue) malloc (sizeof (struct headnode)); Q -Front= NULL; Q -Rear= NULL; Q -Size= 0;returnQ;}voidDelete_queue (QueueQ) { while(!IsEmpty (q)) dequeue (q); Free (Q);} int IsEmpty (QueueQ) {return((Q -Front== NULL)&&(Q -Rear== NULL)&&(Q -Size== 0));}voidEnqueue (ElementTypeData,QueueQ) {position P; P=(position) malloc (sizeof (struct node)); P -Data = Data; P -Next= NULL;if(IsEmpty (Q))//when Q is empty, the new node (P) is both front node and rear node.Q -Front=PElseQ -Rear -Next=P//when Q isn ' t empty, the new node (P) is rear node, the front node isn't change, but original Rear->next would point To PQ -Rear=P Q -Size++;} ElementType Dequeue (QueueQ) {ElementTypeData; Position tem;if(!IsEmpty (Q)) {tem=Q -FrontData =Q -Front -Data; Q -Size--; Q -Front=Tem -Nextif(Q -Size== 0) Q -Rear= NULL; Free (TEM);return Data; }Else{printf ("Enqueue:error!" Q is a empty queue!\n ");return 0; }}int Main (int argc, Char**argv) {QueueQ; ElementTypeData; int I= 0; Q=Init_queue (); printf"Queue size is:%d\n\n"Q -size); printf"Enqueue Ten datas\n"); for (i=0; I<Ten; I++) {Enqueue (I, Q); } printf ("Queue size is:%d\n\n"Q -size); printf"Enqueue 5 datas\n"); for (i=0; I<5; I++) {Data =Dequeue (Q); printf"dequeue data is:%d\n",Data); } printf ("Enqueue Ten datas\n\n"); for (i=Ten; I< -; I++) {Enqueue (I, Q); } printf ("Queue size is:%d\n"Q -size); for (i=0; I< the; I++) {Data =Dequeue (Q); printf"dequeue data is:%d\n",Data); } printf ("Queue size is:%d\n"Q -size);}
Data structure and algorithm analysis-queue