博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
循环队列操作实现
阅读量:4037 次
发布时间:2019-05-24

本文共 1384 字,大约阅读时间需要 4 分钟。

/*

 该实现front始终指向当前的头结点,rear始终指向尾结点的
 下一个结点
*/
#include <stdio.h>
#include <stdlib.h>
#define MAXNUM 10
#define datatype int
typedef struct queue{
        int queue[MAXNUM];
        int front;
        int rear;
}queue;
void initqueue(queue** p)
{
        *p = (queue* )malloc(sizeof(queue));
        if(*p != NULL){
                (*p)->front = 0;
                (*p)->rear = 0;
        }
}
int emptyqueue(queue** p)
{
        int val;
        val = ((*p)->front == (*p)->rear);
        return val;
}
void enqueue(queue** p, datatype x)//将x插入队尾
{
        if(((*p)->rear+1)%MAXNUM == (*p)->front){
                printf("the queue is full/n");
                return;
        }
        (*p)->queue[(*p)->rear] = x;
        (*p)->rear = ((*p)->rear+1)%MAXNUM;
}
void delqueue(queue** p, datatype* x)//删除队列头元素,用x返回
{
        if((*p)->front == (*p)->rear){
                printf("the queue is empty/n");
                return;
        }
        *x = (*p)->queue[(*p)->front];
        (*p)->front = ((*p)->front+1)%MAXNUM;
}
void firstqueue(queue** p, datatype* x)//返回队列头元素
{
        *x = (*p)->queue[(*p)->front];
}
void prqueue(queue** p)//打印队列元素
{
        int i;
        i = (*p)->front;
        while(i != (*p)->rear){
                printf("%c", (*p)->queue[i]);
                i = (i + 1) % MAXNUM;
        }
        printf("/n");
}
int main(int argc, char** argv)
{
        queue* p;
        int c, i;
        initqueue(&p);
        printf("input queue node value:/n");
/***
        while((c = getchar()) !='/n'){
                enqueue(&p, c);
        }
***/
        prqueue(&p);
        for(i = 0; i < 3; i++){
                delqueue(&p, &c);//删除一个头元素
        }
        printf("/n");
        enqueue(&p, '*');
        enqueue(&p, '$');
        prqueue(&p);
        firstqueue(&p, &c);
        printf("first queue is %c/n", c);
        exit(0);
}

转载地址:http://wvcdi.baihongyu.com/

你可能感兴趣的文章
flutter-解析json
查看>>
android中shader的使用
查看>>
java LinkedList与ArrayList迭代器遍历和for遍历对比
查看>>
drat中构造方法
查看>>
JavaScript的一些基础-数据类型
查看>>
JavaScript基础知识(2)
查看>>
转载一个webview开车指南以及实际项目中的使用
查看>>
android中对于非属性动画的整理
查看>>
一个简单的TabLayout的使用
查看>>
ReactNative使用Redux例子
查看>>
Promise的基本使用
查看>>
android给文字加边框(修改不能居中的问题)
查看>>
coursesa课程 Python 3 programming 统计文件有多少单词
查看>>
coursesa课程 Python 3 programming 输出每一行句子的第三个单词
查看>>
coursesa课程 Python 3 programming Dictionary methods 字典的方法
查看>>
Returning a value from a function
查看>>
coursesa课程 Python 3 programming Functions can call other functions 函数调用另一个函数
查看>>
coursesa课程 Python 3 programming The while Statement
查看>>
course_2_assessment_6
查看>>
coursesa课程 Python 3 programming course_2_assessment_7 多参数函数练习题
查看>>