The implementation of chain-type queue

Source: Internet
Author: User

Definition of a queue

only new elements are allowed to be appended at the end of the team, and elements are removed at the first team. is a FIFO model. Queue operations are much less than linked lists, so we can build a queue based on a linked list.

Diagram of the queue


/***********file:linkqueue.hdate:2016/3/28***********/#ifndef _linkqueue_h_#define_linkqueue_h_#include<stdlib.h>typedefintDataType;//define the destination data type of the storetypedefstructnode{//define each of the node typesDataType data; structnode*Next; }node;typedefstruct{//Defining Queuesnode*Front; Node*tail; }linkqueue;voidInitqueue (linkqueue*queue);voidEnQueue (linkqueue*queue,datatype newdata);intDeQueue (linkqueue* queue,datatype*de);intQueuelength (Constlinkqueue*queue);intQueueisempty (Constlinkqueue*queue);#endif

/************file:linkqueue.cppdate:2016/3/28*************/#include"LinkQueue.h"//Initialize the queue, the head pointer, and the pointer are all emptyvoidInitqueue (linkqueue*queue) {Queue->front =NULL; Queue->tail =NULL; }//Join the queue and append an element to the tail of the list. //pay special attention to the first element in the queuevoidEnQueue (linkqueue*Queue,datatype NewData) {Node*p_new; P_new= (node*)malloc(sizeof(Node)); if(P_new==null) Exit (-1); P_new->data =NewData; P_new->next =NULL; if(Queueisempty (queue))//the first element of the queue is a case{Queue->front = Queue->tail =p_new; }        Else{//section 2,3,4 the queue of elementsQueue->tail->next = p_new;//linksQueue->tail = p_new;//Reset Tail node    }    }//out of the team, delete the first element of the list//pay special attention to the case where the last element was deletedintDeQueue (linkqueue* queue,datatype*de) {Node*t = queue->front;//gets the address of the team head node is T            if(Queueisempty (queue))//The queue is empty, no more teams.      return 0; Queue->front = t->next;//the head node address of the queue is moved back    if(queue->front==NULL) Queue->tail = NULL;//if the team head pointer is empty, this time the team is empty.         if(De!=null)//to save the deleted element back*de = t->data;  Free(t); return 1; }//the length of the queueintQueuelength (Constlinkqueue*queue) {    intCount=0; Node*p = queue->Front;  while(p!=NULL) {P= p->Next; Count++; }    returncount;}//whether the queue is emptyintQueueisempty (Constlinkqueue*queue) {    if(Queue->front = = NULL && Queue->tail = =NULL)return 1; return 0; }

The implementation of chain-type 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: 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.