zhzbd
zhzbd
采纳率100%
2021-02-27 18:15

此代码段出现问题,应怎么修改;注:勿变动模板形式和判断满公式

已结题
//LinearList.h
#include<iostream>
using namespace std;
template<class T>
class LinearQueue
{
public:
	LinearQueue(int LQMaxSize);
	~LinearQueue();
	bool IsEmpty();
	bool IsFull();
	bool Insert(T x);
	bool Delete(T& x);
	bool GetElement(T &x);

private:
	int front,rear;
	T*element;
    int Maxsize;
};

template<class T>
LinearQueue<T>::LinearQueue(int LQMaxSize)
{ Maxsize=LQMaxSize;
  element=new T[Maxsize];
  front=0;
  rear=0;}

template<class T>
LinearQueue<T>::~LinearQueue()
{ delete []element;}

template<class T>
bool LinearQueue<T>::IsEmpty()
{ if(front==rear)
     return true;
else return false;}

template<class T>
bool LinearQueue<T>::IsFull()
{
  if((rear+1)%Maxsize==front)
	  return true;
  else
	  return false;}

template<class T>
bool LinearQueue<T>::Insert(T x)
{ if(IsFull())
  {cout<<"此栈已满,无法添加"<<endl;
return false;}
else
{ element[rear]=x;
  rear=(rear+1)%(Maxsize);
 return true;}
}

template<class T>
bool LinearQueue<T>::Delete(T& x)
{ if(IsEmpty())
{cout<<"此栈是空"<<endl;
return false;}
else
{ x=element[front];
  front=(front+1)%(Maxsize);
return true;}}

template<class T>
bool LinearQueue<T>::GetElement(T &x)
{ if(IsEmpty())
   return false;
else
{
	x=element[front];
	return true;}
}


//LinearList.cpp
#include "stdafx.h"
#include"LinearList.h"
#include<iostream>
using namespace std;
void YangHui(int n);
void PrintSpace(int n,int k)
{ for(int i=1;i<=n-k;i++)
   cout<<' ';
}
int main()
{   int n;
   cout<<"请输入杨辉三角的行数"<<endl;
   cin>>n;
   YangHui(n);
   system("pause");
	return 0;
}

void YangHui(int n)
{ 
	LinearQueue<int>Q(n+2);
	int x,y;
	PrintSpace(n,1);
	cout<<'1'<<endl;
	Q.Insert(0);
	Q.Insert(1);
	Q.Insert(1);
	for(int i=2;i<=n;i++)
	{  Q.Insert(0);
	PrintSpace(n,i);
	   do
	   {Q.Delete(x);
	   Q.GetElement(y);
	   if(y)
		   cout<<y<<' ';
	   else
		   cout<<endl;
	   Q.Insert(x+y);}
	   while(y);}
	cout<<endl;}

  • 点赞
  • 收藏
  • 复制链接分享

1条回答

  • zhzbd zhzbd 2月前

    点赞 评论 复制链接分享

为你推荐