栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

数据结构 循环队伍

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

数据结构 循环队伍

一、代码段
#include "stdio.h"
#define  MAXSIZE  100           
typedef  int  DataType;          
typedef  struct                 
{   DataType   data[MAXSIZE];   
    int  front;                 
    int  rear;                  
}SeqQueue;

void InitQueue(SeqQueue *Q)//初始化函数
{
	Q->front=Q->rear=0;//指针初始化
}

int EmptyQueue(SeqQueue *Q)//判断队空函数
{
	if(Q->front==Q->rear)//队伍为空
	   return 1;
	else
	   return 0;
}

int InQueue(SeqQueue *Q,DataType x)//入队函数
{
	if((Q->rear+1)%MAXSIZE==Q->front)//队列为满
    {
    	printf("队满,不能入队元素!");
    	return 0;//队满不能入队
    }
    else//队不为满
    {
    	Q->rear=(Q->rear+1)%MAXSIZE;//队尾指针增1
    	Q->data[Q->rear]=x;
    	return 1;
    }
}

int DeQueue(SeqQueue *Q,DataType *x)//出队函数
{
	if(EmptyQueue(Q))//调用判空函数EmptyQueue(Q),判断队列是否为空
    {
    	printf("队空,不能入队元素!");
    	return 0;
    }
    else//队不为空
    {
    	Q->front=(Q->front+1)%MAXSIZE;//队头指针增1
    	*x=Q->data[Q->front];//队头元素赋给指针x所指x所指变量
    	return 1;
    }
}

int GetFront(SeqQueue *Q,DataType *x)//取队头函数
{
	if(EmptyQueue(Q))//调用判空函数EmptyQueue(Q),判断队列是否为空
    {
    	printf("队空,不能入队元素!");
    	return 0;
    }
    else//队不为空
    {
    	
    	*x=Q->data[Q->front+1]%MAXSIZE;
    	return 1;
    }
}

void ShowQueue(SeqQueue *Q)//显示队列中元素函数
{
	int p=Q->front;
	if(p==Q->rear)
	   printf("队列为空,无元素!n");
	else
	{
		printf("从队头起队列中的各元素为:");
		while(p!=Q->rear)
	    {
	    	printf("%5d",Q->data[p+1]);
	    	p++;
	    }
	 }
}

void  Menu()                                    
{   
    printf("n                  顺序队列的各种操作");
    printf("n =================================================");  
    printf("n|               1——初始化队列                  |");
    printf("n|               2——入队操作                    |");
    printf("n|               3——出队操作                    |");
    printf("n|               4——求队头元素                  |");
    printf("n|               5——显示队中所有元素            |");
    printf("n|               0——返回                        |");
    printf("n ================================================="); 
    printf("n请输入菜单号(0-5):");   
}

main()
{
   int i,n,flag;
   SeqQueue Q;
   DataType x;
   char  ch1,ch2,a;
           
   ch1='y';
   while(ch1=='y'||ch1=='Y') 
   {  Menu();
      scanf("%c",&ch2);
      getchar();
      switch(ch2)
      {
         case  '1':          
             InitQueue(&Q);
             printf("初始化完成!");
             break;
         case  '2':
             printf("请输入要入队的元素个数:");
             scanf("%d",&n);
             printf("请输入%d个入队的整数:",n);
             for(i=0;i 
二、程序运行结果 

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/934795.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号