用java语言实现单链表
1 package com.base.list;
2
3 import java.util.HashSet;
4 import java.util.Set;
5
6
7 public class SingleLinkList {
8
9 class Element {
10 public Object value = null;
11 public Element nextNode = null;
12 }
13
14 class Value
15 {
16 public String code;
17 public String name;
18
19 public Value(){}
20 public Value(String code, String name){ this.code = code;this.name = name;}
21 @Override
22 public String toString() {
23 return code + "-" + name ;
24 }
25
26
27 }
28 private Element header = null;
29
30 public static void main(String[] args) {
31 SingleLinkList list = new SingleLinkList();
32 Value value1 = list.new Value("1", "java");
33 Value value2 = list.new Value("2", "c++");
34 Value value3 = list.new Value("3", "c#");
35 Value value4 = list.new Value("4", "vb");
36 list.add(value1);
37 list.add(value2);
38 list.add(value3);
39 list.add(value4);
40 System.out.println("remove vb ?" + list.remove(value4));
41 System.out.println("have c++ ?" + list.contains(value2));
42 System.out.println("have vb ?" + list.contains(value4));
43 System.out.println("list is emptry ? " + list.isEmpty());
44 System.out.println(list);
45 list.clear();
46 System.out.println("list is emptry ? " + list.isEmpty());
47 }
48
49
50 public void add(Object node) {
51 if (header == null) {
52 header = new Element();
53 header.value = null;
54 header.nextNode = null;
55 }
56 Element element = new Element();
57 element.value = node;
58 element.nextNode = header.nextNode;
59 header.nextNode = element;
60 }
61
62 public void clear() {
63 header = null;
64 }
65
66 public boolean contains(Object o) {
67 if(header==null)
68 return false;
69 Element eqEl = header.nextNode;
70 while(eqEl!=null)
71 {
72 if(eqEl.value==o)
73 {
74 return true;
75 }
76 eqEl = eqEl.nextNode;
77 }
78 return false;
79 }
80
81
82 public String toString() {
83 int size = this.size();
84 String print = "";
85 if(size==0)
86 return print;
87 for( int i = 0; i < size ;i ++)
88 {
89 print = "," + this.get(i) + print;
90 }
91 print = "[" + print.substring(1) + "]";
92 return print;
93 }
94
95
96 public Object get(int index) {
97 if(header==null)
98 return null;
99 int size = this.size();
100 if(index > (size - 1) || index < 0)
101 {
102 return null;
103 }
104 Element temp = header.nextNode;
105 int i = 0;
106 while(temp!=null)
107 {
108 if(index == i)
109 {
110 return temp.value;
111 }
112 i++;
113 temp = temp.nextNode;
114 }
115 return null;
116 }
117
118 private Element getElement(int index) {
119 if(header==null)
120 return null;
121 int size = this.size();
122 if(index > (size - 1) || index < 0)
123 {
124 return null;
125 }
126 Element temp = header.nextNode;
127 int i = 0;
128 while(temp!=null)
129 {
130 if(index == i)
131 {
132 return temp;
133 }
134 i++;
135 temp = temp.nextNode;
136 }
137 return null;
138 }
139
140
141 public boolean isEmpty() {
142 if(header==null)
143 return true;
144 else
145 return false;
146 }
147
148
149 public boolean remove(Object o) {
150 if(header==null)
151 return false;
152 Element eqPreEl = header;
153 Element eqEl = header.nextNode;
154 while(eqEl!=null)
155 {
156 if(eqEl.value==o)
157 {
158 eqPreEl.nextNode = eqEl.nextNode;
159 return true;
160 }
161 eqPreEl = eqEl;
162 eqEl = eqEl.nextNode;
163 }
164 return false;
165 }
166
167 public int size() {
168 if(header==null)
169 return 0;
170 Element temp = header.nextNode;
171 int i = 0;
172 while(temp!=null)
173 {
174 i++;
175 temp = temp.nextNode;
176 }
177 return i;
178 }
179
180 /**
181 * 检查环状单链表
182 * @return
183 */
184 public boolean checkLoop()
185 {
186 if(header==null)
187 return false;
188 int size = this.size();
189 if(size==0)
190 return false;
191 Set<Element> set = new HashSet<Element>();
192 for( int i = 0; i < size ;i ++)
193 {
194 Element el = getElement(i);
195 if(!set.contains(el)){
196 set.add(el);
197 }
198 if(set.contains(el.nextNode))
199 {
200 return true;
201 }
202 }
203 return false;
204 }
205 }
206
2
3 import java.util.HashSet;
4 import java.util.Set;
5
6
7 public class SingleLinkList {
8
9 class Element {
10 public Object value = null;
11 public Element nextNode = null;
12 }
13
14 class Value
15 {
16 public String code;
17 public String name;
18
19 public Value(){}
20 public Value(String code, String name){ this.code = code;this.name = name;}
21 @Override
22 public String toString() {
23 return code + "-" + name ;
24 }
25
26
27 }
28 private Element header = null;
29
30 public static void main(String[] args) {
31 SingleLinkList list = new SingleLinkList();
32 Value value1 = list.new Value("1", "java");
33 Value value2 = list.new Value("2", "c++");
34 Value value3 = list.new Value("3", "c#");
35 Value value4 = list.new Value("4", "vb");
36 list.add(value1);
37 list.add(value2);
38 list.add(value3);
39 list.add(value4);
40 System.out.println("remove vb ?" + list.remove(value4));
41 System.out.println("have c++ ?" + list.contains(value2));
42 System.out.println("have vb ?" + list.contains(value4));
43 System.out.println("list is emptry ? " + list.isEmpty());
44 System.out.println(list);
45 list.clear();
46 System.out.println("list is emptry ? " + list.isEmpty());
47 }
48
49
50 public void add(Object node) {
51 if (header == null) {
52 header = new Element();
53 header.value = null;
54 header.nextNode = null;
55 }
56 Element element = new Element();
57 element.value = node;
58 element.nextNode = header.nextNode;
59 header.nextNode = element;
60 }
61
62 public void clear() {
63 header = null;
64 }
65
66 public boolean contains(Object o) {
67 if(header==null)
68 return false;
69 Element eqEl = header.nextNode;
70 while(eqEl!=null)
71 {
72 if(eqEl.value==o)
73 {
74 return true;
75 }
76 eqEl = eqEl.nextNode;
77 }
78 return false;
79 }
80
81
82 public String toString() {
83 int size = this.size();
84 String print = "";
85 if(size==0)
86 return print;
87 for( int i = 0; i < size ;i ++)
88 {
89 print = "," + this.get(i) + print;
90 }
91 print = "[" + print.substring(1) + "]";
92 return print;
93 }
94
95
96 public Object get(int index) {
97 if(header==null)
98 return null;
99 int size = this.size();
100 if(index > (size - 1) || index < 0)
101 {
102 return null;
103 }
104 Element temp = header.nextNode;
105 int i = 0;
106 while(temp!=null)
107 {
108 if(index == i)
109 {
110 return temp.value;
111 }
112 i++;
113 temp = temp.nextNode;
114 }
115 return null;
116 }
117
118 private Element getElement(int index) {
119 if(header==null)
120 return null;
121 int size = this.size();
122 if(index > (size - 1) || index < 0)
123 {
124 return null;
125 }
126 Element temp = header.nextNode;
127 int i = 0;
128 while(temp!=null)
129 {
130 if(index == i)
131 {
132 return temp;
133 }
134 i++;
135 temp = temp.nextNode;
136 }
137 return null;
138 }
139
140
141 public boolean isEmpty() {
142 if(header==null)
143 return true;
144 else
145 return false;
146 }
147
148
149 public boolean remove(Object o) {
150 if(header==null)
151 return false;
152 Element eqPreEl = header;
153 Element eqEl = header.nextNode;
154 while(eqEl!=null)
155 {
156 if(eqEl.value==o)
157 {
158 eqPreEl.nextNode = eqEl.nextNode;
159 return true;
160 }
161 eqPreEl = eqEl;
162 eqEl = eqEl.nextNode;
163 }
164 return false;
165 }
166
167 public int size() {
168 if(header==null)
169 return 0;
170 Element temp = header.nextNode;
171 int i = 0;
172 while(temp!=null)
173 {
174 i++;
175 temp = temp.nextNode;
176 }
177 return i;
178 }
179
180 /**
181 * 检查环状单链表
182 * @return
183 */
184 public boolean checkLoop()
185 {
186 if(header==null)
187 return false;
188 int size = this.size();
189 if(size==0)
190 return false;
191 Set<Element> set = new HashSet<Element>();
192 for( int i = 0; i < size ;i ++)
193 {
194 Element el = getElement(i);
195 if(!set.contains(el)){
196 set.add(el);
197 }
198 if(set.contains(el.nextNode))
199 {
200 return true;
201 }
202 }
203 return false;
204 }
205 }
206
posted on 2010-05-16 09:39 john.huang 阅读(17257) 评论(0) 编辑 收藏 举报