Concurrency system data Details-queue

Source: Internet
Author: User

Queued Data Structure graph

st650) this.width=650; "Src=" http://images2015.cnblogs.com/blog/610439/201601/ 610439-20160130151820974-712348880.png "width=" 759 "height=" 634 "alt=" 610439-20160130151820974-712348880.png "/ >c code Implementation, PHP program needs to understand C code this is the basis of HA

#include <stdio.h>

#include <stdlib.h>

#include <memory.h>

#define N 100//define constant n = 10

#define MYTYPE int//define constant MyType replace int

struct Myqueue

{

MyType data[n];//Array Storage queue

int front;//poop define team head

int rear;//eat something define the tail of the team

};


typedef struct MYQUEUE MyQ;

/*

Code description

In the process of initializing the queue? If the team head and the end of the team back to indicate that the queue is empty, here we set the team head and team tail for 0

The memory space is initialized at 0

MyQ *p represents an int type pointer

*/

void init (MyQ *p)

{

P->front = p->rear = 0;

memset (p->data,0,sizeof (MyType) *n);


}

/*

Determines whether the queue is full, if the tail equals n indicates that the space is full, returns 1 if it is not full, return O

There seems to be no bool type data in C, and 1 means true o means false. is actually the same as the bool type principle in PHP.

*/

int Isfull (MyQ *p)

{

if (p->rear = = N)

{

return 1;

}else

{

return 0;

}

}

/*

If the team header equals the end of the queue, it will return 1 or return o

*/

int IsEmpty (MyQ *p)

{

if (p->front==p->rear)

{

return 1;

}else{

return 0;

}

}

/*

Queued operation

Determine if the queue is full, return if full, and if not full, add the data you want to insert to the end of the team, and the tail pointer moves backwards

*/

void Array_unshift (MyQ *p,mytype insertdata)

{

if (Isfull (p) ==1)

{

Return

}else{

P->data[p->rear] = InsertData;

p->rear+=1;

}

}

/*

Out of team operation

Determine if the queue is empty and return 0

*/

void DeQ (MyQ *p)

{

if (IsEmpty (p) ==1)

{

return 0;

}

Because the end of the team is constant, the team head minus the end of the team to indicate

int index = p->rear-p->front;

if (index==1)

{

p->rear = 0;

}else{

int i;

for (i=0;i<index-1;i++)

{

P->data[i] = p->data[i+1];

}

p->rear-=1;

}

}

MyType getlast (MyQ *p)

{

if (IsEmpty (p) ==1)

{


return 0;

}

else{

MyType data = p->data[p->front];

int i;

int index = p->rear-p->front;

for (i=0;i<index-1;i++)

{

P->data[i] = p->data[i+1];

}

return data;

}

}

MyType Array_pop (MyQ *p)

{

if (IsEmpty (p) ==1)

{

return 0;

}else{

int i;

MyType index = p->rear-p->front;

for (i=index;i>=0;i--)

{

P->data[i] = p->data[i-1];

}


}

Return p->data[p->rear];

}

void print (MyQ *p)

{

printf ("\ n");

if (IsEmpty (p) ==1)

{

return 0;

}else{

int i;

for (i=p->front;i<p->rear;i++)

{

printf ("%d", p->data[i]);

}

}

}

Description: The above code through the test can be used, will not be fully annotated. This simulates two PHP system functions Array_shift reads from the head and Array_pop reads the function from the tail

When you use a queue to insert at one end and delete at the other end, it is a first-out data structure. In most of the projects? The queue is mostly combined with threads, and the next chapter explains multithreading and queues. help you understand Message Queuing. Please support the blog post a lot. Again, because I have limited technical level, the article has insufficient or wrong place, I will correct.

Concurrency system data Details-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.