C Language single chain queue representation and implementation Example _c language

Source: Internet
Author: User

1. Overview:

C-language queues (queue), which refers to the first out (FIFO, first-in-first-out) of the linear table. In a specific application, a linked list or array is usually used for implementation. Queues allow insertions only at the back end (called rear) and are removed at the front end (called the front).

The single chain queues use linked lists as the basic data results, so there is no problem of pseudo overflow, queue length is not limited. But the time to insert and read is higher

2. Instance code:

/* Single Chain queue----chained storage structure of queue/typedef struct QNODE {qelemtype data;
struct Qnode *next;
}qnode,*queueptr;
typedef struct {queueptr front,rear;/* team head, Team tail pointer * *}LINKQUEUE;
 /* Chain Queue Basic Operations (9)/void Initqueue (Linkqueue *q) {/* Constructs an empty queue Q/q->front=q->rear=malloc (sizeof (Qnode)); if (!
 Q->front) exit (OVERFLOW);
q->front->next=null;
  } void Destroyqueue (Linkqueue *q) {/* Destroy queue Q (either null or void) */while (Q->front) {q->rear=q->front->next;
  Free (Q->front);
 q->front=q->rear;
 } void Clearqueue (Linkqueue *q) {/* The Q is cleared as an empty queue */Queueptr p,q;
 q->rear=q->front;
 p=q->front->next;
 q->front->next=null;
  while (p) {q=p;
  p=p->next;
 Free (q);
 The Status queueempty (linkqueue q) {/* If q is an empty queue, returns True, otherwise returns false */if (q.front->next==null) return true;
else return FALSE;
 The int queuelength (Linkqueue Q) {* * asks for the length of the queue */int i=0;
 Queueptr p;
 P=q.front;
  while (q.rear!=p) {i++;
 p=p->next;
return i; Status gethead_q (Linkqueue q,qElemtype *e) {/* If the queue is not empty, then use E to return the team head element of Q, and return OK, otherwise return error * * * QUEUEPTR p;
 if (q.front==q.rear) return ERROR;
 p=q.front->next;
 *e=p->data;
return OK;
 } void EnQueue (Linkqueue *q,qelemtype e) {//* insert element E As Q of the new team tail element */queueptr p= (queueptr) malloc (sizeof (Qnode));
 if (!p)/* Storage Allocation failed/exit (OVERFLOW);
 p->data=e;
 p->next=null;
 q->rear->next=p;
q->rear=p;
 Status dequeue (linkqueue *q,qelemtype *e) {/* If the queue is not empty, delete Q's team head element, return its value with E, and return OK, otherwise return error * * * QUEUEPTR p;
 if (q->front==q->rear) return ERROR; p=q->front;
 /* Point head node * * *e=p->data; q->front=p->next;
 /* Pick the head node */if (q->rear==p) q->rear=q->front;
 Free (p);
return OK;
 } void Queuetraverse (Linkqueue q,void (*VI) (Qelemtype)) {/* calls function VI ()/queueptr p for each element in queue Q from Team head to Team tail;
 p=q.front->next;
  while (p) {VI (P->DATA);
 p=p->next;
printf ("\ n"); }

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.