package test.written.examination;
public class singlelinkedlist
public node()
} node head, tail;
public singlelinkedlist()
public void mp() else
} }// 新增在頭部
public void addheadnode(int n) else
} // 新增在尾部
public void addtailnode(int n)
node tmp = null;
while (p != null)
tmp.next = new node(n);
} // 只刪除第乙個data=n的節點
public void deleteone(int n)
node pre = head;
node q = pre.next;
while (q != null) else
} }// 刪除所有data=n的節點
public void deleteall(int n)
node pre = head;
node q = pre.next;
while (q != null) else
} }public void depul() else
}p = p.next;
} }public void sort()
q = q.next;
}p = p.next;
} }public void reverse()
head.next = null;
head = p;
} // 指定位置插入節點,插入在第n個節點之後
public void insert(int loc, int data) else else
n++;
pre = p;
p = p.next;
}if(loc > this.length())
}} }
// 返回鍊錶長度
public int length()
return len;
} public static void main(string args) ;
singlelinkedlist list = new singlelinkedlist();
singlelinkedlist list2 = new singlelinkedlist();
for (int i : arr)
system.out.println("原始長度:" + list.length());
system.out.println("原始鍊錶初始化(addtailnode):");
list2.mp();
system.out.println("\n原始鍊錶初始化(addheadnode):");
list.mp();
system.out.println("\nsort():");
list.sort();
list.mp();
system.out.println("\ndeleteone(6):");
list.deleteone(6);
list.mp();
system.out.println("\ndeleteall(1):");
list.deleteall(1);
list.mp();
system.out.println("\nreverse():");
list.reverse();
list.mp();
system.out.println("\ndepul():");
list.depul();
list.mp();
system.out.println("\ninsert(1,999):");
list.insert(3, 999);
list.mp();
}}
output:
原始長度:16
原始鍊錶初始化(addtailnode):
1,5,6,6,6,5,8,9,7,9,0,0,0,0,10,1,
原始鍊錶初始化(addheadnode):
1,10,0,0,0,0,9,7,9,8,5,6,6,6,5,1,
sort():
0,0,0,0,1,1,5,5,6,6,6,7,8,9,9,10,
deleteone(6):
0,0,0,0,1,1,5,5,6,6,7,8,9,9,10,
deleteall(1):
0,0,0,0,5,5,6,6,7,8,9,9,10,
reverse():
10,9,9,8,7,6,6,5,5,0,0,0,0,
depul():
10,9,8,7,6,5,0,
insert(1,999):
10,9,8,999,7,6,5,0,
java單鏈表實現
class node 非空節點 public node object obj description 注 在這裡鍊錶預設都是帶有頭節點 資料域為空 version 1.0 author meify 2013 7 29 下午3 36 19 public class linklist 往單鏈表頭部插入節...
Java實現單鏈表
1.定義兩個介面 定義線性表的介面 和順序表介面一樣 插入,刪除,獲取某個元素等一些基本操作 定義單鏈表節點的介面 獲取節點資料域 設定節點資料域 2.單鏈表節點類 兩個屬性 資料域element 指標域next 3.單鏈錶類的實現 插入操作 將資料元素e插入到object之後 插入前 obj節點 ...
java實現單鏈表
package com.tyxh.link 節點類 public class node 顯示此節點 public void display package com.tyxh.link 單鏈表 public class linklist 插入乙個頭節點 public void addfirstnode...