Queue (i)--array implementation of the queue

Source: Internet
Author: User

1. The concept of a queue
A queue is a special linear table that simply agrees to be inserted at one end of the queue and deleted at the other end.
Queues typically have a team head (front pointer) and a team tail (rear pointer). When a queue is not in the data, the front and rear pointers point to the head of the team.
Queued operation: Rear back, deposit data in the rear point of the unit, the team is not in the queue, this same time also indicates that front always point to the first element of the precursor.
Operation of the team: front, elements out of the team, the team should not be out of the team.


Note: In such a way that the queue is implemented. A waste of a unit, but this will ensure that the team full and the team empty is different conditions to infer.
2. Queue empty and queue full
Queue empty: Front = rear
Queue full: The next unit of rear is front
3. How to implement the array of queues

#include <stdio.h> #include <malloc.h>typedef struct sq{int maxsize;int front, rear;int *quence;} Qs;void init_quence (Qs *s, int ms)/* Initialize Queue */{s->maxsize = Ms;s->quence = (int *) malloc (ms*sizeof (int)); S->front = S->rear = 0;} void In_quence (Qs *s, int val)/* Queue function */{if ((s->rear+1)%s->maxsize = = S->front) {printf ("Quence is full.\n"); return;} S->rear = (s->rear+1)%s->maxsize;s->quence[s->rear] = val;} int out_quence (Qs *s)/* out Team function */{if (s->rear! = S->front) {S->front = (s->front+1)%s->maxsize;return s-> Quence[s->front];}} void Print_quence (Qs *s)/* print queue element */{int i;i = s->front;if (s->rear = = S->front) return;do{printf ("%d", s-> quence[(i+1)%s->maxsize]); i = (i+1)%s->maxsize;} while (i! = s->rear);} void Clear_quence (Qs *s)/* Clear Queue */{free (s->quence); s->quence = 0;s->maxsize = 0;s->front = S->rear = 0;} int count_quence (Qs *s)/* Count queue Count */{int I, count = 0;i = s->front;if (s->rear = S->front) RetuRN 0;do{count ++;i = (i+1)%s->maxsize;} while (i! = s->rear); return count;} int main () {Qs s;int i;int dat[7] = {1, 2, 3, 4, 5, 6, 7};init_quence (&s, 7); for (i = 0; i < 7; i++) {in_quence (&s , Dat[i]);p rintf ("quence number is:%d.", Count_quence (&s));p rint_quence (&s);p rintf ("\ n");} printf ("Out quence number is:%d.\n", Out_quence (&s));p rint_quence (&s); clear_quence (&s); return 0;}

Program execution:


Queue (i)--array implementation of the 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.