Data structure and algorithm analysis-queue

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.