当我把元素插入线性表头部时,再从头部删除时,为什么返回头部元素时最后插入的元素没有打印出来?求助不解,下面是代码和运行效果,麻烦大家帮忙看看
运行结果
这个是SeqListForCpp.h
#pragma once template <typename T> class SeqListForCpp { public: SeqListForCpp(int capacity); ~SeqListForCpp(); void clearList(); int getLength(); int getCapacity(); int insertNode(T &node, int pos); T& getNode(int pos); T& deleteNode(int pos); private: int capacity; int length; T *pArr; };
SeqListForCpp.cpp 问题代码在82到85行
1 #include <iostream> 2 using namespace std; 3 #include "SeqListForCpp.h" 4 5 template <typename T> 6 SeqListForCpp<T>::SeqListForCpp(int capacity) { 7 this->pArr = new T[capacity]; 8 this->capacity = capacity; 9 this->length = 0; 10 } 11 12 template <typename T> 13 SeqListForCpp<T>::~SeqListForCpp() { 14 if(this->pArr != NULL) { 15 delete [] pArr; 16 } 17 this->capacity = 0; 18 this->length = 0; 19 } 20 21 template <typename T> 22 void SeqListForCpp<T>::clearList() { 23 if(this->pArr != NULL) { 24 delete [] pArr; 25 } 26 this->pArr = new T[capacity]; 27 this->capacity = 0; 28 this->length = 0; 29 } 30 31 template <typename T> 32 int SeqListForCpp<T>::getLength() { 33 return this->length; 34 } 35 36 template <typename T> 37 int SeqListForCpp<T>::getCapacity() { 38 return this->capacity; 39 } 40 41 42 template <typename T> 43 int SeqListForCpp<T>::insertNode(T &node, int pos) { 44 45 if(pos < 0) { 46 return -1; 47 } 48 if(pos > this->length) { 49 pos = this->length; 50 } 51 52 int i; 53 for(i = this->length; i > pos; i--) { 54 this->pArr[i] = this->pArr[i - 1]; 55 } 56 57 this->pArr[i] = node; 58 this->length++; 59 return 0; 60 } 61 62 template <typename T> 63 T& SeqListForCpp<T>::getNode(int pos) { 64 /* 65 if(pos > this->length -1 || pos < 0) { 66 return NULL; 67 } 68 */ 69 return this->pArr[pos]; 70 } 71 72 template <typename T> 73 T& SeqListForCpp<T>::deleteNode(int pos) { 74 /* 75 if(pos < 0) { 76 return NULL; 77 } 78 if(pos > this->length) { 79 return NULL; 80 } 81 */ 82 T &node = this->pArr[pos]; 83 for(int i = pos; i < this->length; i++) { 84 this->pArr[i] = this->pArr[i + 1]; 85 } 86 this->length--; 87 return node; 88 }
测试代码 第38行出问题
1 #include <iostream> 2 #include <string> 3 using namespace std; 4 #include "SeqListForCpp.cpp" 5 6 struct Teacher { 7 int age; 8 char name[32]; 9 }; 10 11 void test01() { 12 Teacher t1, t2, t3, t4; 13 t1.age = 21; 14 strcpy(t1.name, "zhangsan"); 15 t2.age = 22; 16 strcpy(t2.name, "lisi"); 17 t3.age = 23; 18 strcpy(t3.name, "wangwu"); 19 t4.age = 24; 20 strcpy(t4.name, "zhaoliu"); 21 22 SeqListForCpp<Teacher> list(10); 23 24 list.insertNode(t1, 0); 25 list.insertNode(t2, 0); 26 list.insertNode(t3, 0); 27 list.insertNode(t4, 0); 28 29 30 cout << "capacity = " << list.getCapacity() << endl; 31 cout << "length = " << list.getLength() << endl; 32 33 for(int i = 0; i < list.getLength(); i++) { 34 cout << i << " " << list.getNode(i).age << " " << list.getNode(i).name << endl; 35 } 36 37 while(list.getLength() > 0) { 38 cout << list.deleteNode(0).name << endl; 39 } 40 cout << endl; 41 list.clearList(); 42 cout << "length = " << list.getLength() << endl; 43 44 } 45 46 int main() { 47 test01(); 48 system("pause"); 49 return 0; 50 }
deletenode写的有问题
我知道,这个方法有问题,degug出来了,但不知道它为什么会出错
@menz_i:
@jello chen: 这是顺序存储的啊,类似于数组,删除了该节点,后面的所有节点会相应的往前移吧.....
deleteNode的问题
以第一调用deleteNode为例
82行得到pArr[0]的引用,却在下面的循环中将pArr[1]的值赋给pArr[0],这时node值由zhaoliu变成了wangwu,而zhaoliu再也不会出现了...
所以deleteNode的返回值和node的类型都应该为T,而不是T&。
还有下面的循环会将pArr[3]的值变成pArr[4],也就是没初始化的值。
再经过多少调用deleteNode直到length为1时,这个值赋给了pArr[0]并返回,于是最后看到一堆乱码。