#include<process.h>
#include<iostream.h>
#include<stdlib.h>#define MaxSize 100
typedef int ElemType
typedef struct
{
ElemType data[MaxSize]
int front
int rear
}CircSeqQueue//顺序循环队列的初始化
void QueueInitial(CircSeqQueue *pQ)
{
//创建一个又指针pQ所指向的空顺序循环队列
pQ->front=pQ->rear=0
}//顺序循环队列判空
int IsEmpty(CircSeqQueue *pQ)
{//顺序循环队列为空时返回1,否则返回0
return pQ->front==pQ->rear
}//顺序循环队列判满
int IsFull(CircSeqQueue *pQ)
{//循环队列为满时返回1,否则返回0
return (pQ->rear+1)%MaxSize==pQ->front
}//元素进队
void EnQueue(CircSeqQueue *pQ, ElemType e)
{//若队列不满,则元素e进队
if(IsFull(pQ))//队列已满,退出
{
printf("队列溢出!\n")
exit(1)
}
pQ->rear=(pQ->rear+1)%MaxSize//队尾指针后移
pQ->data[pQ->rear]=e
}//元素出队
ElemType DeQueue(CircSeqQueue *pQ)
{//若循环队列不为空,则删除队头元素,并返回它的值
if(IsEmpty(pQ))//队列为空,退出
{
printf("空队列!\n")
exit(1)
}
pQ->front=(pQ->front+1)%MaxSize//队头指针后移
return pQ->data[pQ->front]
}//取队头元素
ElemType GetFront(CircSeqQueue *pQ)
{//若队列不为空,则返回队头元素的值
if(IsEmpty(pQ))
{
printf("空队列!\n")
exit(1)
}
return pQ->data[(pQ->front+1)%MaxSize]
}//循环队列置空
void MakeEmpty(CircSeqQueue *pQ)
{//将由指针pQ所指向的队列变为孔队
pQ->front=pQ->rear=0
} void output()
{
int i
for(i=0i<32i++)
printf("+")
printf("\n")
printf("1.元素进队\n")
printf("2.元素出队\n")
printf("3.队首元素读取\n")
printf("4对列制空\n")
printf("5.退出\n")
for(i=0i<32i++)
printf("+")
printf("\n")
} void main()
{
int k,m,n,i
CircSeqQueue *pQ
ElemType e
pQ=new CircSeqQueue
QueueInitial(pQ)
output()
while(k)
{
printf("请选择:1-5\n")
scanf("%d",&m)
switch(m)
{
case 0:return
case 1:
printf("请输入入队元素的个数:\n")
scanf("%d",&n)
printf("输入元素,入队\n")
for(i=0i<ni++)
{
scanf("%d",&e)
EnQueue(pQ,e) } break
case 2:
{
printf("请输入出队元素的个数\n")
scanf("%d",&n)
for(i=0i<ni++)
{
e=DeQueue(pQ)
printf("%d\n",e)
} break
}
case 3:
{
printf("取出队首元素\n")
e=GetFront(pQ)
printf("%d\n",e)
break
}
case 4:
{
printf("队列置空\n")
MakeEmpty(pQ)
printf("\n")
break
}
default:return
}
printf("是否继续(1|0)\n")
scanf("%d",&k)
if(!k) return
}
}看下吧!不是很完善!
(1)编写一个程序,实现顺序环形队列的各种基本运算,并在此基础上设计一个主程序完成如下功能:(1)初始化队列q;
(2)判断队列q是否非空;
(3)依次进队元素100、909、44、8;
(4)出队一个元素,输出该元素;
(5)输出队列q的元素个数;
(6)依次进队元素-67、55、99、70;
(7)输出队列q的元素个数;
#include<stdio.h>
#include<malloc.h>
#define QUEUE_INIT_SIZE 100
#define QUEUEINCREMENT 10
#define OK 1
#define TURE 1
#define FALSE 0
#define ERROR 0
#define INFEASIBLE -1
#define OVERFLOW -2
typedef int Status
typedef int QElemType
typedef struct
{
QElemType *base
int front
int rear
}SqQueue
Status InitQueue(SqQueue &Q)
{
Q.base=(QElemType *)malloc
(QUEUE_INIT_SIZE*sizeof(QElemType))
if(!Q.base)
exit(OVERFLOW)
Q.front=Q.rear=0
return OK
}
int QueueNumElem(SqQueue Q)
{
return (Q.rear-Q.front)%QUEUE_INIT_SIZE
}
Status EnQueue(SqQueue &Q,QElemType e)
{
if((Q.rear+1)%QUEUE_INIT_SIZE==Q.front)
return ERROR
Q.base[Q.rear]=e
Q.rear=(Q.rear+1)%QUEUE_INIT_SIZE
return OK
}
SqQueue DeQueue(SqQueue Q,int e)
{
int i
if(Q.front==Q.rear)
printf("队为空!\n")
for(i=ei<Q.reari++)
Q.base[i]=Q.base[i+1]
Q.rear--
return Q
}
int main()
{
SqQueue Q,Q1
static int qele=0
int i,j=0,k=0,m=0
static int frontd=Q.front
i=InitQueue(Q)
if(i==1)
printf("队初始化成功!!\n")
else
printf("队初始化失败!!\n")
if(Q.front!=Q.rear)
printf("队不为空!!\n")
else
printf("队为空L!!\n")
printf("输入数据(END of '9999'):")
scanf("%d",&qele)
while(qele!=9999||Q.rear==Q.front)
{
EnQueue(Q,qele)
scanf("%d",&qele)
}
frontd=Q.front
while(Q.rear!=Q.front)
{
printf(" %d ",Q.base[Q.front])
Q.front++
j++
}
printf("\n")
Q.front=frontd
printf("输入要出队的元素:")
scanf("%d",&j)
while(Q.front!=Q.rear)
{
if(Q.base[Q.front]==j)
{
printf("%d\n",Q.base[Q.front])
Q=DeQueue(Q,Q.front)
m++
break
}
Q.front++
}
Q.front=frontd
while(Q.front!=Q.rear)
{
printf(" %d ",Q.base[Q.front])
Q.front++
}
printf("\n")
Q.front=frontd
printf("队的长度:%d\n",Q.rear-Q.front)
printf("输入数据(END of '9999'):")
scanf("%d",&qele)
while(qele!=9999||Q.rear==Q.front)
{
EnQueue(Q,qele)
scanf("%d",&qele)
}
Q.front=frontd
printf("队的长度:%d\n",Q.rear-Q.front)
Q.front=frontd
printf("出队顺序:")
while(Q.rear!=Q.front)
{
printf(" %d ",Q.base[Q.front])
Q=DeQueue(Q,Q.front)
m++
}
printf("end\n")
Q.front=0
Q.rear=m
while(Q.rear!=Q.front)
{
free(Q.base)
//Q.base++
Q.front++
if(Q.rear-1==Q.front)
printf("队已经释放!\n")
}
return 0
}
#include <stdio.h>#include <malloc.h>
#define Maxqsize 5
typedef char ElemType
typedef struct
{
ElemType elem[Maxqsize]
int front,rear
}SqQueue
void InitQueue(SqQueue *&q)
{
q=(SqQueue *)malloc (sizeof(SqQueue))
q->front=q->rear=0
}
void ClearQueue(SqQueue *&q)
{
free(q)
}
int QueueLength(SqQueue *q)
{
return (q->rear-q->front+Maxqsize)%Maxqsize
}
int QueueEmpty(SqQueue *q)
{
return(q->front==q->rear)
}
int enQueue(SqQueue *&q,ElemType e)
{
if ((q->rear+1)%Maxqsize==q->front)
return 0
q->rear=(q->rear+1)%Maxqsize
q->elem[q->rear]=e
return 1
}
int deQueue(SqQueue *&q,ElemType &e)
{
if (q->front==q->rear)
return 0
q->front=(q->front+1)%Maxqsize
e=q->elem[q->front]
return 1
}
void main()
{
ElemType e
SqQueue *q
printf("(1)初始化队列Q\n")
InitQueue(q)
printf("(2)依次进队列元素A,B,C\n")
if (enQueue(q,'A')==0) printf("队满,不能进队\n")
if (enQueue(q,'B')==0) printf("队满,不能进队\n")
if (enQueue(q,'C')==0) printf("队满,不能进队\n")
printf("(3)队列为%s\n",(QueueEmpty(q)?"空":"非空"))
if (deQueue(q,e)==0)
printf("队空,不能出队\n")
else
printf("(4)出队一个元素%c\n",e)
printf("(5)队列Q的元素个数:%d\n",QueueLength(q))
printf("(6)依次进队列元素D,E,F\n")
if (enQueue(q,'D')==0) printf("队满,不能进队\n")
if (enQueue(q,'E')==0) printf("队满,不能进队\n")
if (enQueue(q,'F')==0) printf("队满,不能进队\n")
printf("(7)队列Q的元素个数:%d\n",QueueLength(q))
printf("(8)出队列序列:")
while (!QueueEmpty(q))
{ deQueue(q,e)
printf("%c ",e)
}
printf("\n")
printf("(9)释放队列\n")
ClearQueue(q)
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)