有时间的话,每天写一点儿代码,不然真的会生疏的。
今天是第一天,希望能够起好头。
下面是java实现这个链表的功能,完全用java代码写的,可不是用现成的东西哦
首先,这个是我自己定义的节点了
public class Node {
//定义节点的内容,我用了Object,什么都可以添加了
private Object info = null;
private Node next = null;
//构造函数
public Node() {
}
public Node(Object info) {
this.info = info;
}
//可以得到Node的信息,貌似没什么用。。。
public Object getInfo() {
return info;
}
public void setInfo(Object node) {
this.info = info;
}
public void setNext(Node next) {
this.next = next;
}
public Node getNext() {
return next;
};
//用于打印的时候,这个有用的
@Override
public String toString() {
// TODO Auto-generated method stub
return info.toString();
}
}
接下来就是链表的实现了
public class ChinedList {
//两个记录头尾的节点,我的代码好像尾节点也没什么用。。。
private Node mhead = null;
private Node mtail = null;
private String mErrLog = null;
public ChinedList(Node head) {
this.mhead = head;
}
public ChinedList() {
}
public String getErrLog() {
return mErrLog;
}
//在某个地方插入节点,我还判断了一下这个东西,小了就头插入,大了嘛,就尾插入了
public void insert(int index, Object ob) {
Node node = new Node(ob);
if (mhead == null) {
mhead = node;
mtail = node;
} else if (index <= 0) {
node.setNext(mhead);
mhead = node;
mErrLog = "index is less than zero";
} else {
Node tmp = mhead;
int i = index;
while (i-- > 0) {
tmp = tmp.getNext();
if (tmp.getNext() == null) {
mErrLog = "index is out of size";
break;
}
}
Node oldNext = tmp.getNext();
tmp.setNext(node);
node.setNext(oldNext);
}
}
//出栈了
public Node pop() {
Node tmp = mhead;
if (mhead == null) {
mErrLog = "No content";
} else {
mhead = mhead.getNext();
}
return tmp;
}
//移除某个节点了
public Node remove(int index) {
if (mhead == null) {
return null;
}
Node tmp = mhead;
if (index <= 0) {
tmp = pop();
} else {
int i = index;
while (--i > 0) {
tmp = tmp.getNext();
if (tmp.getNext() == null) {
mErrLog = "remove beyond size";
return tmp;
}
}
Node last = tmp;
tmp = tmp.getNext();
last.setNext(tmp.getNext());
}
return tmp;
}
//添加一个节点
public void push(Object ob) {
Node node = new Node(ob);
node.setNext(null);
if (mhead == null) {
mhead = node;
mtail = node;
} else {
node.setNext(mhead);
mhead = node;
}
}
//计算里面有多少个元素
public int size() {
// TODO Auto-generated method stub
int i = 0;
Node tmp = mhead;
while (tmp != null) {
tmp = tmp.getNext();
++i;
}
return i;
}
//打印这个东西
@Override
public String toString() {
// TODO Auto-generated method stub
StringBuilder sb = new StringBuilder("{ ");
Node tmp = mhead;
while (tmp != null) {
sb.append(tmp + ",");
tmp = tmp.getNext();
}
sb.setCharAt(sb.length() - 1, ' ');
sb.append("}");
return sb.toString();
}
}
好的,接下来测试一下子。
import java.util.Date;
//专门用于测试的类,也可以添加进去了
class Info {
int i = 0;
public Info(int i) {
this.i = i;
}
public int getI() {
return i;
}
public void setI(int i) {
this.i = i;
}
@Override
public String toString() {
// TODO Auto-generated method stub
return i + "";
}
}
public class TestList {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Info in1 = new Info(123);
Info in3 = new Info(589);
Info in2 = in1;
in1 = in3;
ChinedList lst = new ChinedList(new Node(in1));
lst.push(in3);
lst.push(in2);
lst.push(234);
lst.push("bugforme");
System.out.println(lst);
lst.pop();
System.out.println(lst);
lst.insert(8, "杜鹃花工作室");
System.out.println(lst);
lst.insert(2, new Date());
System.out.println(lst);
lst.insert(0, "Bill Gates");
System.out.println(lst);
System.out.println(lst.remove(2));
System.out.println(lst);
System.out.println("the list's size is :" + lst.size());
//--------------------------------
ChinedList lst2 = new ChinedList();
lst2.pop();
lst2.remove(3);
System.out.println(lst2);
System.out.println("the list's size is :" + lst2.size());
}
}
有问题可以问,我用的node什么都可以加入进去,不过最好还是用泛型,比较规范一点儿,不过打印结果的时候注意toString覆盖一下子哦。