数据结构C-7-队列的建立与实现(顺序-动态、链式)

1.动态顺序队列

#include <stdio.h>
#include<stdlib.h>
#define MAX_SIZE 100
typedef int ElemType;
typedef struct{
    ElemType *base;//动态分配
    int front;
    int rear;
}Queue;
void InitQueue(Queue *q);
void QueueDestroy(Queue q);
void QueueClear(Queue q);
int QueueLength(Queue q);
int QueueEmpty(Queue q);
void EnQueue(Queue *q,ElemType e);
void DeQueue(Queue *q,ElemType *e);
int GetHead(Queue *q,ElemType *e);
void Display(Queue q);

int main(void){
    Queue q;ElemType e;int len;
    InitQueue(&q);
    Display(q);
    for(int i=0;i<5;i++){
        int a=2*i+1;
        EnQueue(&q,a);
    }
    Display(q);
    len=QueueLength(q);
    printf("队列长度为%d\n",len);
    DeQueue(&q,&e);
    printf("出队的元素为%d\n",e);
    Display(q);
    len=QueueLength(q);
    printf("队列长度为%d\n",len);
    return 0;
}
void InitQueue(Queue *q){
    q->base=(ElemType*)malloc(MAX_SIZE*sizeof(ElemType));
    if(!q->base)
        exit(-1);
    q->front=q->rear=0;
}
void QueueDestroy(Queue q){
    free(q.base);
    q.base=NULL;
}
void QueueClear(Queue q){
    q.front=0;
    q.rear=0;
}
int QueueLength(Queue q){
    return (q.rear-q.front+MAX_SIZE)%MAX_SIZE;
}
int QueueEmpty(Queue q){
    if(q.front==q.rear)
        return 1;
    return 0;
}
void EnQueue(Queue *q,ElemType e){
    if((q->rear+1)%MAX_SIZE==q->front)
        exit(-1);
    q->base[q->rear]=e;
    q->rear=(q->rear+1)%MAX_SIZE;
}
void DeQueue(Queue *q,ElemType *e){
    if(q->front==q->rear)
        exit(-1);
    *e=q->base[q->front];
    q->front=(q->front+1)%MAX_SIZE;
}
int GetHead(Queue *q,ElemType *e){
    if(QueueEmpty(*q))
        return -1;
    *e=q->base[q->front];
    return *e;
}
void Display(Queue q){
    if(q.front==q.rear)
        printf("队列为空\n");
        else{
             int i=q.front;
        printf("这个队列为:");
        while(i!=q.rear){
            printf("%d ",q.base[i]);
            i=(i+1)%MAX_SIZE;
        }
        printf("\n");
    }
}

2.链式

#include<stdio.h>
#include<stdlib.h>
#define OK 1
#define ERROR -1
#define TRUE 1
#define FALSE -1
typedef int Status;
typedef int ElemType;
typedef struct QNode{
    ElemType data;
    struct QNode *next;
}QNode;//结点定义
typedef struct Link_Queue{
    QNode *front,*rear;
}Link_Queue;//链队
void InitQueue(Link_Queue *q);
Status DestroyQueue(Link_Queue q);
void ClearQueue(Link_Queue q);
int QueueEmpty(Link_Queue q);
int QueueLength(Link_Queue q);
void EnQueue(Link_Queue *q,ElemType e);
void EnQueue_1(Link_Queue *q,ElemType e);
void DeQueue(Link_Queue *q,ElemType *e);
void GetHead(Link_Queue q,ElemType *e);
void Display(Link_Queue q);

