Chained queue templates

Source: Internet
Author: User

#include <stdio.h>#include<stdlib.h>#include<malloc.h>#defineTRUE 1#defineFALSE 0#defineOK 1#defineERROR 0#defineINFEASIBLE-1#defineOVERFLOW-2typedefintStatus;typedefintElemtype;typedefstructqnode{elemtype data; structQnode *Next;} Qnode,*Ptr;typedefstruct{ptr head; PTR tail;} Linkqueue;status Initqueue (Linkqueue&AMP;Q) {//Initialize QueueQ.head=q.tail= (PTR)malloc(sizeof(Qnode)); if(!q.head) exit (OVERFLOW); Q.head->next=NULL; returnOK;} Status Enqueue (Linkqueue&q,elemtype e) {//inserting elements into a queuePTR p= (PTR)malloc(sizeof(Qnode)); if(!p) exit (OVERFLOW); P->next=null,p->data=e; Q.tail->next=p; Q.tail=p; returnOK;} Status Dequeue (Linkqueue&q,elemtype &e) {//Delete team first element    if(q.head==q.tail)returnERROR; PTR P= (PTR)malloc(sizeof(Qnode)); P=q.head->Next; E=p->data; Q.head->next=p->Next; if(q.tail==p) Q.tail=Q.head;  Free(P); returnOK;} Status Destroy (Linkqueue&AMP;Q) {//destroying a queue chain list      while(q.head) {Q.tail=q.head->Next;  Free(Q.head); Q.head=Q.tail; }     returnOK;}intGet_len (Linkqueue q) {//Get Queue Length  intCnt=0;  while(q.head->next!=NULL) {Q.tail=q.head->Next; CNT++; Q.head=Q.tail; }    returnCNT;} Status print (Linkqueue q) {//output Linear table elements     while(q.head->next!=NULL) {Q.tail=q.head->Next; printf ("%d",q.tail->data); Q.head=Q.tail; } printf ("\ n"); returnOK;} Status GetHead (Linkqueue q,elemtype&e) {//get the first element of the teamPTR p= (PTR)malloc(sizeof(Qnode)); P=q.head->Next; E=p->data; returnOK;} Status Qempty (Linkqueue q) {//determines whether the empty    if(q.head==q.tail)returnTRUE; Else        returnFALSE;} Status Clearqueue (Linkqueue&AMP;Q) {//clear Q to empty queueptr p1,p2; Q.tail=Q.head; P1=q.head->Next; Q.head->next=NULL;  while(p1) {P2=P1; P1=p1->Next;  Free(p2); }  returnOK;}intMain () {Linkqueue q;    Initqueue (q);    Elemtype e;  for(intI=1; i<=5; i++) {enqueue (q,i);    } gethead (Q,e); printf ("%d\n", e);//Output Team first elementprint (q); printf ("%d\n", Get_len (q));     Dequeue (q,e); Enqueue (q,Ten);     Print (q); printf ("%d\n", Get_len (q)); intFlag=qempty (q);//deciding whether to use when emptyprintf"%d\n", flag); Clearqueue (q);//use when emptying the queue    return 0;}

Chained queue templates

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.