使用队列打印杨辉三角

文章展示了如何使用C++模板类实现一个链队,并利用该队列数据结构生成杨辉三角。通过`enqueue`和`dequeue`操作,以及STL中的`queue`,展示了计算和打印杨辉三角的具体步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

C++抽象出链队,打印杨辉三角

#include <iostream>
using namespace std;

enum status {
    success, failed
};

template<class T>
class node {
public:
    T data;
    node<T>* next;
};

template<class T>
class LinkedQueue {
public:
    LinkedQueue() {
        head = rear = new node<T>;
        rear->next = nullptr;
        count = 0;
    }
    bool isEmpty();
    int getLength();
    status enQueue(T x);
    status deQueue(T& x);
    status travQueue();
private:
    node<T>* head;
    node<T>* rear;
    int count;
};

template<class T>
bool LinkedQueue<T>::isEmpty() {
    return count == 0;
}

template<class T>
int LinkedQueue<T>::getLength() {
    return count;
}

template<class T>
status LinkedQueue<T>::enQueue(T x) {
    node<T>* cur = new node<T>;
    if (cur) {
        cur->data = x;
        cur->next = nullptr;
        rear->next = cur;
        rear = cur;
        count++;
        return success;
    }
    else {
        return failed;
    }
}

template<class T>
status LinkedQueue<T>::deQueue(T& x) {
    if (isEmpty()) {
        return failed;
    }
    else {
        node<T>* cur = head->next;
        x = cur->data;
        head->next = cur->next;
        delete cur;
        count--;
        return success;
    }
}

template<class T>
status LinkedQueue<T>::travQueue() {
    if (head->next) {
        node<T>* cur = head->next;
        while (cur != nullptr) {
            cout << cur->data << " ";
            cur = cur->next;
        }
        cout << endl;
        return success;
    }
    return failed;
}

void printSpaces(int n) {
    for (int i = 0; i < n; i++) {
        cout << " ";
    }
}

void Yanghui(int n) {
    LinkedQueue<int> queue;
    printSpaces(n-1);
    cout << 1 << endl;
    int prev = 0;
    int flag = 0;
    int cur = 0;
    queue.enQueue(1);
    queue.enQueue(1);
    for (int i = 1; i < n; i++)
    {
        printSpaces(n-1-i);
        queue.enQueue(flag);
        for (int j = 0; j <= i + 1; j++)//第i行i+1个数
        {
            queue.deQueue(cur);
            queue.enQueue(prev + cur);
            prev = cur;
            if (j != i + 1)
            {
                cout << cur << " ";
            }
        }
        cout << endl;
    }
}

int main() {
    cout << "请输入杨辉三角行数:";
    int n; cin >> n;
    Yanghui(n);
    return 0;
}

复用STL版本:

#include<iostream>
#include<queue>
using namespace std;

void printSpaces(int n) {
	for (int i = 0; i < n; i++) {
		cout << " ";
	}
}

void Yanghui(int n) {
	queue<int> q;
	printSpaces(n - 1);
	cout << 1 << endl;
	int prev = 0;
	int flag = 0;
	int cur = 0;
	q.push(1);
	q.push(1);
	for (int i = 1; i < n; i++)
	{
		printSpaces(n - 1 - i);
		q.push(flag);
		for (int j = 0; j <= i + 1; j++)//第i行i+1个数
		{
			cur=q.front();
			q.pop();
			q.push(prev + cur);
			prev = cur;
			if (j != i + 1)
			{
				cout << cur << " ";
			}
		}
		cout << endl;
	}
}

int main() {
	cout << "请输入杨辉三角行数:";
	int n; cin >> n;
	Yanghui(n);
	return 0;
}

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值