循环队列出队入队运算

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include<stdio.h>
#define MAXSIZE 100
typedef int datatype;
typedef struct node
{
    datatype data[MAXSIZE+1];
    int rear,head;
}sequeue;
sequeue *sq,SQ;
void CIR_INITQUEUE(sequeue *sq)
{
    sq->head=MAXSIZE-1;
    sq->rear=MAXSIZE-1;
    printf("初始化成功\n");
}
int CIR_EMPTY(sequeue *sq)
{
    if(sq->rear==sq->head)
    return 1;
    else return 0;
}

int CIR_GETFRONT(sequeue *sq)//取出循环队列的队头元素 (位置) 
{
    if(CIR_EMPTY(sq))
    {
        printf("队列为空不能执行出队运算");
        return 0
    }
    else
    {
        return((sq->head+1)%MAXSIZE);
    }
}

int CIR_ENQUEUE(sequeue *sq,datatype x) //循环队列的入队运算 
{
    if(sq->head==(sq->rear+1)%MAXSIZE)
    {
        printf("队列已满");
        return 0;
    }
    else
    {
        sq->rear=(sq->rear+1)%MAXSIZE;
        sq->data[sq->rear]=x;
        return 1;
    }
}
datatype CIR_DEQUEUE(sequeue *sq)
{
    if(CIR_EMPTY(sq))
    {
        printf("队列为空");
        return 0;
    }
    else
    {    sq->head=(sq->head+1)%MAXSIZE;
        printf("%d",sq->data[sq->head]);
        return (sq->data[sq->head]);
    }
}
int main()
{sq=&SQ;
datatype x=5;
void CIR_INITQUEUE(sequeue *sq)
int CIR_EMPTY(sequeue *sq);
int CIR_GETFRONT(sequeue *sq);
int CIR_ENQUEUE(sequeue *sq,datatype x);
datatype CIR_DEQUEUE(sequeue *sq);
CIR_INITQUEUE(&SQ);
scanf("%d",&x);
CIR_ENQUEUE(&SQ,x);
CIR_DEQUEUE(&SQ);
    return 0;
}