java 帶頭迴圈單鏈表

2021-09-19 10:21:08 字數 1512 閱讀 8507

首先建立乙個介面

public inte***ce iclinked
然後實現介面:

public class headsinglelistimpl implements iclinked 

//資料節點

public node(int data)

}private node head;

public headsinglelistimpl()

@override

public void addfirst(int data)

@override

public void addlast(int data)

node.next = cur.next;

cur.next = node;

/* cur.next = node;

node.next = this.head;*/

}//index-1的位置

private node searchindex(int index)

}@override

public boolean addindex(int index, int data)

@override

public boolean contains(int key)

cur = cur.next;

}return false;

}//如果找不到返回空

private node searchpre(int key)

pre = pre.next;

}return null;

}@override

public int remove(int key)

node del = pre.next;

int olddata = del.data;

pre.next= del.next;

return olddata;

}@override

public void removeallkey(int key) else}}

@override

public int getlength()

return count;

}@override

public void display()

system.out.println();

}@override

public void clear()

//頭結點為空

this.head = null;}}

測試類:

public class testmain 

}

以下就是執行後的結果

迴圈單鏈表 JAVA

前兩章寫了單鏈表的操作,在這裡,寫了乙個迴圈單鏈表。為了更好地實現各操作,在此引入頭結點,這裡的頭結點與前兩章定義的頭結點不同,前兩章的頭結點即首節點,而這裡的頭結點並不是首節點。這樣說可能有些糊塗。那就再詳細地解釋一下頭結點和首節點的區別,頭結點是物理上的第乙個節點,但是它存放的不是我們所需要的資...

帶頭節點的單鏈表

需要注意 include define maxlen 20 define elementtype char using namespace std typedef struct slnode node 函式宣告 void initiallist node l int listlength node ...

單鏈表(帶頭結點)

按照自己的想法和思路寫了一下帶頭結點的單鏈表,並進行了測試,畢竟自己能力有限,可能有的地方沒有測試到,還可能存在一些潛在的錯誤。標頭檔案 include using namespace std typedef struct node node,link typedef struct list lis...