第二章线性表设计2【物联网1132-11】
来源:互联网 发布:淘宝购买的模板找不到 编辑:程序博客网 时间:2024/05/18 03:56
以下程序是对于“程序的实际代码没有深刻了解”就打出来的诸多问题的代码。
#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【物联网1132-11】
- 实验二:线性表的实验【物联网1132-11】
- 《数据结构》第二章 线性表教学设计2
- 第二章 学后心得【物联网1132-11】
- 第二章 思维导图【物联网1132-11】
- 第二章 线性表
- 第二章:线性表
- 第二章 线性表
- 第二章 线性表
- 第二章 线性表
- 线性表 第二章
- 第二章:线性表
- 第二章线性表
- 第二章 线性表
- 第二章:线性表
- 第二章:线性表
- 第二章线性表
- 第二章 线性表
- hdu 1045 Fire Net
- 堆和栈的区别
- STL源码剖析——关联容器之multiset
- 使用thinkphp时遇到的一些问题
- android的消息处理机制(图+源码分析)——Looper,Handler,Message
- 第二章线性表设计2【物联网1132-11】
- 网站优化
- 笨鸟之AOP的xml配置的实现
- 由海底捞的高质量服务引起的思考
- c++学习笔记之关联容器
- 输入一个整数求每位之和
- CPU的数据线、地址线和通用寄存器位数的意义
- Java基础学习记录
- 数据库多条件查询 CASE... WHEN