java数据结构----链表

Posted 海的味道

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java数据结构----链表相关的知识,希望对你有一定的参考价值。

1.链表:链表是继数组之后第二种使用的最广泛的通用存储结构,它克服了数组的许多弊端:无序数组的查找慢问题,有序数组的插入慢问题,数组定义时的定长问题。它也可取代数组,作为其他数据结构的基础。

2.引用的概念补充:

3.单链表代码:

  3.1.Link.java

 1 package com.cn.linklist;
 2 /**
 3  * 单链表节点对象
 4  * @author Administrator
 5  *
 6  */
 7 public class Link {
 8 public int idata;
 9 public double ddata;
10 public Link next;
11 
12 public Link(int id,double dd){
13     idata = id;
14     ddata = dd;
15 }
16 public void displaylink(){
17     System.out.print("{"+idata+","+ddata+"}");
18 }
19 }
View Code

  3.2.LinkList.java

 1 package com.cn.linklist;
 2 
 3 /**
 4  * 单链表对象
 5  * @author Administrator
 6  *
 7  */
 8 public class LinkList {
 9 private Link first;
10 public  LinkList(){
11     first = null;
12 }
13 public boolean isEmpty(){
14     return (first == null);
15 }
16 public Link find(int key){
17     Link current = first;
18     while (current.idata != key){
19         if (current.next == null)
20             return null;
21         current = current.next;
22     }
23     return current;
24 }
25 public Link delete(int key){
26     Link current = first;
27     Link previous = first;
28     while (current.idata != key){
29         if (current.next == null)
30             return null;
31         previous = current;
32         current = current.next;
33     }
34     if (current == first)
35         first = first.next;
36     else 
37         previous.next = current.next;
38     return current;
39 }
40 public void insertFirst(int id,double dd){
41     Link newlink = new Link(id, dd);
42     newlink.next = first;
43     first = newlink;
44 }
45 public Link deleteFirst(){
46     Link tmp = first;
47     first = first.next;
48     return tmp;
49 }
50 public void displaylist() throws InterruptedException{
51     System.out.print("list(first--->last):");
52     Link current = first;
53     while (current != null){
54         current.displaylink();
55         current = current.next;
56     }
57     System.out.println("");
58 }
59 }
View Code

  3.3.LLTest.java

 1 package com.cn.linklist;
 2 /**
 3  * 单链表的测试代码
 4  * @author Administrator
 5  *
 6  */
 7 public class LLTest {
 8 public static void main(String[] args) throws InterruptedException {
 9     LinkList list = new LinkList();
10     list.insertFirst(0, 22);
11     list.insertFirst(1, 10);
12     list.insertFirst(2, 33);
13     list.displaylist();
14     while (! list.isEmpty()){
15         System.out.println(list.deleteFirst().idata);
16 }
17 }
18 }
View Code

4.双端链表:双端链表和单链表类似,但是它有一个新特性:即对最后一个链节点的引用,它使得链表可以像单链表一样在表添加一个链节点,可用双端链表作为队列的底层数据结构来实现。

5.双端链表代码:

  5.1.LinkT.java

 1 package com.cn.linklist;
 2 /**
 3  * 双端链表节点
 4  * @author Administrator
 5  *
 6  */
 7 public class LinkT {
 8 public long ddata;
 9 public LinkT next;
10 public LinkT(long dd){
11     ddata = dd;
12 }
13 public void displaylink(){
14     System.out.print("{"+ddata+"}");
15 }
16 }
View Code

  5.2.LinkTList.java

 1 package com.cn.linklist;
 2 /**
 3  * 双端链表实现
 4  * @author Administrator
 5  *
 6  */
 7 public class LinkTList {
 8 private LinkT first;
 9 private LinkT last;
10 public LinkTList(){
11     first = null;
12     last = null;
13 }
14 public boolean isEmpty(){
15     return (first == null);
16 }
17 public void insertFirst(long dd){
18     LinkT l = new LinkT(dd);
19     if (isEmpty())
20         last = l;
21     l.next = first;
22     first = l;
23 }
24 public void insertLast(long dd){
25     LinkT l = new LinkT(dd);
26     if (isEmpty())
27         first = l;
28     else
29         last.next = l;
30     last = l;
31 }
32 public long deleteFirst(){
33     long temp = first.ddata;
34     if (first.next == null)
35         last = null;
36     first = first.next;
37     return temp;
38 }
39 public void displaylinktlist(){
40     System.out.print("list(first--->last):");
41     LinkT current = first;
42     while (current != null){
43         current.displaylink();
44         current = current.next;
45     }
46     System.out.println("");
47 }
48 
49 }
View Code

  5.3.LTLTest.java

 1 package com.cn.linklist;
 2 /**
 3  * 双端链表测试代码
 4  * @author Administrator
 5  *
 6  */
 7 public class LTLTest {
 8 public static void main(String[] args) {
 9     LinkTList list = new LinkTList();
10     list.insertFirst(100);
11     list.insertLast(200);
12     list.insertFirst(300);
13     list.insertFirst(400);
14     list.insertLast(500);
15     list.displaylinktlist();
16     while (! list.isEmpty()){
17         System.out.print(list.deleteFirst());
18         System.out.print("   ");
19     }
20     System.out.println(list.isEmpty());
21 }
22 }
View Code

6.链表的效率:在表头添加和删除节点时间复杂度O(1),查找,删除,和在指定位置插入节点的平均时间复杂度为:O(N).与数组比较,链表在插入和删除时的效率要高得多,因为它可以成功的避开多次移动造成的时间浪费。另为,链表需要多少内存就可得到多少,是在定义时不需要指定大小的。

