package yaobo.stack;public
class
linklist
else
}//定位索引的位置
public
void posindex(int
index)
if (index == -1
) current =head;
int j = 1
;
while (current != null && j }
//刪除鍊錶元素
public
void delete(int
index)
catch
(exception e)
}posindex(index-1
); current.setnext(current.next.next);
}//方法:遍歷鍊錶(列印輸出鍊錶。方法的引數表示從節點node開始進行遍歷
public
void
print(node node)
current =node;
while (current != null
) }
class
node
public
intgetdata()
public
void setdata(int
data)
public
node getnext()
public
void
setnext(node next)
}public
static
void
main(string args)
list.print(list.head);
//從head節點開始遍歷輸出
system.out.println("
\n" + "
刪除元素後:");
list.delete(4);
list.print(list.head);
}}
使用內部類的最大好處是可以和外部類進行私有操作的互相訪問。
注:內部類訪問的特點是:內部類可以直接訪問外部類的成員,包括私有;外部類要訪問內部類的成員,必須先建立物件。
為了方便新增和遍歷的操作,在linklist類中新增乙個成員變數current,用來表示當前節點的索引(03行)。
這裡面的遍歷鍊錶的方法(20行)中,引數node表示從node節點開始遍歷,不一定要從head節點遍歷。
**
鍊錶的實現
鍊錶是一種非常重要的資料結構,比起陣列來雖然操作繁瑣,查詢效率也不如陣列效率高,但在進行插入刪除操作時,鍊錶具有陣列無法比擬的效率,下面的 是鍊錶的實現 include include include define n 100 typedef struct node link link node i...
鍊錶的實現
include using namespace std template class linklist node head public linklist t a,int n 0 利用尾插法來構建線性鍊錶 linklist bool isempty 不為空,則返回0,為空則返回非0 t getnod...
鍊錶的實現
記憶體結構 鍊錶也是資料結構的一種,但是和陣列不一樣,陣列在記憶體中每個節點的位置是相連的。而鍊錶的每個節點在物件中是分散的,依靠引用相連。優點1 單鏈表在增加和刪除上要比陣列結構更加快捷。原因 因為順序表在記憶體中是相連的,所以刪除乙個節點,在該節點之後的節點都要隨之前移,所以效率不高。而單鏈表使...