United States (seven) data structure of the order queue storage structure and chain storage structure

table of Contents

queue

Sequential storage queue structure

Storage Structure queue

Enqueue operation

Dequeue


queue

Sequential storage queue structure

Queues, as the name suggests it is a kind of structure we usually line up, the following JV with you a detailed look at its definition.

Queue (Queue) are only allowed in the insertion operation (the tail) end , while the other end of the delete operation (HOL) linear form. It FIFO (First In First Out), just line up the same.

Why delete operation is O (n) do? Because there will be a mobile element after team head deletion. Then we can think of a way to optimize a bit and let the delete operation simple.

We say that an end of a delete operation is referred to as the first team, but not necessarily required in the next queue head for the location of 0, so we can introduce two pointers point to the head and tail of the queue, so do not move when the element deletion operation, but to move the pointer, so that the time complexity becomes O (1) from O (n).

OK, now we have two pointers: front and rear, respectively, point to the team head and the tail.

So, how do we judge this queue is empty is full of it? Some would say that it is very simple, since the front point to the team head, rear pointing to the tail, as long as the front = REAR , the queue was filled with chanting. The students you're too naive friends, take a look at the figure below.

You see, both of which do not satisfy the front = rear it, but a team a team full of empty, then in the end determine how full or empty the queue of it?

In fact, there are two ways:

1, we find that with these two cases is that the front = rear, except that a completely empty, there is a whole. Then we can find a place in the queue of the most pleasing to the eye to see, standing at a flag-tagged, when the front = rear, we judge the flag of place, if the flag is empty, the whole queue is empty, otherwise the whole is full .

2, modify the judgment conditions no longer allow to determine the empty front = rear full, we can redefine: The front = rear, air force, when there is an element of the space is empty, full team.

It is this sub-sub-drop ~

Let's talk discuss why, rather than directly define the front = rear + 1 or front = rear-1 do? Because a pointer to spend, we certainly hope that the queue has to be more efficient use of space for the array as possible, rear may appear in front of or behind the front, but when you tell the computer, can not be ambiguous, so when we define there is an element of the space is empty, full team.

This idea how to tell the computer it? Provided maximum dimension QueueSize queue, then the queue full condition should be (rear + 1)% QueueSize == front, queue length is calculated as: (rear-front + QueueSize)% QueueSize (because of an empty, it can not be counted length, that is to say, this formula is calculated using the actual queue length) you can be the front of the queue, practice it.

ok, then we can start writing code for it

typedef int QElemType;

typedef struct
{
    QElemType data[MAXSIZE];
    int front;
    int rear;
}SqQueue;


//初始化一个空队列
Status InitQueue(SqQueue *Q)
{
    Q->front=0;
    Q->rear=0;
    return OK;
}

//返回队列的元素个数,即当前长度
int QueueLength(SqQueue Q)
{
    return (Q.rear-Q.front+MAXSIZE)%MAXSIZE;
}

//入队操作
Status EnQueue(SqQueue *Q,QElemType e)
{
    if((Q->rear+1)%MAXSIZE==Q->front)
        return ERROR;        //判断队列是否满了
    Q->data[Q->rear]=e;        //赋值
    Q->rear=(Q->rear+1)%MAXSIZE; //指向下一个
    return OK;
}

//出队操作,用e返回其值
Status DeQueue(SqQueue *Q,QElemType *e)
{
     if((Q->rear+1)%MAXSIZE==Q->front)
        return ERROR;        //判断队列是否满了
    *e=Q->data[Q->front];//将队头元素赋值给e
    Q->front=(Q->front+1)%MAXSIZE    //front指针向后移动一位
                                     //若到最后则转到数组头部
    return OK;
}

Storage Structure queue

Storage Structure queue, in fact, a single chain linear form, but only the intake end of the head only.

When the queue is empty, front and rear head node point.

typedef int QElemType;

typedef struct QNode  //结点结构
{
    QElemType data;
    struct QNode *next;
}QNode,*QueuePtr;

typedef struct 
{
    QueuePtr front,rear; //队头队尾指针
}LinkQueue;

Enqueue operation

Status EnQueue(LinkQueue *Q,QElemType e)
{
    QueuePtr s=(QueuePtr)malloc(sizeof(QNode))
    if(!s)
        exit(OVERFLOW);
    s->data=e;
    s->next=NULL;
    Q->rear->next=s;
    Q->rear=s;
    return OK;
}

Dequeue

//删除队头,并用e返回其值
Status DeQueue(LinkQueue *Q,QElemType *e)
{
    QueuePtr p;
    if(Q->front==Q->rear)   //判断是否为空
        return ERROR;

    p=Q->front->next;       
    *e=p->data;
    Q->front->next=p->next;

    if(Q->rear==p)
        Q->rear=Q->front;
    free(p);

    return OK;
}

 

Published 38 original articles · won praise 6 · views 1906

Guess you like

Origin blog.csdn.net/weixin_43827227/article/details/100903422