Java数据结构之链表(动力节点之Java学院整理)
程序员文章站
2024-03-01 23:31:34
单链表:
insertfirst:在表头插入一个新的链接点,时间复杂度为o(1)
deletefirst:删除表头的链接点,时间复杂度为o(1)
find:查...
单链表:
insertfirst:在表头插入一个新的链接点,时间复杂度为o(1)
deletefirst:删除表头的链接点,时间复杂度为o(1)
find:查找包含指定关键字的链接点,由于需要遍历查找,平均需要查找n/2次,即o(n)
remove:删除包含指定关键字的链接点,由于需要遍历查找,平均需要查找n/2次,即o(n)
public class linkedlist { private class data{ private object obj; private data next = null; data(object obj){ this.obj = obj; } } private data first = null; public void insertfirst(object obj){ data data = new data(obj); data.next = first; first = data; } public object deletefirst() throws exception{ if(first == null) throw new exception("empty!"); data temp = first; first = first.next; return temp.obj; } public object find(object obj) throws exception{ if(first == null) throw new exception("linkedlist is empty!"); data cur = first; while(cur != null){ if(cur.obj.equals(obj)){ return cur.obj; } cur = cur.next; } return null; } public void remove(object obj) throws exception{ if(first == null) throw new exception("linkedlist is empty!"); if(first.obj.equals(obj)){ first = first.next; }else{ data pre = first; data cur = first.next; while(cur != null){ if(cur.obj.equals(obj)){ pre.next = cur.next; } pre = cur; cur = cur.next; } } } public boolean isempty(){ return (first == null); } public void display(){ if(first == null) system.out.println("empty"); data cur = first; while(cur != null){ system.out.print(cur.obj.tostring() + " -> "); cur = cur.next; } system.out.print("\n"); } public static void main(string[] args) throws exception { linkedlist ll = new linkedlist(); ll.insertfirst(4); ll.insertfirst(3); ll.insertfirst(2); ll.insertfirst(1); ll.display(); ll.deletefirst(); ll.display(); ll.remove(3); ll.display(); system.out.println(ll.find(1)); system.out.println(ll.find(4)); } }
1 -> 2 -> 3 -> 4 -> 2 -> 3 -> 4 -> 2 -> 4 -> null 4
双端链表(不是双向链表):
与单向链表的不同之处在保存有对最后一个链接点的引用(last)
insertfirst:在表头插入一个新的链接点,时间复杂度o(1)
insertlast:在表尾插入一个新的链接点,时间复杂度o(1)
deletefirst:删除表头的链接点,时间复杂度o(1)
deletelast::删除表尾的链接点,由于只保存了表尾的链接点,而没有保存表尾的前一个链接点(这里就体现出双向链表的优势了),所以在删除表尾链接点时需要遍历以找到表尾链接点的前一个链接点,需查找n-1次,也就是o(n)
有了这几个方法就可以用双端链表来实现一个队列了
public class firstlastlist { private class data{ private object obj; private data next = null; data(object obj){ this.obj = obj; } } private data first = null; private data last = null; public void insertfirst(object obj){ data data = new data(obj); if(first == null) last = data; data.next = first; first = data; } public void insertlast(object obj){ data data = new data(obj); if(first == null){ first = data; }else{ last.next = data; } last = data; } public object deletefirst() throws exception{ if(first == null) throw new exception("empty"); data temp = first; if(first.next == null) last = null; first = first.next; return temp.obj; } public void deletelast() throws exception{ if(first == null) throw new exception("empty"); if(first.next == null){ first = null; last = null; }else{ data temp = first; while(temp.next != null){ if(temp.next == last){ last = temp; last.next = null; break; } temp = temp.next; } } } public void display(){ if(first == null) system.out.println("empty"); data cur = first; while(cur != null){ system.out.print(cur.obj.tostring() + " -> "); cur = cur.next; } system.out.print("\n"); } public static void main(string[] args) throws exception { firstlastlist fll = new firstlastlist(); fll.insertfirst(2); fll.insertfirst(1); fll.display(); fll.insertlast(3); fll.display(); fll.deletefirst(); fll.display(); fll.deletelast(); fll.display(); } }
1 -> 2 -> 1 -> 2 -> 3 -> 2 -> 3 -> 2 ->
有序链表:
链表中的数据按从小到大排列
public class sortedlist { private class data{ private object obj; private data next = null; data(object obj){ this.obj = obj; } } private data first = null; public void insert(object obj){ data data = new data(obj); data pre = null; data cur = first; while(cur != null && (integer.valueof(data.obj.tostring()) .intvalue() > integer.valueof(cur.obj.tostring()) .intvalue())){ pre = cur; cur = cur.next; } if(pre == null) first = data; else pre.next = data; data.next = cur; } public object deletefirst() throws exception{ if(first == null) throw new exception("empty!"); data temp = first; first = first.next; return temp.obj; } public void display(){ if(first == null) system.out.println("empty"); system.out.print("first -> last : "); data cur = first; while(cur != null){ system.out.print(cur.obj.tostring() + " -> "); cur = cur.next; } system.out.print("\n"); } public static void main(string[] args) throws exception{ sortedlist sl = new sortedlist(); sl.insert(80); sl.insert(2); sl.insert(100); sl.display(); system.out.println(sl.deletefirst()); sl.insert(33); sl.display(); sl.insert(99); sl.display(); } }
first -> last : 2 -> 80 -> 100 -> 2 first -> last : 33 -> 80 -> 100 -> first -> last : 33 -> 80 -> 99 -> 100 ->
表的插入和删除平均需要比较n/2次,即o(n),但是获取最小数据项只需o(1),因为其始终处于表头,对频繁操作最小数据项的应用,可以考虑使用有序链表实现,如:优先级队列和数组相比,链表的优势在于长度不受限制,并且在进行插入和删除操作时,不需要移动数据项,故尽管某些操作的时间复杂度与数组想同,实际效率上还是比数组要高很多。劣势在于随机访问,无法像数组那样直接通过下标找到特定的数据项 。
以上所述是小编给大家介绍的java数据结构之链表(动力节点之java学院整理),希望对大家有所帮助