package Link;
interface Link{
void add(Object obj); //添加节点
boolean remove(int index); //删除下标为index的节点
boolean contains(Object obj); //判断链表是否包含值为obj的节点
int indexOf(Object obj); //查找值为obj的节点的下标
boolean set(int index,Object obj); //将下标为index的节点的值改为obj
Object get(int index); //获取下标为index的节点的值
int length(); //获取链表的长度
void clear(); //清空链表
Object[] toArray(); //将链表中节点的值转为数组
void printLink(); //打印链表
}
class Factory{
private Factory() {}
public static Link getLinkInstance() {
return new LinkImpl();
}
}
class LinkImpl implements Link{
private int size = 0;
private Node first;
private Node last;
public class Node{
private Node prev;
private Object data;
private Node next;
private Node(Node prev,Object data,Node next) {
this.prev = prev;
this.data = data;
this.next = next;
}
}
public void add(Object obj) {
Node temp = this.last;
Node newNode = new Node(temp,obj,null);
this.last = newNode;
if(temp == null) {
this.first = newNode;
}else {
temp.next = newNode;
}
this.size++;
}
public int length() {
return this.size;
}
public int indexOf(Object obj) {
int index = 0;
if(obj == null) {
for(Node temp = this.first;temp != null;temp=temp.next) {
if(temp.data == null) {
return index;
}
index++;
}
}else {
for(Node temp = this.first;temp != null;temp = temp.next) {
if(temp.data.equals(obj)) {
return index;
}
index++;
}
}
return -1;
}
private boolean isElementIndex(int index) {
return index>=0 && index<=size;
}
public void clear() {
Node temp = this.first;
for(temp = this.first;temp != null;) {
Node x = temp.next;
temp.prev = null;
temp.data = null;
temp.next = null;
temp = x;
this.size--;
}
this.first = this.last = null;
}
public Object get(int index) {
if(!isElementIndex(index)) {
return false;
}
int i=0;
for(Node temp = this.first;temp != null;temp = temp.next) {
if(i == index) {
return temp.data;
}
i++;
}
return false;
}
public boolean contains(Object obj) {
for (Node temp = this.first;temp != null;temp = temp.next) {
if(temp.data == obj) {
return true;
}
}
return false;
}
public boolean remove(int index) {
Node temp = this.first;
if(0 == index) {
this.first = temp.next;
temp = null;
this.size--;
return true;
}
Node temp2 = this.last;
if(this.size-1 == index) {
this.last = temp2.prev;
this.last.next = null;
temp2 = null;
this.size--;
return true;
}
int i = 1;
Node temp3 = this.first.next;
for(;temp3 != null;) {
Node x = temp3.next;
if(i == index){
temp3.next.prev = temp3.prev;
temp3.prev.next = temp3.next;
temp3 = null;
this.size--;
return true;
}
temp3 = x;
i++;
}
return false;
}
public boolean set(int index,Object obj) {
Node temp = this.first;
int i = 0;
for(;temp != null;temp =temp.next) {
if(i == index) {
temp.data = obj;
return true;
}
i++;
}
return false;
}
public Object[] toArray() {
Object[] result = new Object[size];
int i = 0;
for(Node temp = this.first;temp != null;temp = temp.next) {
result[i++] = temp.data;
}
return result;
}
public void printLink() {
Object[] result = this.toArray();
for(int i = 0;i < result.length;i++) {
System.out.println(result[i]);
}
}
}
public class linkLink {
public static void main(String[] args) {
Link link = Factory.getLinkInstance();
link.add("火车头");
link.add("车厢1");
link.add("车厢2");
link.add("火车尾");
// link.printLink();
//
// link.remove(2);
// link.printLink();
// link.set(3, "车厢3");
// link.printLink();
// System.out.println(link.indexOf("火车5"));
// System.out.println(link.indexOf("火车尾"));
// link.clear();
// link.printLink();
System.out.println(link.contains("火车头"));
System.out.println(link.contains("车厢5"));
}
}
Java实现双向链表
最新推荐文章于 2024-09-10 10:22:48 发布