Linux操作系统课程论文.doc
《Linux操作系统》课程论文
论文题目Linux下用java实现双链表学生姓名(学号)柳志东
200811621212所在学院信息学院所在班级计科1082指导教师彭伟民提交时间2011年 11 月 10日成绩
目 录
目录
1程序名称3
2程序的算法思想3
3源代码分析3
3.1链表的接口3
3.2链表的接口的实现4
3.3节点类6
4运行结果与总结6
参考文献7
程序名称
在linux下用java实现双链表的算法,双(向)链表中有两条方向不同的链,即每个结点中除next域存放后继点地址外,还增加一个指向其直接前趋的指针域priorpackage com.doubleLink.alg.dao;
import com.doubleLink.alg.node.Node;
public interface DoubleLink {
public int addFirst(Node node);
public int addLast(Node node);
public boolean isEmpty();
public int delete(Node node);
public void print();
public void insertBefore(Node target,Node node);
public int search(Node node);
}
链表的接口的实现
在接口实现中,定义了头节点head,尾节点tail和当前链表的大小size,
1.初始化链表:
public DoubleLinkImp(){
head=new Node(0);
tail=new Node(0);
head.setNext(tail);
tail.setPre(head);
size=0;
}
2.把新节点插入到head之后的位置
public int addFirst(Node node) {
try {
node.setNext(head.getNext());
node.setPre(head);
head.getNext().setPre(node);
head.setNext(node);
size++;
return 1;
} catch (Exception e) {
return 0;
}
}
3.打印链表节点
public void print() {
Node temp=head.getNext();
while(temp!=null&&temp.getNext()!=null){
System.out.print(temp.getData()+"--");
temp=temp.getNext();
}
}
4.查找节点
public int search(Node node) {
if(isEmpty())
return 0;
Node temp=head.getNext();
while(temp!=null&&temp.getNext()!=null){
if(temp.getData()==node.getData()){
return 1;
}
temp=temp.getNext();
}
return 0;
}
节点类
在节点类中定义了next pre data基本的属性和它们的get,set方法,用以操作数据变量。
运行结果与总结
新建一个test类,用以测试链表的正确性,设计如下执行程序
/**
* @param args
* @author lzd
*
*/
public static void main(String[] args) {
DoubleLinkImp db=new DoubleLinkImp();//初始化链表
Node first=new Node(1);//初始化节点
Node last=new Node(2);
Node third=new Node(3);
Node forth=new Node(4);
Node insert=new Node(5);
db.addFirst(first);//添加节点
db.addLast(last);
db.addLast(third);
db.addLast(forth);
db.print();//打印当前链表
System.out.println("");
db.de