当前位置:文档之家› 队列实现杨辉三角

队列实现杨辉三角

Main:
queue.h:
typedef int ElemType;
typedef struct Inode{
ElemType data;
struct Inode *next;
}Inode;
typedef struct linkque{
Inode *front;
Inode *rear;
}linkque;
}
while(q2.front!=q2.rear){
QueOut(q2,e);
QueIn(q1,e);
}
}
}
queue.cpp:
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include "queue.h"
int QueInit(linkque &q){
Inode *p;
p=(Inode *)malloc (sizeof(Inode));
if(!p)
return (0);
p->data=e;
p->next=NULL;
q.rear->next=p;
q.rear=p;
return (1);
}
int QueOut(linkque &q,ElemType &e){
Inode *p;
if(q.front==q.rear)
return (0);
p=q.front->next;
e=p->data;
q.front->next=p->next;
if(q.rear==p)
q.rear=q.front;
free(p);
return(1);
}
#include "queue.h"
vБайду номын сангаасid main(){
linkque q1,q2;
int i,n;
ElemType e,e1,e2,e3;
printf("请输入需要的杨辉三角长度:\n");
scanf("%d",&n);
QueInit(q1);
QueInit(q2);
for(i=1;i<=n;i++){
e3=0;
while(q1.front!=q1.rear){
QueOut(q1,e1);
e2=e3+e1;
printf("%d\t",e2);
QueIn(q2,e2);
e3=e1;
}
if(q1.front==q1.rear){
e2=1;
QueIn(q2,e2);
printf("%d",e2);
printf("\n");
int QueInit(linkque &);
int QueIn(linkque &,ElemType);
int QueOut(linkque &,ElemType &);
app.cpp:
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
q.front=(Inode *)malloc (sizeof(Inode));
q.rear=q.front;
if(!q.front){
printf("溢出");
return (0);
}
q.front->next=NULL;
return (1);
}
int QueIn(linkque &q,ElemType e){
相关主题