7.抽象数据类型(ADT):他是一种考虑数据结构的方式,着重与它做了什么,而忽略它是怎么做的。栈和队列就是典型的例子。

8.使用链表创建堆代码:

  8.1.LinkX.java

 1 package com.cn.linklist;
 2 /**
 3  * 用于创建堆的数据结构--》链表的节点对象生成类
 4  * @author Administrator
 5  *
 6  */
 7 public class LinkX {
 8 public long ddata;
 9 public LinkX next;
10 public LinkX(long dd){
11     ddata = dd;
12 }
13 public void displaylink(){
14     System.out.print("{"+ddata+"}");
15 }
16 }
View Code

  8.2.LinkXList.java

 1 package com.cn.linklist;
 2 /**
 3  * 用于创建堆的链表代码
 4  * @author Administrator
 5  *
 6  */
 7 public class LinkXList {
 8 private LinkX first;
 9 public LinkXList(){
10     first = null;
11 }
12 public boolean isEmpty(){
13     return (first == null);
14 }
15 public void insertFirst(long dd){
16     LinkX link = new LinkX(dd);
17     link.next = first;
18     first = link;
19 }
20 public long deleteFirst(){
21     LinkX tmp = first;
22     first = first.next;
23     return tmp.ddata;
24 }
25 public void displaylist(){
26     LinkX current = first;
27     while(current != null){
28         current.displaylink();
29         current =current.next;
30     }
31     System.out.println("");
32 }
33 }
View Code

  8.3.LinkXStack.java

 1 package com.cn.linklist;
 2 /**
 3  * 利用链表实现的堆代码
 4  * @author Administrator
 5  *
 6  */
 7 public class LinkXStack {
 8 private LinkXList thelist;
 9 public LinkXStack(){
10     thelist = new LinkXList();
11 }
12 public void push(long j){
13     thelist.insertFirst(j);
14 }
15 public long pop(){
16     return thelist.deleteFirst();
17 }
18 public boolean isEmpty(){
19     return (thelist.isEmpty());
20 }
21 public void displaystack(){
22     System.out.print("stack(top--->bottom):");
23     thelist.displaylist();
24 }
25 }
View Code

  8.4.LXSTest.java

 1 package com.cn.linklist;
 2 /**
 3  * 利用链表实现的堆测试代码
 4  * @author Administrator
 5  *
 6  */
 7 public class LXLSTest {
 8 public static void main(String[] args) {
 9     LinkXStack stack = new LinkXStack();
10     stack.push(100);
11     stack.push(200);
12     stack.push(300);
13     stack.push(400);
14     stack.push(500);
15     stack.push(600);
16     stack.displaystack();
17     while (!stack.isEmpty()){
18         System.out.print(stack.pop());
19         System.out.print("   " );
20     }
21     System.out.println("");
22     System.out.println(stack.isEmpty());
23 }
24 }
View Code

9.使用双端链表创建队列代码:

  9.1.LinkT.java

 1 package com.cn.linklist;
 2 /**
 3  * 双端链表节点
 4  * @author Administrator
 5  *
 6  */
 7 public class LinkT {
 8 public long ddata;
 9 public LinkT next;
10 public LinkT(long dd){
11     ddata = dd;
12 }
13 public void displaylink(){
14     System.out.print("{"+ddata+"}");
15 }
16 }
View Code

  9.2.LinkTList.java

 1 package com.cn.linklist;
 2 /**
 3  * 双端链表实现
 4  * @author Administrator
 5  *
 6  */
 7 public class LinkTList {
 8 private LinkT first;
 9 private LinkT last;
10 public LinkTList(){
11     first = null;
12     last = null;
13 }
14 public boolean isEmpty(){
15     return (first == null);
16 }
17 public void insertFirst(long dd){
18     LinkT l = new LinkT(dd);
19     if (isEmpty())
20         last = l;
21     l.next = first;
22     first = l;
23 }
24 public void insertLast(long dd){
25     LinkT l = new LinkT(dd);
26     if (isEmpty())
27         first = l;
28     else
29         last.next = l;
30     last = l;
31 }
32 public long deleteFirst(){
33     long temp = first.ddata;
34     if (first.next == null)
35         last = null;
36     first = first.next;
37     return temp;
38 }
39 public void displaylinktlist(){
40     System.out.print("list(first--->last):");
41     LinkT current = first;
42     while (current != null){
43         current.displaylink();
44         current = current.next;
45     }
46     System.out.println("");
47 }
48 
49 }
View Code

  9.3.LinkTQueue.java

 1 package com.cn.linklist;
 2 /**
 3  * 使用链表实现的队列代码
 4  * @author Administrator
 5  *
 6  */
 7 public class LinkTQueue {
 8 private LinkTList thelist;
 9 public LinkTQueue(){
10     thelist = new LinkTList();
11 }
12 public boolean isEmpty(){
13     return (thelist.isEmpty());
14 }
15 public void insert(long j){
16     thelist.insertLast(j);
17 }
18 public long remove(){
19     return thelist.deleteFirst();
20 }
21 public void displayqueue(){
22     System.out.print("queue(front--->rear):");
23     thelist.displaylinktlist();
24 }
25 }
View Code

  9.4.LTQTest.java

 1 package com.cn.linklist;
 2 
 3 public class LTQTest {
 4 public static void main(String[] args) {
 5     LinkTQueue queue = new LinkTQueue();
 6     queue.insert(10);
 7     queue.insert(20);
 8     queue.insert(30);
数据结构  ---[链表 ] [使用链表实现栈 以及 队列 (Java代码实现)]

数据结构单向链表及其Java代码实现

java数据结构:单链表常见操作代码实现

Java数据结构—环形链表

Java数据结构线性表之链表

Java数据结构线性表之链表