int main(void){
    Link_Queue q;
    InitQueue(&q);
    ElemType e,b,a;
    int len=0;
    Display(q);
    for(int i=0;i<5;i++){
        a=2*i+1;
        EnQueue(&q,a);
    }
    Display(q);
    GetHead(q,&e);
    len=QueueLength(q);
    printf("队列长度为%d\n",len);
    DeQueue(&q,&b);
    Display(q);
    GetHead(q,&e);
    return 0;
}
void InitQueue(Link_Queue *q){
    q->front=q->rear=(QNode*)malloc(sizeof(QNode));
    if(!q->front)
        exit(-1);
    q->front->next=NULL;
}
Status DestroyQueue(Link_Queue q){
    QNode *p=NULL;
    if(q.front==q.rear){
        printf("当前链队列为空,无需完成销毁操作\n");
        return ERROR;
    }
    while(q.front->next){
        p=q.front->next;
        q.front->next=p->next;
        if(QueueLength(q)==0)
            break;
        free(p);
    }
    printf("销毁成功\n");
    return OK;
}
void ClearQueue(Link_Queue q){
    DestroyQueue(q);
    InitQueue(&q);
}
int QueueEmpty(Link_Queue q){
    if(q.front==q.rear)
        return OK;
    return ERROR;
}
int QueueLength(Link_Queue q){
    QNode *p;
    int len=0;
    if(q.front==q.rear)
        return ERROR;
    else
        p=q.front->next;
    while(p){
        len++;
        p=p->next;
    }
    return len;
}
void EnQueue(Link_Queue *q,ElemType e){//带头结点
    QNode *p;
    p=(QNode*)malloc(sizeof(QNode));
    if(!p){
        printf("插入元素失败\n");exit(-1);
    }
    p->data=e;
    p->next=NULL;//生成新结点
    q->rear->next=p;
    q->rear=p;
}
void EnQueue_1(Link_Queue *q,ElemType e){//不带头结点
    QNode *p;
    p=(QNode*)malloc(sizeof(QNode));
    if(!q)
        exit(-1);
    p->data=e;p->next=NULL;
        if(q->front==NULL){//边界操作
            q->front=p;q->rear=p;
            return ;
        }
        q->rear->next=p;
        q->rear=p;
}
void DeQueue(Link_Queue *q,ElemType *e){
    QNode *p;
    if(q->front==q->rear)
        exit(-1);
    p=q->front->next;
    *e=p->data;
    q->front->next=p->next;
    if(q->rear==p)
        q->rear=q->front;
    free(p);
    printf("出队的元素为%d\n",*e);
}
void GetHead(Link_Queue q,ElemType *e){//取第一个元素
    if(q.front==q.rear)
        exit(-1);
    *e=q.front->next->data;
    printf("头元素为:%d\n",*e);
}
void Display(Link_Queue q){
    if(q.front==q.rear)
        printf("队列为空\n");
    else {
        printf("该队列为:");
        QNode *p=q.front->next;
        while(p){
            printf("%d ",p->data);
            p=p->next;
        }
    printf("\n");
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Github下载地址:https://github.com/XLAccount/MiaoBo 项目详解地址:http://www.code4app.com/blog-843201-350.html 快速集成RTMP的视频推流教程:http://www.code4app.com/blog-843201-315.html ffmpeg常用命令操作:http://www.code4app.com/blog-843201-326.html #关于IJKMediaFramework/IJKMediaFramework.h找不到的问题,下载后直接拉到项目中即可 下载地址:https://pan.baidu.com/s/1boPOomN 密码::9yd8 #BUG修复: 解决登录程序偶尔崩溃,修复轮播图片和页面控制器叠加等问题,修复新浪授权登录 (2016.9.7) 解决程序运行中偶尔崩溃问题,解决连续下拉刷新崩溃问题,优化代码 (2016.9.8) 优化直播页面,减少不必要的性能消耗,增加用户体验 (2016.9.11) 适配5s以上的机型除了6sPlus和6Plus延迟较大外,其余延迟都较小,网速好的话可以忽略不计 (2016.9.12) 新版本极大优化程序性能,修复关注数据异常等小问题,重新布局热门页面,减少因反复加载带来的性能消耗 (2016.9.13) 增加个人中心页面,采用下拉放大图片 ➕ 波纹效果 (2016.9.14) ![image text](https://github.com/XLAccount/ALLGIFS/blob/master/psb.gif) 展示图片 ![image](https://github.com/XLAccount/ALLGIFS/blob/master/psb-1.gif) 展示图片 ![image text](https://github.com/XLAccount/ALLGIFS/blob/master/psb-2.gif) 展示图片 ![image text](https://github.com/XLAccount/ALLGIFS/blob/master/psb-3.gif) 展示图片 感谢大神Monkey_ALin http://www.jianshu.com/users/9723687edfb5/latest_articles 的demo支持
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值