用队列实现杨辉三角 c语言,用队列结构解决杨辉三角问题,C++,出错

用队列结构解决杨辉三角问题,C++,出错0

02ae427d08e371d7e90d5b995e828d6d.pngxcjk12014.02.20浏览59次分享举报

#ifndef LINKQUEUE_H

#define LINKQUEUE_H

#include

#include

#include

using namespace std;

template

class LinkQueueNode{

public:

T data;

LinkQueueNode* link;

LinkQueueNode(T& value):data(value),link(NULL){}

};

template

class LinkQueue{

LinkQueueNode* front;

LinkQueueNode* back;

public:

LinkQueue():front(NULL),back(NULL){}

void EnQueue(T& element);

T DelQueue();

T& GetFront();

void MakeEmpty();

bool IsEmpty();

};

template

void LinkQueue::EnQueue(T& value)

{

LinkQueueNode* add = new LinkQueueNode(value);

if(back == NULL)

{

front = back = add;

}

else

{

back->link = add;

back = back->link;

}

}

template

T LinkQueue::DelQueue()

{

assert(!IsEmpty());

LinkQueueNode* old = front;

T data = old->data;

front = front->link;

if(back == old)

back = NULL;

delete old;

return

data;

}

template

T& LinkQueue::GetFront()

{

assert(!IsEmpty());

return

front->data;

}

template

void LinkQueue::MakeEmpty()

{

while(!this->IsEmpty()){

this->DelQueue();

}

}

template

bool LinkQueue::IsEmpty()

{

return

front == NULL;

}

#endif

.cpp

#include "LinkQueue.h"

using namespace std;

template

void evaluate(LinkQueue& ori,LinkQueue& target){

ori.MakeEmpty();

while(!target.IsEmpty()){

ori.EnQueue(target.DelQueue());

}

}

int main(){

cout<2):";

int num;

cin>>num;

LinkQueue ori;

ori.EnQueue(1);

ori.EnQueue(1);

LinkQueue next;

for(int i=0;i

next.EnQueue(1);

while(!ori.IsEmpty()){

int i=ori.DelQueue();

if(!ori.IsEmpty())

next.EnQueue(i+ori.GetFront());

if(ori.IsEmpty())

next.EnQueue(i);

}

evaluate(ori,next);

}

cout<

while(!ori.IsEmpty()){

cout<

}

cout<

return 0;

}

Error 1 error C2664: 'LinkQueue::EnQueue' : cannot convert parameter 1 from 'int' to 'int &' e:\cpwork\实例\杨辉三角\杨辉三角\杨辉三角.cpp 18

Error 2 error C2664: 'LinkQueue::EnQueue' : cannot convert parameter 1 from 'int' to 'int &' e:\cpwork\实例\杨辉三角\杨辉三角\杨辉三角.cpp 19

Error 3 error C2664: 'LinkQueue::EnQueue' : cannot convert parameter 1 from 'int' to 'int &' e:\cpwork\实例\杨辉三角\杨辉三角\杨辉三角.cpp 22

Error 4 error C2664: 'LinkQueue::EnQueue' : cannot convert parameter 1 from 'int' to 'int &' e:\cpwork\实例\杨辉三角\杨辉三角\杨辉三角.cpp 26

请问错误怎么解决啊

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
杨辉三角是一个由数字构成的三角形,其中每个数字都是它上方两个数字之和。要用队列实现杨辉三角,首先需要创建一个队列。在C语言中,可以用数组来表示队列,并使用两个指针(front和rear)来指向队列的前端和后端。 首先,我们创建一个函数来生成杨辉三角,并将结果存储在队列中。 ```c #include <stdio.h> #define MAX_SIZE 100 int queue[MAX_SIZE]; int front = -1; int rear = -1; // 入队 void enqueue(int num) { if (rear == MAX_SIZE-1) { printf("队列已满\n"); return; } if (front == -1 && rear == -1) { front = 0; rear = 0; } else { rear++; } queue[rear] = num; } // 出队 int dequeue() { if (front == -1 || front > rear) { printf("队列为空\n"); return -1; } int num = queue[front]; front++; return num; } // 生成杨辉三角 void generateYanghuiTriangle(int numRows) { for (int i = 0; i < numRows; i++) { for (int j = 0; j <= i; j++) { if (j == 0 || j == i) { enqueue(1); } else { int a = dequeue(); int b = queue[front]; enqueue(a + b); } printf("%d ", queue[j]); } printf("\n"); } } int main() { int numRows; printf("请输入杨辉三角的行数:"); scanf("%d", &numRows); generateYanghuiTriangle(numRows); return 0; } ``` 在上述代码中,我们首先定义了一个可以最大容纳100个元素的队列,并初始化了front和rear指针。enqueue函数用于将元素存储到队列中,dequeue函数用于从队列中取出元素。generateYanghuiTriangle函数根据输入的行数生成杨辉三角,并将每一行的数字存储到队列中,然后打印每个数字。在main函数中,我们接受用户输入的行数,并调用generateYanghuiTriangle函数来生成杨辉三角

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值