亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定

clearList并不能完全刪除元素,會留有第一個元素未刪除,這是為啥?

http://img1.sycdn.imooc.com//5873998600013d0905310206.jpg

大家可以看到在我加了斷點后,運行到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;

}


正在回答

1 回答

你好,我的理解是:clearList只是將節點刪除,但是這個鏈表空間還在,而第一個節點儲存了鏈表空間的地址。所以,要使用ListEmpty才可以完全刪除。(根據數組推斷的,僅供參考)

0 回復 有任何疑惑可以回復我~
#1

慕村3311326 提問者

應該是這樣,就像數組不能清空,只能將其置零一樣
2017-02-04 回復 有任何疑惑可以回復我~

舉報

0/150
提交
取消

clearList并不能完全刪除元素,會留有第一個元素未刪除,這是為啥?

我要回答 關注問題
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號