用java构建循环链表并作为存储结构
1个回答
展开全部
package arithmetic.structure;
/**
* @author 作者 :popl
*/
public class LoopDoubleList<E> {
/**
* @param args
*/
public static void main(String[] args) {{
LoopDoubleList<String> m = new LoopDoubleList<String>();
m.addLast("sad");
m.addLast("阿斯顿");
m.addLast("中国");
m.addFirst("lm");
m.addFirst("咖啡色");
for (int i = 0; i < m.size(); i++)
System.out.println(m.get(i));
// System.out.println(m.get(8));
m.remove(2);
m.add(2, "jia");
for (int i = 0; i < m.size(); i++)
System.out.println(m.get(i));
// m.finalize();
System.gc();}
while(true){System.gc();}
}
private Node hand;
/**
* 节点类
*/
private class Node {
private E data;
private Node prior;
private Node next;
public Node(E data) {
this.data = data;
this.next = null;
this.prior = null;
}
public void finalize(){
try {
System.out.println("节点回收" + data);
super.finalize();
} catch (Throwable e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
/**
* 在末尾添加元素
*/
public void addLast(E data) {
Node n = new Node(data);
if (hand == null) {
hand = n;
hand.next = hand;
hand.prior = hand;
} else {
Node tail = hand.prior;
tail.next = n;
n.prior = tail;
n.next = hand;
hand.prior = n;
}
}
/**
* 在头部添加元素
*/
public void addFirst(E data) {
Node n = new Node(data);
if (hand == null) {
hand = n;
hand.next = hand;
hand.prior = hand;
} else {
n.next = hand;
n.prior = hand.prior;
hand.prior.next = n;
hand.prior = n;
hand = n;
}
}
/**
* 在指定位置添加
*/
public void add(int index, E data) {
if (index >= this.size()) {
System.out.println("不存在此长度");
}
Node temp = new Node(data);
Node p = hand;
for (int i = 0; i < index - 1; i++)
p = p.next;
temp.next = p.next;
temp.prior = p;
p.next.prior = temp;
p.next = temp;
}
/**
* 获得链表长度
*/
public int size() {
int flage = 0;
if (hand == null)
return 0;
Node p = hand.next;
while (true) {
flage++;
if (p == hand)
return flage;
p = p.next;
}
}
/**
* 在指定位置取元素
*/
public E get(int index) {
if (index >= this.size())
throw new ArrayIndexOutOfBoundsException(index);
Node p = hand;
if (index < this.size() / 2) {
for (int i = 0; i < index; i++)
p = p.next;
return p.data;
}else{
for (int i = 0; i < this.size() - index; i++)
p = p.next;
return p.data;
}
}
/**
* 删除指定位置的元素
*/
public void remove(int index) {
Node p = hand;
for (int i = 0; i < index; i++)
p = p.next;
p.next.prior = p.prior;
p.prior.next = p.next;
}
}
/**
* @author 作者 :popl
*/
public class LoopDoubleList<E> {
/**
* @param args
*/
public static void main(String[] args) {{
LoopDoubleList<String> m = new LoopDoubleList<String>();
m.addLast("sad");
m.addLast("阿斯顿");
m.addLast("中国");
m.addFirst("lm");
m.addFirst("咖啡色");
for (int i = 0; i < m.size(); i++)
System.out.println(m.get(i));
// System.out.println(m.get(8));
m.remove(2);
m.add(2, "jia");
for (int i = 0; i < m.size(); i++)
System.out.println(m.get(i));
// m.finalize();
System.gc();}
while(true){System.gc();}
}
private Node hand;
/**
* 节点类
*/
private class Node {
private E data;
private Node prior;
private Node next;
public Node(E data) {
this.data = data;
this.next = null;
this.prior = null;
}
public void finalize(){
try {
System.out.println("节点回收" + data);
super.finalize();
} catch (Throwable e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
/**
* 在末尾添加元素
*/
public void addLast(E data) {
Node n = new Node(data);
if (hand == null) {
hand = n;
hand.next = hand;
hand.prior = hand;
} else {
Node tail = hand.prior;
tail.next = n;
n.prior = tail;
n.next = hand;
hand.prior = n;
}
}
/**
* 在头部添加元素
*/
public void addFirst(E data) {
Node n = new Node(data);
if (hand == null) {
hand = n;
hand.next = hand;
hand.prior = hand;
} else {
n.next = hand;
n.prior = hand.prior;
hand.prior.next = n;
hand.prior = n;
hand = n;
}
}
/**
* 在指定位置添加
*/
public void add(int index, E data) {
if (index >= this.size()) {
System.out.println("不存在此长度");
}
Node temp = new Node(data);
Node p = hand;
for (int i = 0; i < index - 1; i++)
p = p.next;
temp.next = p.next;
temp.prior = p;
p.next.prior = temp;
p.next = temp;
}
/**
* 获得链表长度
*/
public int size() {
int flage = 0;
if (hand == null)
return 0;
Node p = hand.next;
while (true) {
flage++;
if (p == hand)
return flage;
p = p.next;
}
}
/**
* 在指定位置取元素
*/
public E get(int index) {
if (index >= this.size())
throw new ArrayIndexOutOfBoundsException(index);
Node p = hand;
if (index < this.size() / 2) {
for (int i = 0; i < index; i++)
p = p.next;
return p.data;
}else{
for (int i = 0; i < this.size() - index; i++)
p = p.next;
return p.data;
}
}
/**
* 删除指定位置的元素
*/
public void remove(int index) {
Node p = hand;
for (int i = 0; i < index; i++)
p = p.next;
p.next.prior = p.prior;
p.prior.next = p.next;
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询