『壹』 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();
}