您的位置:首页 > 理论基础 > 数据结构算法

数据结构链表(java版)的实现和思考

2014-04-23 15:10 357 查看
java的链表实现,步骤如下:
1.定义一个节点类:
//定义链表的节点类,里面专门存储下一个元素和数据
public class Node {
public Node next;
public int date;
public Node(int date) {
this.date = date;
}
public void PrintDate() {
System.out.print(date + " ");
}
}
2.定义链表类:
public class LinkedList {
private Node head;
private int Length;
public LinkedList() {
head = new Node(0);// head只是用来搜寻数据用的,所以里面存放的数据为空
Length = 0;
}
// 定义插入函数,分别表示在哪个位置和插入哪个数
public void Insert(int position, int date) {
if (position < 1 || position > Length + 1)
System.out.println("这个要求插入的数已经越界");
else {
Node temp = head;// 表示将头指针给一个中转变量
for (int i = 1; i < position; i++) {
temp = temp.next;
}
Node NewNode = new Node(date);// 定义一个新的节点,专门用来存储插入的数
NewNode.next = temp.next;
temp.next = NewNode;
Length++;
}
}
// 定义一个输出函数
public void PrintOut() {
if (Length == 0)
System.out.println("这个链表为空");
else {
Node temp = head.next;
for (int i = 1; i <= Length; i++) {
temp.PrintDate();
temp = temp.next;
}
}
}
// 定义一个删除函数
public int delete(int position) {
if (position < 1 || position > Length + 1)
System.out.println("这个删除的数越界");
Node temp = head;
for (int i = 1; i < position; i++) {
temp = temp.next;
}
int date = temp.next.date;// 保存这个被删除的数
temp.next = temp.next.next;
Length--;
return date;
}
// 得到链表中数据的长度
public int getLength() {
return Length;
}
// 继续定义一个插入函数,这个插入函数与前面插入函数的不同在于这个函数可以进行排序,有利于
// 进行下面的合并函数
public void SortLinkedList(int date) {
Node newNode = new Node(date);
if (Length == 0) {
head.next = newNode;
Length++;
} else {
Node temp = head.next;
for (int i = 1; i <= Length; i++) {
if (newNode.date <= temp.date) {
newNode.next = temp.next;
temp.next = newNode;
break;
}
if(i>=2)
temp=temp.next;//关键性一步
}
temp.next=newNode;
Length++;
}
}
// 定义一个合并函数,用来合并两个链表(用递增的方式合并)
public void CombineList(LinkedList list1, LinkedList list2) {
LinkedList listCombine = new LinkedList();
// 首先先定义两个链表的头指针中转变量
Node temp1 = list1.head.next;// 表示指向list1第一个数据
Node temp2 = list2.head.next;// 表示指向list2的第一个数据

// 表示任意一个链表的长度到达结尾时,终止循环
while (temp1 != null && temp2 != null) {
if (temp1.date <= temp2.date) {
// 在合并的链表中,我们把数据默认的情况下插入表尾
listCombine.Insert(listCombine.Length + 1, temp1.date);
temp1 = temp1.next;

} else {
listCombine.Insert(listCombine.Length + 1, temp2.date);
temp2 = temp2.next;

}
}
while (temp1 != null) {
listCombine.Insert(listCombine.getLength() + 1, temp1.date);
temp1 = temp1.next;

}
while(temp2 != null) {
listCombine.Insert(listCombine.getLength() + 1, temp2.date);
temp2 = temp2.next;

}
listCombine.PrintOut();

}

}
3.测试代码与截图:
public class TestLinkedList {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
LinkedList list=new LinkedList();
for(int i=1,j=1;i<10;i++,j++){
list.Insert(i, j);//测试插入是否可以实现,
}
list.PrintOut();

LinkedList list1=new LinkedList();
LinkedList list2=new LinkedList();

for(int i=0;i<10;i++){
list1.SortLinkedList(i);
}
for(int j=10;j<20;j++){
list2.SortLinkedList(j);
}

//测试使用第二种插入方法,可不可以进行排序
list1.PrintOut();
System.out.println();
list2.PrintOut();
System.out.println();

//测试是否可以进行合并
new LinkedList().CombineList(list1,list2);

}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  java数据结构