第二章线性表设计2【物联网1132-11】

以下程序是对于“程序的实际代码没有深刻了解”就打出来的诸多问题的代码。


#include<iostream>
using namespace std;

template<class T>
struct Student
{
	T data;
	Student<T> * next;
};

template<class T>
class LinkList
{
public:
	LinkList();
	LinkList(T a[],int n);
	~LinkList();
	void Insert(int i,T x);
	T Delete(int i);
	void PrintList();
private:
	Student<T> * first;
};

template<class T>
LinkList<T>::LinkList()
{
	first=new Student;
	first->next=NULL;
}

template<class T>
LinkList<T>::LinkList(T a[],int n)
{
	first=new Student;first->next=NULL;
	for(i=0;i<n;i++)
	{
		s=new Student;s->data=a[i];
		s->next=first->next;first->next=s;
	}
}

template<class T>
void LinkList<T>::Insert(int i,T x)
{
	p=first;count=0;
	while(p!=NULL&&count<i-1)
	{
		p=p->next;
		count++;
	}
	if(p==NULL)throw"输入错误"
		else{
		s=new Student;s->data=x;
		s->next=p->next;p->next=s;
	}
}

template<class T>
T LinkList<T>::Delete(int i)
{
	p=first;count=0;
	while(p!=NULL&&count<i-1)
	{
		p=p->next;
		count++;
	}
	if(p==NULL||p->next==NULL)throw"输入错误";
	else{
		q=p->next;x=q->data;
		p->next=q->next;
		delete q;
		return x;
	}
}

template<class T>
void LinkList<T>::PrintList()
{
	p=first->next;
	while(p!=NULL)
	{
		cout<<p->data;
		p=p->next;
	}
}

void main( )  
{  
	int score[5]={10, 20, 40, 50,60};  
    LinkList<int> ScoreList(score, 5);
	ScoreList.PrintList();
	ScoreList.Insert(4,30);
	ScoreList.PrintList();
	ScoreList.Delete(4);
	ScoreList.PrintList();
}


以下是经过老师的指导教育和自己的听课反思改好的代码。


#include<iostream>
using namespace std;

template<class T>
struct Student
{
	T data;
	Student<T> * next;
};

template<class T>
class LinkList
{
public:
	LinkList();
	LinkList(T a[],int n);
	~LinkList();
	void Insert(int i,T x);
	T Delete(int i);
	void PrintList();
private:
	Student<T> * first;
};

template<class T>
LinkList<T>::LinkList()
{
	first=new Student<T>;
	first->next=NULL;
}

template<class T>
LinkList<T>::LinkList(T a[],int n)
{
	Student<T> *s;
	first=new Student<T>;
	first->next=NULL;
	int i;
	for(i=0;i<n;i++)
	{
		s=new Student<T>;
		s->data=a[i];
		s->next=first->next;
		first->next=s;
	}
}

template <class T>  
LinkList<T> :: ~LinkList( )  
{  
    Student<T> *q;  
    while (first != NULL)        
    {  
        q = first;               
        first = first->next;      
        delete q;      
    }  
}

template<class T>
void LinkList<T>::Insert(int i,T x)
{
	Student<T> *p = first , *s;
	int count=0;
	while(p!=NULL&&count<i-1)
	{
		p=p->next;
		count++;
	}
	if(p==NULL)throw"输入错误";
		else{
		s=new Student<T>;
		s->data=x;
		s->next=p->next;
		p->next=s;
	}
}

template<class T>
T LinkList<T>::Delete(int i)
{
	Student<T> *p =first, *q;
	T x;
	int count=0;
	while(p!=NULL&&count<i-1)
	{
		p=p->next;
		count++;
	}
	if(p==NULL||p->next==NULL)throw"输入错误";
	else{
		q=p->next;x=q->data;
		p->next=q->next;
		delete q;
		return x;
	}
}

template<class T>
void LinkList<T>::PrintList()
{
	Student<T> *p = first->next;
	while(p!=NULL)
	{
		cout<<p->data<<" ";
		p=p->next;
	}
	cout<<endl;
}

void main( )  
{  
	int score[5]={10,20,40,50,60};  
    LinkList<int>ScoreList(score, 5);
	ScoreList.PrintList();
	ScoreList.Insert(4,30);
	ScoreList.PrintList();
	ScoreList.Delete(4);
	ScoreList.PrintList();
}



在这里,以后我们要注意的问题如下:【不专业不厉害,可是一步一个脚印】

1、定义模板类后要记得在使用的时候加上“<T>”。

2、定义Student的这个“结构体”模板指针的结点后记得在使用时写成“Student<T>”这种字样。

3、对于每个小框架代码里的变量,记得在使用它们前先定义好,否则就是undeclear之类的。

4、调用的时候不需要写成“ScoreList<T>XXXX“,不过要记得创建对象,否则不可以调用模板函数。



                
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值