您的位置:首页 > 其它

队列实现 杨辉三角的打印

2013-09-09 22:53 288 查看
实验目的:
对队列的应用

问题描述:
给出一个参数n打印
n层的杨辉三角。

求解问题基本思路:
根据n-1层的数,计算出n层的数并将其打印

数据结构:
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>

typedef struct QNode{
   int data;
   struct QNode *next;
  
}QNode,*QueuePtr;

typedef struct{
   QueuePtr front;//队头指针 指向头结点结构体  为结构体指针
   QueuePtr rear;//队尾指针
}LinkQueue;

void YHSJ(LinkQueue Q){
  int i,x,y,N;
  LinkQueue head;
  InitQueue (Q);
  printf("请输入你想打印的层数:");
  scanf("%d",&N);
 
 
  int Init_array[5] = {0,1,2,1,0};
     for(i = 0; i < 5; i++)
   EnQueue(Q,Init_array[i]);
  printf("0 1 2 1 0\n");
  for(i = 0; i < N-1; i++)
  {
   do
   {
   
    DeQueue(Q,x);
     // printf("%d",x);
    GetHead(Q,y);
  // printf("%d",y);
    EnQueue(Q,x+y);
   
   }while(y);//while(y==0) 与while(y)不等价 第一种直接终止循环
          
             EnQueue(Q,0);
             head.front = Q.front;
    // GetHead(Q,y);
    // printf("%d",y);
     while(Q.front != Q.rear){
        Q.front = Q.front ->next;
     printf("%d ",Q.front->data);
     }
  
  
   printf("\n");
  Q.front = head.front;

  }

}

算法流程步骤:
1.杨辉三角第n-1层产生第n层
2.每次队列出队一个元素x ,并且读取下一个头元素 y
3.将x+y 入队
4.不断重复1,2,3直到 m = n;

例如:给出 1 3 3 1
          1 出队 读取3 1和3相加入队 则为 3 3 1 4
          3 出队 读取3 3和3相加入队 则为 3 1 4 6
          3出队   读取1 3和1 相加入队 则为 1 4 6 4
这里有些问题 产生 循环需要终止条件 而且 后面再继续走下去 就产生错误
为了解决这个问题 在给出的数的 末尾两端 加上 0
即 0 1 3 3 1 0
当y = 0 的时候即循环终止。

完整代码:

#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>

typedef struct QNode{
   int data;
   struct QNode *next;
  
}QNode,*QueuePtr;

typedef struct{
   QueuePtr front;//队头指针 指向头结点结构体  为结构体指针
   QueuePtr rear;//队尾指针
}LinkQueue;

int InitQueue(LinkQueue &Q){
  Q.front = Q.rear = (QueuePtr)malloc(sizeof(QNode)); //初始化
  if(!Q.front) exit(0); //存储分配失败
  Q.front -> next = NULL;//头结点 指向第一个结点为空
  return 1;
}

void GetHead(LinkQueue Q, int &e)
{
    e = Q.front -> next -> data;
}

int EnQueue(LinkQueue &Q,int e){
 QueuePtr p;
   p = (QueuePtr)malloc(sizeof(QNode));
   if(!p) exit(0); //存储分配失败
   p ->data = e;//元素e存入新的结点
   p ->next = NULL;//p成为队尾 他的下一个结点 应为NULL
   Q.rear -> next = p;//当前队尾的下一个元素位置指向p
   Q.rear = p;//改变队尾 为p
  return 1;
  
}

int DeQueue (LinkQueue &Q,int &e){
 QueuePtr p;
   if(Q.front == Q.rear) return 0; //队列为空
   p = Q.front -> next;//第一个结点 赋给p
   e = p->data;//e 存储第一个结点的值
   Q.front ->next = p->next;//头结点的下一个位置 指向 第二个结点
   if(Q.rear == p) Q.rear = Q.front;//当删除到最后一个元素时 头指针 尾指针都指向头结点。
   free(p);
  return 1;
}

void DestroyQueue(LinkQueue &Q)
{
   while(Q.front)
   {//删除元素都是从队头删除
     Q.rear = Q.front -> next;//Q.rear 起一个过渡作用 指向头结点下一个位置
     free(Q.front);//释放当前头结点
     Q.front = Q.rear;//头结点下移了一个位置
    
   }
  
}
void YHSJ(LinkQueue Q){
  int i,x,y,N;
  LinkQueue head;
  InitQueue (Q);
  printf("请输入你想打印的层数:");
  scanf("%d",&N);
 
 
  int Init_array[5] = {0,1,2,1,0};
     for(i = 0; i < 5; i++)
   EnQueue(Q,Init_array[i]);
  printf("0 1 2 1 0\n");
  for(i = 0; i < N-1; i++)
  {
   do
   {
   
    DeQueue(Q,x);
     // printf("%d",x);
    GetHead(Q,y);
  // printf("%d",y);
    EnQueue(Q,x+y);
   
   }while(y);//while(y==0) 与while(y)不等价 第一种直接终止循环
          
             EnQueue(Q,0);
             head.front = Q.front;
    // GetHead(Q,y);
    // printf("%d",y);
     while(Q.front != Q.rear){
        Q.front = Q.front ->next;
     printf("%d ",Q.front->data);
     }
  
  
   printf("\n");
  Q.front = head.front;

  }

}
int main(){
   LinkQueue Q;
   YHSJ(Q);
   //DestroyQueue(Q);
return 0;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: