clearList并不能完全刪除元素,會留有第一個元素未刪除,這是為啥?
大家可以看到在我加了斷點(diǎn)后,運(yùn)行到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();//并沒有達(dá)到清空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;
}
2017-01-10
你好,我的理解是:clearList只是將節(jié)點(diǎn)刪除,但是這個鏈表空間還在,而第一個節(jié)點(diǎn)儲存了鏈表空間的地址。所以,要使用ListEmpty才可以完全刪除。(根據(jù)數(shù)組推斷的,僅供參考)