在写了MyArrayList类之后,也写了个MyLinkedList类,这个类当时只实现了基本的链表操作,很多功能只求能够实现而没有考虑优化,好多细节没有考虑周到。今天重看了那个破烂不堪的类,优化改进了一下。主要有:1,添加类的int length属性,实时记录链表的长度。2,添加类的Node lastNode属性,记录最后一个结点,这样在链表添加操作时就不用每次都遍历一遍,提高了add操作的效率。3,完善方法使用规范,添加异常处理
包下包含MyList.java接口类,Node.java结点类,MyLinkedList.java链表类。(第一个是用循环链表解决约瑟夫问题的类,与本文无关)
结点类 Node.java
1 package cn.ftf.mylinkedlist;
2 /*
3 * 结点类
4 */
5 public class Node {
6 public Object obj;
7 public Node next;
8 public Node() {
9 super();
10 }
11 public Node(Object obj, Node next) {
12 super();
13 this.obj = obj;
14 this.next = next;
15 }
16 public Node(Object obj) {
17 super();
18 this.obj = obj;
19 }
20 }
List接口类 MyList.java
1 package cn.ftf.mylinkedlist;
2
3 public interface MyList {
4 public void clear();
5 public boolean isEmpty();
6 public int length();
7 public Object get(int i) throws Exception;
8 public void add(Object obj);
9 public void inset(int i,Object obj) throws Exception;
10 public void remove(int i) throws Exception;
11 public int indexOf(Object obj);
12 public void display();
13 }
链表类 MyLinkedList.java
1 package cn.ftf.mylinkedlist;
2 /*
3 * 链表类
4 */
5 public class MyLinkedList implements MyList{
6 private Node head; //头结点
7 private int length=0; //实时记录链表长度
8 private Node lastNode=null; //记录最后一个结点,方便插入新结点,就不用遍历了
9
10 public MyLinkedList() {
11 this.head = new Node();
12 }
13
14 @Override
15 public void clear() {
16 this.head=new Node();
17 this.length=0;
18 this.lastNode=null;
19 }
20
21 @Override
22 public boolean isEmpty() {
23 return head.next==null;
24 }
25
26 @Override
27 public int length() {
28 return this.length;
29 }
30
31 @Override
32 public Object get(int i) throws Exception {
33 if(i<0||i>length-1) {
34 return null;
35 }
36 Node p = this.head;
37 for(int a=0;a<i+1;a++) {
38 p=p.next;
39 }
40 return p.obj;
41 }
42
43 @Override
44 public void add(Object obj) {
45 // TODO Auto-generated method stub
46 if(this.lastNode==null) {
47 Node p = this.head;
48 Node newNode=new Node(obj);
49 p.next=newNode;
50 this.lastNode=newNode;
51 }else {
52 Node newNode=new Node(obj);
53 this.lastNode.next=newNode;
54 this.lastNode=newNode;
55 }
56 this.length++;
57 }
58
59 @Override
60 public void inset(int i, Object obj) throws Exception {
61 if(i<0||i>length) {
62 throw new Exception("操作位置非法!");
63 }
64 Node p = this.head;
65 Node newNode=new Node(obj);
66 for(int a=0;a<i;a++) {
67 p=p.next;
68 }
69 newNode.next=p.next;
70 p.next=newNode;
71 this.length++;
72 }
73
74 @Override
75 public void remove(int i) throws Exception {
76 if(i<0||i>length-1) {
77 throw new Exception("操作位置非法!");
78 }
79 Node p = this.head;
80 for(int a=0;a<i;a++) {
81 p=p.next;
82 }
83 p.next=p.next.next;
84 if(i==this.length-1) {
85 this.lastNode=p;
86 }
87 this.length--;
88 }
89
90 @Override
91 public int indexOf(Object obj) {
92 int a=0;
93 Node p = this.head;
94 while(p.next!=null) {
95 p=p.next;
96 if(obj.equals(p.obj)) {
97 return a;
98 }
99 a++;
100 }
101 return -1;
102 }
103
104 @Override
105 public void display() {
106 Node p=head;
107 while(p.next!=null) {
108 System.out.print(p.next.obj+" ");
109 p=p.next;
110 }
111 System.out.println();
112 }
113
114 //主方法测试用
115 public static void main(String[] args) throws Exception {
116 MyLinkedList li=new MyLinkedList();
117 System.out.println(li.isEmpty());
118 li.add("hello");
119 li.add("word!");
120 System.out.println(li.isEmpty());
121 System.out.println(li.length());
122 li.display();
123 System.out.println(li.get(1));
124 li.inset(1, "hahaha");
125 li.display();
126 li.remove(2);
127 li.add(123);
128 li.add("word!");
129 li.display();
130 System.out.println(li.indexOf(123));
131 System.out.println(li.length);
132 }
133
134 }
测试结果:
原文:https://www.cnblogs.com/fangtingfei/p/11524587.html