⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 duilie.cpp

📁 数据结构创建一个队列的程序源码,用c语言实现对其编程
💻 CPP
字号:
// duilie.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include<iostream.h>
#include<stdlib.h>
#define MaxSize  100
#define OK 1
#define ERROR 0
#include <malloc.h>

typedef int Qelemtype;

typedef struct
{
 Qelemtype *base;  //指向队列的存储空间;
 int front;  //指向队头元素;
 int rear;  //指向队尾元素的下一位置;
}SqQueue;

int InitQueue(SqQueue &Q)
{//队列的初始化;
 Q.front=Q.rear=0;
 Q.base=(Qelemtype *)malloc(MaxSize*sizeof(Qelemtype));
 if(!Q.base)
  exit(1);
 Q.base[Q.front]='\0';
 return 1;
}

int QueueLength(SqQueue Q)
{//计算队列的长度;
 return (Q.rear-Q.front+MaxSize)%MaxSize;
}

void CreatQueue(SqQueue &Q,Qelemtype tag)
{//输入一些数据,建立一个队列,输入tag表示结束输入;
 int i;
 cout<<"Please Input The Data Of Queue"<<endl
  <<"Input 0 Means End "<<endl;
 cin>>i;
 while(i!=tag)
 {
  if(QueueLength(Q)==MaxSize)
  {
   cout<<"EnQueue Error "<<endl;
   return ;
  }
  Q.base[Q.rear++]=i;
  cout<<"Please Input The Data Of Queue"<<endl
   <<"Input 0 Means End "<<endl;
  cin>>i;
 }
 return ;
}

void EnQueue(SqQueue &Q, Qelemtype x)
{//入队; 
 if(QueueLength(Q)==MaxSize)
 {
  cout<<"EnQueue Error "<<endl;
  return ;
 }
 Q.base[Q.rear++]=x;
}

void DelQueue(SqQueue &Q, Qelemtype &x)
{//出队;
 if(Q.front==Q.rear)
 {
  cout<<"DelQueue Error ."<<endl;
  return ;
 }
 x=Q.base[Q.front++];
}

void DispQueue(SqQueue Q)
{//输出队列的所有元素;
 int i=0,j=Q.front;
 while(i<QueueLength(Q))
 {
  cout<<Q.base[j]<<endl;
  j++;
  i++;
 }
}

int main(int argc, char* argv[])
{
	SqQueue Q;
	int x;
	InitQueue(Q);
	CreatQueue(Q,0);
	EnQueue(Q,1);
	DelQueue(Q,x);
	cout<<x<<endl;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -