因为不太会最后一个要求,所以我这里最后一个要求没有实现,知道怎么实现的告诉我下 List.h
#ifndef LIST_H_
#define LIST_H_
template
struct Node{
T num;
struct Node *next;
};
template
class List{
static const int MAX=10;
private:
//T t[MAX];
//int top;
Node
Node
int qsize;
Node
public:
//List
List();
~List();//构造函数有new ,必须显示析构
void add(const T &t);
bool isEmpty()const;
bool isFull()const;
//void set(const T &t)const;
void visit(){
Node
Node
while(q!=rear)
{
cout<<”p->num=”<
cout<<”q->num=”<
//cout<<”front=”<
//cout<<”rear=”<
p=q;//
q=q->next;//后移
//if(q==rear)//仔细调试一番,就会知道为什么
//break;
}
cout<<”\nshow end!”<<endl;
};
void showNow()
{
cout<<”\nnow is “<
}
void showRear()
{
cout<<”\nRear is “<
}
//test
void test(){
cout<<”Just test!”<<endl;
}
};
#endif
List.cpp
#include
#include “List.h”
using std::cout;
using std::cin;
using std::endl;
template
List
{
front=rear=nullptr;
qsize=0;
}
template
List
{
delete front;//释放指向首指针的内容
}
template
void List
{
if(isEmpty())
{
Node
front=n;
n->num=t;
n->next=nullptr;
qsize++;
now=n;//令now指向当前节点
rear=n->next;
}
else if(isFull())
{
cout<<”List is full”<<endl;
}
else
{
Node
n->num=t;
now->next=n;
n->next=nullptr;
qsize++;
now=n;//令now指向当前节点
rear=n->next;
}
}
template
bool List
{
//front=nullptr;//不是不能改变里面的数据么?
//qsize=100;编译无报错,但是运行阶段会报错
//cout<<”const is not work! “<<qsize<<endl;
return front==nullptr;
}
template
bool List
{
return qsize==MAX;
}
main108.cpp
#include
#include “List.cpp”//不要写出List.h,不然会出现外部链接错误
using std::cout;
using std::cin;
using std::endl;
void main108()
{
const int ar1[5]={1,2,3,4,5};
double ar2[12]={1.1,2.2,3.3,4.4,5.5,6.6,7.7,8.8,9.9,10.1,11.2};
List
List
for(int i=0;i<5;i++)
{
arr1.add(ar1[i]);
//arr1.showNow();
//arr1.showRear();
}
for(int i=0;i<11;i++)
{
arr2.add(ar2[i]);
arr2.showNow();
}
//arr1.add(10);
arr1.visit();
//arr1.test();
cin.get();
}
—————————————————————————————————————————————————— //写的错误或者不好的地方请多多指导,可以在下面留言或者给我发邮件,指出我的错误以及不足,以便我修改,更好的分享给大家,谢谢。 转载请注明出处:https://www.royalchen.com/ author:royalchen Email:royalchen@royalchen.com ———————————————————————————————————————————————————
- 本文作者: royalchen
- 本文链接: http://www.royalchen.com/2016/02/24/cprimerplus第六版课后编程题答案108/
- 版权声明: 本博客所有文章除特别声明外,均采用 MIT 许可协议。转载请注明出处!