head.h
#ifndef __QQ_H__
#define __QQ_H__
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX 10
typedef int datedef;
typedef struct
{
char name[20];
int age;
int score;
} student;
typedef struct
{
datedef date[MAX]; // 数据域
int head; // 指针域
int rear;
} *queue, Queue;
queue creat(); // 创建新队列
void output(queue L); // 遍历
int in(queue L); // 入队
void output(queue L); // 遍历
int out(queue L); // 出队
//**********循环队列×××××××××//
int r_in(queue L); // 入队
void r_output(queue L); // 遍历
int r_out(queue L); // 出队
#endif
fun.c
#include "head.h"
queue creat() // 创建新队列
{
queue L = (queue)malloc(sizeof(Queue));
if (L == NULL)
{
return NULL;
}
L->head = 0;
L->rear = 0;
for (int i = 0; i < MAX; i++)
{
L->date[i] = 0; // 初始化
}
return L;
}
int in(queue L) // 入队
{
if (L == NULL || L->rear == MAX)
{
printf("入队失败\n");
return -1;
}
int n;
printf("你要入队的元素个数:");
scanf("%d", &n);
for (int i = 0; i < n; i++)
{
printf("你要入队的元素是:");
scanf("%d", &L->date[L->rear++]);
// L->rear++;
}
return 0;
}
void output(queue L) // 遍历
{
if (L->head == L->rear || L == NULL)
{
printf("遍历失败\n");
}
printf("输出的元素是:\n");
int i = L->head;
while (i != L->rear)
{
printf("%d\t", L->date[i]);
i++;
}
puts("");
printf("该队列有%d个元素。\n", L->rear - L->head);
}
int out(queue L) // 出队
{
if (L->head == L->rear || L == NULL)
{
printf("出队失败\n");
return -1;
}
printf("将要出队的元素是:%d\n", L->date[L->head]);
char a[10];
while (1)
{
printf("是否确认出队?yes/no\t");
scanf("%s", a);
puts("");
if (strcmp(a, "yes") == 0)
{
printf("\t%d\t已出队\n", L->date[L->head]);
L->head++;
return 0;
}
}
}
//************循环队列×××××××××××××××××××//
int r_in(queue L) // 入队
{
if (L == NULL || (L->rear + 1) % MAX == L->head)
{
printf("入队失败\n");
return -1;
}
int n;
printf("你要入队的元素个数:");
scanf("%d", &n);
for (int i = 0; i < n; i++)
{
if ((L->rear + 1) % MAX == L->head)
{
printf("入队失败\n");
return -1;
}
printf("你要入队的元素是:");
scanf("%d", &L->date[L->rear]);
L->rear = (L->rear + 1) % MAX;
}
return 0;
}
void r_output(queue L) // 遍历
{
if ((L->head + 1) % MAX == L->rear || L == NULL)
{
printf("遍历失败\n");
}
printf("输出的元素是:\n");
int i = L->head;
while (i != L->rear)
{
printf("%d\t", L->date[i]);
i = (i + 1) % MAX;
}
puts("");
printf("该队列有%d个元素。\n", (L->rear + MAX - L->head) % MAX);
}
int r_out(queue L) // 出队
{
if (L->head == L->rear || L == NULL)
{
printf("出队失败\n");
return -1;
}
printf("将要出队的元素是:%d\n", L->date[L->head]);
char a[10];
while (1)
{
printf("是否确认出队?yes/no\t");
scanf("%s", a);
puts("");
if (strcmp(a, "yes") == 0)
{
printf("\t%d\t已出队\n", L->date[L->head]);
L->head++;
}
else
{
printf("出栈未执行\n");
return 0;
}
}
}
main.c
#include "head.h"
int main(int argc, const char *argv)
{
queue L = creat();
int n;
datedef s, e;
r_in(L);
r_output(L);
r_out(L);
r_output(L);
r_in(L);
r_output(L);
r_out(L);
r_output(L);
free(L);
L = NULL;
}