数据结构day6(队列)

  • 队列是一种特殊的线性表,特殊之处在于它只允许在表的前端(front)进行删除操作,而在表的后端(rear)进行插入操作,和栈一样,队列是一种操作受限制的线性表。进行插入操作的端称为队尾,进行删除操作的端称为队头。队列中没有元素时,称为空队列。
  • 队列的数据元素又称为队列元素。在队列中插入一个队列元素称为入队,从队列中删除一个队列元素称为出队。因为队列只允许在一端插入,在另一端删除,所以只有最早进入队列的元素才能最先从队列中删除,故队列又称为先进先出(FIFO—first in first out)线性表

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;
}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值