clearList并不能完全删除元素,会留有第一个元素未删除,这是为啥?
大家可以看到在我加了断点后,运行到clearList的下一行,list里面依旧有首元素(我第一个元素输入的是9)
贴出源程序:
void myList::clearList()
{
m_iLength = 0;
/*delete []m_pList;
m_pList = NULL;*/
}
bool myList::listInsert(int i,int*e)
{
if(i > m_iLength)
return false;
else
{
for(int j = m_iLength-1;j >= i;j--)
{
m_pList[j+1] = m_pList[j];
/*m_pList[i] = *e;
m_iLength++;
return true;*///不能放在此处,否则在最开始时无法插入值
}
}
m_pList[i] = *e;
m_iLength++;
return true;
}
cpp文件:
#include"myList.h"
#include<stdlib.h>
#include<iostream>
using namespace std;
int main()
{
myList list1(20);
list1.listTraverse();
int e1 =9;
int e2 =7;
int e3 =6;
int e4 =5;
int e5 =4;
int e6 =1;
int e7 =2;
list1.listInsert(0,&e1);
//list1.listTraverse();
list1.listInsert(1,&e2);
list1.listInsert(2,&e3);
list1.listInsert(3,&e4);
list1.listInsert(4,&e5);
list1.listInsert(5,&e6);
list1.listInsert(6,&e7);
list1.listTraverse();
cout << endl;
cout << "链表长度:" << list1.listLength() << endl;
list1.clearList();//并没有达到清空list的作用
list1.test();
/*list1.listTraverse();
cout << endl;
cout << "链表长度:" << list1.listLength() << endl;
int ele = 0;
list1.getEterm(3,&ele);
int temp = 5;
list1.locateEterm(&temp);
int curre = 4;
int prior = 0;
list1.priorEterm(&curre,&prior);
int lator = 0;
list1.latorEterm(&curre,&lator);
int i =3;
int deleeterm = 0;
list1.listDeleteElterm(i,&deleeterm);
list1.listTraverse();
cout << endl;
cout << "被删除的元素是:" << deleeterm << endl;*/
system("pause");
return 0;
}