队列的链式实现及顺序实现

链式:

#include<stdio.h>
#include<malloc.h>
#define MAXQSIZE 100
#define OK 1
#define OVERFLOW -2
typedef int ElemType;
//LinkQueue链队列

//队列节点
typedef struct Node{
    ElemType data;
    struct Node *next;
}Node;
//队列队头、队尾指针
typedef struct{
    Node* front;
    Node* rear;
}LinkQueue;

//初始化队列(以下带头结点)
void creatQueue(LinkQueue &Q){
    //初始时,front、rear都指向头结点
    Q.front = Q.rear = (Node*)malloc(sizeof(Node));
    Q.front->next = NULL;
}

//判断队空
bool isEmpty(LinkQueue Q){
    if(Q.front == Q.rear)
        return true;
    else
        return false;
}

//入队操作(带头结点)
void enQueue(LinkQueue &Q,ElemType e){
    Node *s = (Node*)malloc(sizeof(Node));//创建新节点
    s->data = e;//赋值
    s->next = NULL;//队尾插入,所以s的next域为NULL
    Q.rear->next = s;//新节点接到rear之后
    Q.rear = s;//修改表尾指针位置
}
//出队操作(带头结点)
int deQueue(LinkQueue &Q,ElemType &x){
    if(Q.front == Q.rear)
        return 0;//队空
    Node *p = Q.front->next;//p指向队头元素
    x = p->data;//将对头元素返给x
    Q.front->next = p->next;//修改队头next指针
    if(Q.rear == p)//此次是最后一个节点出队!!!
        Q.rear = Q.front;//修改rear指针
    free(p);//释放节点空间
    return 1;

}

int main(){

    LinkQueue q;
    creatQueue(q);
    enQueue(q,1);
    enQueue(q,2);
    int x;
    deQueue(q,x);
    printf("%d\n",x);
    deQueue(q,x);
    printf("%d\n",x);



    return 0;
}

顺序:

#include<stdio.h>
#include<malloc.h>
#define MAXQSIZE 100//最大队列长度
#define OK 1
#define OVERFLOW -2
typedef int QElemType;


typedef struct{
    QElemType *base;//动态分配存储空间
    int front;//头指针,若队列不空,指向队列头元素,均为下标
    int rear;//尾指针,若队列不空,指向队尾元素的下一个位置,均为下标
}myQueue;
//创建队列
int creatQueue(myQueue &Q){
    Q.base = (QElemType*)malloc(MAXQSIZE*sizeof(QElemType));//分配数组空间
    if(!Q.base)
        return -1;//储存分配失败
    Q.front = Q.rear = 0;//头尾指针置为0,队列为空
    return 1;
}
//获取队列长度
int queueLenth(myQueue Q){
    //循环队列中,(尾指针-头指针+最大长度)  再对最大长度求模
    return((Q.rear - Q.front + MAXQSIZE) % MAXQSIZE);
}
//通过采取少用一个内存位置的方法确定队空或队满
//队空Q.front == Q.rear 头尾指针一样
//队满(Q.rear+1)%MAXQSIZE == Q.front  尾指针+1等于头指针

//入队
int inQueue(myQueue &Q,QElemType e){
    if((Q.rear+1)%MAXQSIZE == Q.front){
        return -1;//队满
    }else{
        Q.base[Q.rear] = e;
        Q.rear = (Q.rear+1)%MAXQSIZE;//循环插入
    }
    return OK;
}
//出队
int outQueue(myQueue &Q,QElemType &e){
    if(Q.front == Q.rear)
        return -1;//队空
    e = Q.base[Q.front];//保存队头元素
    Q.front = (Q.front+1)%MAXQSIZE;//队头指针+1
}
//取队头元素
QElemType getHead(myQueue Q){
    if(Q.front != Q.rear){//队列不为空
        return Q.base[Q.front];
    }
}

int main(){

    myQueue que;
    creatQueue(que);
    printf("1");
    inQueue(que,1);
    inQueue(que,2);
     inQueue(que,3);
     int x = getHead(que);
     printf("%d",x);
     printf("%d",queueLenth(que));
     outQueue(que,x);
     printf("%d",queueLenth(que));
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值