队列的链式存储和顺序存储

1、队列的链式存储:

思路:

(1)建立两个结构体,一存储数据及下一个节点的信息,另一个存储链表的表头和表尾指针。

(2)出队列时一定要注意如果只剩下最后一个节点,要提前让Q->Top=Q->Rear,因为如果tp被删除后,就不能再去调用它的next,

最后就找不到结果了。

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

struct Node{
    int x;
    struct Node* next;
};

struct N{
    struct Node* Top;
    struct Node* Rear;
};
typedef struct N* Queue;

int IsEmpty(Queue Q)
{
    return Q->Top==Q->Rear; 
}

Queue CreateQueue()
{
    Queue Q=(Queue)malloc(sizeof(struct N));
    if(Q==NULL) printf("Out of Space!!!");
    
    Q->Rear=(Node*)malloc(sizeof(struct Node));
    if(Q->Rear==NULL) printf("Out of Space!!!");
    Q->Rear->next=NULL;
    
    Q->Top=(Node*)malloc(sizeof(struct Node));
    if(Q->Top==NULL) printf("Out of Space!!!");
    Q->Top->next=NULL;
    
    Q->Top=Q->Rear;
    return Q;
}

void EnQueue(int x,Queue Q)
{
    struct Node* tp=(Node*)malloc(sizeof(struct Node));
    tp->x=x;
    tp->next=Q->Rear->next;
    Q->Rear->next=tp;
    Q->Rear=Q->Rear->next;
}

int DeQueue(Queue Q)
{
    if(!IsEmpty(Q))
    {
        int x;
        struct Node* tp=Q->Top->next;
        x=tp->x;
        Q->Top->next=tp->next;
        if(Q->Rear==tp) Q->Rear=Q->Top; //重点,没有就不能结束。 
        free(tp);
        return x;
    }
}

int main(void)
{
    Queue Q=CreateQueue();
    int n,x,i;
    scanf("%d",&n);
    for(i=0;i<n;i++)
    {
        scanf("%d",&x);
        EnQueue(x,Q);
    }
    while(!IsEmpty(Q))
    {
        printf("%d ",DeQueue(Q));
    }
    printf("\n");
    return 0;
}
View Code

 

2、队列的顺序存储

思路:

 建立队列的结构体,每个结构体中有top头指针,rear尾指针,size队列的现在的大小,capcity队列的最大容量。

然后初始化top=1,rear=0,每次插入rear++,就表示插入新节点,top++表示出队列,这里要对rear,top进行取余或者判断操作,

让它们在数组的范围内变化(就是循环数组)。

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

const int maxn = 1200;

struct Node{
    int capcity;
    int top;
    int rear;
    int size;
    int *array;
};

typedef struct Node* Queue;

int IsEmpty(Queue Q)
{
    return Q->size==0;
}

int IsFull(Queue Q)
{
    return Q->size==Q->capcity;
}

void MakeEmpty(Queue Q)
{
    Q->top=1;
    Q->rear=0;
}

int Succ(int x,Queue Q)
{
    if(++x==Q->capcity) x=0;
    return x;
}

void EnQueue(int x,Queue Q)
{
    if(IsFull(Q))
    {
        printf("Full Queue");
    }
    else
    {
        Q->size++;
        Q->rear=Succ(Q->rear,Q);
        Q->array[Q->rear]=x;
    }
}

int DeQueue(Queue Q)
{
    if(!IsEmpty(Q))
    {
        Q->size--;
        int x=Q->array[Q->top];
        Q->top++;
        if(Q->top==Q->capcity) Q->top=0;
        return x;
    }
}

Queue CreateQueue(int maxsize)
{
    Queue Q=(Queue)malloc(sizeof(struct Node));
    if(maxsize>maxn) printf("too small");
    Q->capcity=maxsize;
    Q->size=0;
    Q->array=(int*)malloc(sizeof(int)*maxsize);
    MakeEmpty(Q);
    return Q;
}

int main(void)
{
    int n,i,x;
    scanf("%d",&n);
    Queue Q=CreateQueue(n);
    for(i=0;i<n;i++)
    {
        scanf("%d",&x);
        EnQueue(x,Q);
    }
    while(!IsEmpty(Q))
    {
        printf("%d ",DeQueue(Q));
    }
    printf("\n");
    return 0;
}
View Code

 

posted @ 2018-11-29 18:51  麟阁  阅读(597)  评论(0编辑  收藏  举报