‘壹’ c语言数据结构循环队列问题
主要错在InitQueue函数里面。当声明一个指针的时候,除了指针本身占用的内存以外,是不会分配具体的内存空间的。也就是说,如果只是CircQueue *q;声明指针q,然后直接使用它的内部成员q->front,q->rear = 0是不合法的。实际上,在Visual Studio里面是编译不通过的。
修改后运行截图
CircQueue*InitQueue(){
CircQueue*q=(CircQueue*)malloc(sizeof(CircQueue));
q->front=0;
q->rear=0;
returnq;
}
‘贰’ 谁能告诉一下用循环队列实现打印杨辉三角形的C语言代码
#include
<stdio.h>
#include
<iostream.h>
#include
"queue.h"
void
YANGHUI(int
n)
{
SeqQueue
q(n+2);
//队列初始化p121
q.EnQueue(1);
q.EnQueue(1);
int
s
=
0,
t;
for
(int
i
=
1;
i
<=
n;
i++)
{
//逐行计算
cout
<<
endl;
q.EnQueue(0);
for
(int
j
=
1;
j
<=
i+2;
j++)
{
//下一行
q.DeQueue(t);
q.EnQueue(s
+
t);
s
=
t;
if
(j
!=
i+2)
cout
<<
s
<<
'
';
}
}
}
课件地址:http://wenku..com/view/cb953d5e804d2b160b4ec0ba.html
3.3.4
队列的应用:打印杨辉三角形。
‘叁’ C语言,请用数组作个循环队列
a#include
"Stdio.h"
#include
<stdlib.h>
#include
"Conio.h"
#include
"malloc.h"
#define
TRUE
1
#define
FALSE
0
#define
INFEASIBLE
1
#define
OVERFLOW
-2
#define
OK
1
#define
ERROR
0
#define
MAXQSEZE
100
/*最大队列长度*/
typedef
int
QElemType;
typedef
int
Status;
typedef
struct{
QElemType
*base;
/*初始化的动态分配存储空间*/
int
front;
/*头指针,若队列不空,指向队列头元素*/
int
rear;
/*尾指针,若队列不空,指向队列尾元素的下一位置*/
}SqQueue;
Status
Queuelength(SqQueue
*Q){
/*构造一个空的循环队列*/
Q->base=(QElemType
*)malloc(MAXQSEZE*sizeof(SqQueue));
if(!Q->base)
exit(OVERFLOW);
/*存储分配失败*/
Q->front=Q->rear=0;
return
OK;
}
Status
EnQueue(SqQueue
*Q,QElemType
e){
/*插入元素e为Q的新的队尾元素*/
if((Q->rear+1)%MAXQSEZE==Q->front)
return
ERROR;/*队列满*/
Q->base[Q->rear]=e;
Q->rear=(Q->rear+1)%MAXQSEZE;
return
OK;
}
Status
DeQueue(SqQueue
*Q,QElemType
*e){
/*若队列不空,则删除Q的队头元素,用e返回其值*/
/*否则返回ERROR*/
if(Q->front==Q->rear)
return
ERROR;
*e=Q->base[Q->front];
Q->front=(Q->front+1)%MAXQSEZE;
return
OK;
}
Status
GetHead(SqQueue
*Q,QElemType
*e){
/*队列不为空用e返回Q的头元素,并返回OK,否则返回ERROR*/
if(Q->front==Q->rear)
return
ERROR;
*e=Q->base[Q->front];
return
OK;
}
Status
QueueEmpty(SqQueue
*Q){
/*队列为空时返回OK否则返回FALSE*/
if(Q->front==Q->rear)
return
OK;
return
FALSE;
}
void
yanghuiTriangle(int
n){
/*打印输出杨辉三角的钱n(n>0)行*/
SqQueue
Q;
char
ch='
';
int
i,k;
QElemType
s,e;
FILE
*fq;
if((fq=fopen("output.txt","w"))==NULL){
/*打开写入文件*/
printf("error
on
open\n");
exit
(1);
}
Queuelength(&Q);
/*创建空的队列*/
for(i=1;i<n;i++)
{
printf("
");
fputc(ch,fq);}
/*输出n个空格以保持三角形的队形*/
printf("1\n");
fprintf(fq,"%d\n",1);
EnQueue(&Q,0);
/*添加第一行末尾的行分界0并入队*/
EnQueue(&Q,1);
/*第二行的两个1值入队列*/
EnQueue(&Q,1);
k=2;
while(k<n){
/*通过循环队列输出第2行到第n-1行的值*/
for(i=1;i<=n-k;i++)
{printf("
");
fputc(ch,fq);}
/*输出n-k个空格以保持三角形*/
EnQueue(&Q,0);
do{
/*输出第k行,计算第k+1行*/
DeQueue(&Q,&s);
GetHead(&Q,&e);
if(e)
/*若e为非行分界值0,则打印输出e的值,并加一空格*/
{printf("%d
",e);
fprintf(fq,"%d%c",e,ch);
}
else
{
printf("\n");
fputc('\n',fq);}
/*回车换行,为下一行输出做准备*/
EnQueue(&Q,s+e);
/*计算所得抵k+1行的值入队列*/
}while(e!=0);
k++;
}
DeQueue(&Q,&e);
/*行界值“0“出队列*/
while(!QueueEmpty(&Q)){
/*单独处理第n行的值的输出*/
DeQueue(&Q,&e);
{
printf("%d
",e);
fprintf(fq,"%d%c",e,ch);
}
}
}
int
main(void)
{
FILE
*
fp;
QElemType
n;
if((fp=fopen("input.txt","r"))==NULL){
/*打开写入文件*/
printf("error
on
open\n");
exit
(1);
}
fscanf(fp,"%d",&n);
/*读入n*/
fclose(fp);
yanghuiTriangle(n);
getch();
return
0;
}
用一个文件输入一个N,这个数位杨辉三角的行数上面是用循环队列做的,你看看
‘肆’ C语言循环队列
简单的程序,先构造一个循环链表,包括N个缓寸区和一个额外的区域(便于队列的操作).
输入1时,消费者输出.
输入其他数字时,生成者输入.
#include <stdio.h>
#define N 10
struct queue
{
int data;
queue *front;
};
int main()
{
int i, choice;
queue *head;
queue *tail;
queue *temp;
head = tail = new queue;
temp = new queue;
temp->front = head;
tail->front = temp;
for(i=1; i<N; i++)
{
temp->front = new queue;
temp = temp->front;
temp->front = head;
}
tail = temp;
while(true)
{
scanf("%d", &choice);
if(choice == 1) //consumer
{
if(head == tail->front) break;
printf("消费:%d\n", head->data);
head = head->front;
}
else //proctor
{
if(tail->front->front == head) break;
printf("生产:\n");
scanf("%d", &tail->front->data);
tail = tail->front;
}
}
return 0;
}
‘伍’ c语言循环队列
队列是一种特殊的线性表,循环队列是将向量空间想象为一个首尾相接的圆环。
队列是一个特殊的线性表,它的特殊之处在于它只允许表的前面的操作删除,而在表的后面的操作插入,就像堆栈一样,队列100是一个线性表,具有有限的操作。
循环队列就是把向量空间想象成一个首尾相连的环,把这样的向量称为循环向量。存储学位的队列称为循环队列。
在顺序队列中,当指向队列末端的指针到达数组的上界时,不能有更多的队列条目,但数组中仍然有一个空位置。这称为“假溢出”。
(5)c语言循环队列代码扩展阅读:
判断满队列状态:
1.计数;你通常使用count
Count等于队列的MAXSIZE
2.国旗int
Queueinflag=1Queueoutflag=0
= && flag = = 0的前面和后面
3.放一个存储应答单元为空,不存储数据
后面+1=前面
注:(不)顺序结构,SeqQueuemyQueue;
‘陆’ C语言编程题,实现一个顺序存储的循环队列。
#include<stdio.h>
#include<stdbool.h>
#include<malloc.h>
typedef
int
typedata;
struct
node
{
struct
node
*prev,
*next;
typedata
data;
};
typedef
struct
node
node;
typedef
struct
node*
link;
//
============init_head===============
//
//头节点的初始化
link
init_head(void)
{
link
head
=
(link)malloc(sizeof(node));
if(head
!=
NULL)
{
head->prev
=
head->next
=
head;
}
return
head;
}
//
============newnode
================
//
//创建新节点
link
newnode(typedata
data)
{
link
new
=
(link)malloc(sizeof(node));
if(new
!=
NULL)
{
//前趋指针和后趋指针都指向自己
new->prev
=
new->next
=
new;
new->data
=
data;
}
return
new;
}
//
=================is_empty================
//
bool
is_empty(link
head)
{
//为空时,头节点的前趋指针和后趋指针都指向head(头节点)
if((head->next==head)
&&
(head->prev==head))
return
true;
return
false;
}
//
================insert_tail
==================
//
void
insert_tail(link
head,
link
new)
{
if(is_empty(head))
{
//第一个节点插入
head->next
=
head->prev
=
new;
new->next
=
new->prev
=
head;
return
;
}
//除了第一个节点插入
new->prev
=
head->prev;
new->next
=
head;
new->prev->next
=
new;
new->next->prev
=
new;
}
//
================show
====================
//
void
show(link
head)
{
//为空时,直接返回
if(is_empty(head))
return
;
//遍历整个链
link
tmp
=
head->next;
while(tmp
!=
head)
{
printf("%d\t",
tmp->data);
tmp
=
tmp->next;
}
printf("\n");
}
//
==============insert_opint
===============
//
void
insert_opint(link
end_node,
link
p)
{
p->prev
=
end_node;
p->next
=
end_node->next;
end_node->next->prev
=
p;
end_node->next
=
p;
}
//
================insertion_sort===========
//
//顺序排序
void
insertion_sort(link
head)
{
if(is_empty(head))
return;
//把队列分拆,头节点和第一个节点为一个已排序的队列,
//其他的节点逐个比较已排序队列插
link
p
=
head->next->next;
head->prev->next
=
NULL;
head->next->next
=
head;
head->next->prev
=
head;
head->prev
=
head->next;
while(p
!=
NULL)
{
link
end_node
=
head->prev;
if(p->data
>
end_node->data)
{
link
tmp
=
p;
p
=
p->next;
insert_tail(head,
tmp);
}
else
{
while(end_node!=head
&&
p->data<end_node->data)
end_node
=
end_node->prev;
link
tmp
=
p;
p
=
p->next;
insert_opint(end_node,
tmp);
}
}
}
int
main(void)
{
link
head
=
init_head();
if(head
==
NULL)
{
printf("falure\n");
return
0;
}
typedata
data;
while(1)
{
if(scanf("%d",
&data)
!=
1
)
break;
link
new
=
newnode(data);
if(new
==
NULL)
{
printf("falure\n");
return
0;
}
insert_tail(head,
new);
show(head);
}
printf("the
figure
is:\n");
show(head);
insertion_sort(head);
show(head);
return
0;
}
‘柒’ 求用循环队列解决约瑟夫环问题的C语言代码,急,速度!!!!!!!
他们都是用链表做的。。我没用链表做。。。很简单的写了一个。。。
#include<iostream.h>
voidFmade(intx,inty,intz);
voidmain()
{
inta,b,c;
//ti,j,k;
//taa[100],b[100];
cout<<"请输入总人数:";
cin>>a;
cout<<endl<<"请输入开始位子:";
cin>>b;
cout<<endl<<"请输入步长:";
cin>>c;
Fmade(a,b,c);
}
voidFmade(intx,inty,intz)
{
inti,j=0,k=0;
intaa[100],bb[100];
intstart;
aa[0]=0;
for(i=1;i<=x;i++)
{
aa[i]=i;
}
start=y;
while(j<x)
{
while(start<=x)
{
if(aa[start]!=0)
{
k++;
}
if(k==z)
{
bb[j]=aa[start];
aa[start]=0;
j++;
k=0;
}
start++;
}
start=1;
}
cout<<"出列顺序为:";
for(i=0;i<x;i++)
{
cout<<bb[i]<<"";
}
}
说明下:因为这个数组只定义了100个字节。其中第一字节没有用。。所以只能计算99以内的出列。。。包括99,如果需要扩大计算范围,只需要扩大数组长度就行了。。。也就Fmade函数的定义。。。
最够插入运算的答案的图片:
‘捌’ 求一个C语言 循环队列的插入 完整程序
(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=e;i<Q.rear;i++)
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;
}
‘玖’ C语言用数组实现循环队列的入队出队
//定义一个int型数组que,长度为N(常量切大于2).
intque[N];
intrear=0,front=0;//队尾队头
判断队列已满:
if((front+1)%N==rear%N)//成立则队列已满
判断队列为空
if((rear==front))//成立则队列空
入队(一般在入队前判断队列是否已满)
//将val入队
que[front++]=val;
front%=N;
出队(一般在出队前判断队列是否为空)
rear=(rear+1)%N;
下一个要出队的元素(一般先判断是否为空)
que[rear];
‘拾’ 急:c语言,循环队列:
#include<stdio.h>
#include<stdlib.h>
#define MAXSIZE 10
typedef struct cqueue{
int *base;
int front;
int rear;
}CQueue;
CQueue *InitQueue(){
CQueue *p=(CQueue*)malloc(sizeof(CQueue)); //p要初始化的
p->base=(int *)malloc(MAXSIZE*4);
p->front=p->rear=0;
return p;
}
void main(){
CQueue *p;
p=InitQueue();
getch();
}