C语言C++链队列数据结构的软件实现代码

12 篇文章 0 订阅
2 篇文章 0 订阅
/*
链队列
@hey超级巨星
*/
#include <stdio.h>
#include<stdlib.h>
#include <iostream>
using namespace std;
#define ERROR 0
#define OK 1
#define True 1
#define False 0
#define MAXSIZE 20
typedef int Status;
typedef int Elemtype;
typedef struct QNode
{
   Elemtype data;
   struct QNode* next;
}QNode, * Queueptr;
typedef struct
{
   Queueptr front;
   Queueptr rear;
}LinkQueue;
//初始化函数
Status InitQueue(LinkQueue& Q)
{
 Q.front = Q.rear = (Queueptr)malloc(sizeof(QNode));
 if (!Q.front)
  exit(0);
 Q.rear->next = NULL;
 return OK;
}
//插入函数
Status EnQueue(LinkQueue& Q, Elemtype e)
{
 if (Q.front == NULL || Q.rear == NULL)
 {
  printf("此队列为空,请插入值,谢谢");
  return ERROR;
 }
 Queueptr ptr = (Queueptr)malloc(sizeof(QNode));
 if (!ptr)
  exit(0);
 ptr->data = e;
 ptr->next = NULL;
 Q.rear->next = ptr;
 Q.rear = ptr;
 //free(ptr);
 return OK;
}
//弹出函数
Status DeQueue(LinkQueue& Q, Elemtype& e)
{
 if (Q.front == Q.rear)
 {
  printf("此队列为空,请插入值,谢谢");
  return ERROR;
 } 
 Queueptr ptr = (Queueptr)malloc(sizeof(QNode));
 if (!ptr)
  exit(0);
 ptr = Q.front->next;
 e = ptr->data;
 Q.front->next = ptr->next;
 if (ptr == Q.rear)
 {
  Q.rear = Q.front;
 }
 //free(ptr);
 return OK;
}
//判空函数
Status QueueEmpty(LinkQueue& Q)
{
 if (Q.front == Q.rear && Q.front != NULL)
 {
  return OK;
 }
 else
 {
  return ERROR;
 }
}
//遍历函数
void TravQueue(LinkQueue& Q)
{
 int tag=QueueEmpty(Q);
 if (tag == 1)
 {
  printf("此函数为空,请插入谢谢");
 }
 else
 {
  Queueptr p=Q.front->next;
  do
  {
   printf("%d\t",*p);
   p = p->next;
  } while (p->next);
 }
}
int main()
{
 printf("welcome to the system\n");
 LinkQueue Q;
 int x = InitQueue(Q);
 if (x == 1)
 {
  printf("队列创建完毕\n");
 }
 else
  exit(0);
 int sat;
 TravQueue(Q);
 //操作交互
 int loc, val;
 char m;
 while (1)
 {
  printf("\n插入请按A,弹出值请按B,遍历请C");
  //system("cls");
  cin >> m;
  switch (m)
  {
  case 'A':
   printf("开始插入,请输入插入值\n");
   scanf_s("%d", &val);
   sat = EnQueue(Q, val);
   break;
  case 'B':
   printf("开始删除\n");
   int e;
   sat=DeQueue(Q, e);
   printf("已弹出值%d\n",e);
   break;
  case 'C':
   printf("当前数组值如下");
   TravQueue(Q);
   break;
  default:
   exit(0);
  }
 }
}

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值