您的位置:首页 > 编程语言 > Java开发

Java中单链表的部分操作总结

2016-05-24 16:58 555 查看
1.定义数据类Node 来存储结点信息

public class Node {
Node next = null;
int data;

public Node(int data) {
this.data = data;
}

public String toString() {
return this.data + "";
}
}


2.单链表操作代码如下:

import java.util.Hashtable;

public class MyLinkedList {
static Node head = null;

public static void main(String[] args) {
MyLinkedList list = new MyLinkedList();
list.add(5);
list.add(3);
list.add(2);
list.add(3);
System.out.print("length:" + list.length() + "\n");
list.print();
/*
* 删除指定点
*/
list.deleteNode(4);
System.out.print("删除指定点后  length:" + list.length() + "\n");
list.print();
/*
* 指定点处插入数据
*/
list.addNode(7, 8);
System.out.print("指定点处插入数据后 length:" + list.length() + "\n");
list.print();
/*
* 排序
*/
list.orderList();
list.print();
/*
* 刪除重复数据
*/
list.deleteDuplecate2(head);
list.print();
/*
* 找出单链表中倒数第K个元素
*/
System.out.print(list.findElemFromEnd(head, 3));
/*
* 单链表的反转
*/
list.ReverseIteratively(head);
list.print();
/*
* 从尾到头输出单链表
*/
list.printListRevaesely(head);
list.print();
/*
* 寻找单链表的中间结点
*/
System.out.println(list.searchNode(head));

}

/*
* 刪除链表中重复数据:方法一 时间复杂度低,但是需要额外的空间
*/
public void deleteDuplecate1(Node head) {
Hashtable<Integer, Integer> table = new Hashtable<Integer, Integer>();
Node tmp = head;
Node pre = null;
while (tmp != null) {
if (table.containsKey(tmp.data)) {
pre.next = tmp.next;

} else {
table.put(tmp.data, 1);
pre = tmp;
}
tmp = tmp.next;
}
}

/*
* 刪除链表中重复数据:方法二 不需要额外的空间, 时间复杂度高
*/
public void deleteDuplecate2(Node head) {
Node p = head;
while (p != null) {
Node q = p;
while (q.next != null) {
if (q.next.data == p.data) {
q.next = q.next.next;
} else {
q = q.next;
}
}
p = p.next;
}
}

/*
* 找出单链表中的倒数第K个元素
*/
public Node findElemFromEnd(Node head, int k) {
if (k < 1 || k > length()) {
return null;
}
Node p1 = head;
Node p2 = head;
for (int i = 0; i < k - 1; i++) {
p1 = p1.next;
}
while (p1 != null) {
p1 = p1.next;
p2 = p2.next;
}

return p2;

}

/*
* 找出单链表的中间结点
*/
public Node searchNode(Node head) {
Node p = this.head;
Node q = this.head;
while (p != null && p.next != null && p.next.next != null) {
p = p.next.next;
q = q.next;
}
return q;

}

/*
* 向链表尾部添加数据
*/
public void add(int data) {
Node newNode = new Node(data);
if (head == null) {
head = newNode;
return;
}
Node tmp = head;
while (tmp.next != null) {
tmp = tmp.next;
}
tmp.next = newNode;
}

/*
* 在第index个结点插入数据
*/
public Boolean addNode(int index, int data) {
Node newNode = new Node(data);
if (index < 1) {
return false;
}
if (index == 1) {
newNode.next = head;
head = newNode;
return true;
}
if (index > length()) {
add(data);
return true;
}
int i = 2;
Node preNode = head;
Node curNode = preNode.next;
while (curNode != null) {
if (i == index) {
preNode.next = newNode;
newNode.next = curNode;
return true;
}
preNode = curNode;
curNode = curNode.next;
i++;
}
return true;

}

/*
* 单链表的反转
*/
public void ReverseIteratively(Node head) {
Node pReversedHead = head;
Node pNode = head;
Node pPrev = null;
while (pNode != null) {
Node pNext = pNode.next;
if (pNext == null) {
pReversedHead = pNode;
}
pNode.next = pPrev;
pPrev = pNode;
pNode = pNext;
}
this.head = pReversedHead;
}

/*
* 从尾到头输出单链表:采用递归实现,单链表实际上并没有反转
*/
public void printListRevaesely(Node pListHead) {
if (pListHead != null) {
printListRevaesely(pListHead.next);
System.out.println(pListHead.data);
}

}

/*
* 链表中的排序
*/
public void orderList() {
Node nextNode = null;
Node curNode = head;
int tmp = 0;
while (curNode.next != null) {
nextNode = curNode.next;
while (nextNode != null) {
if (curNode.data > nextNode.data) {
tmp = curNode.data;
curNode.data = nextNode.data;
nextNode.data = tmp;
}
nextNode = nextNode.next;
}
curNode = curNode.next;
}

}

/*
* 删除第index个结点
*/
public Boolean deleteNode(int index) {

if (index < 1 || index > length()) {
return false;
}

if (index == 1) {
head = head.next;
return true;
}
int i = 2;
Node preNode = head;
Node curNode = preNode.next;
while (curNode != null) {
if (i == index) {
preNode.next = curNode.next;
return true;
}
preNode = curNode;
curNode = curNode.next;
i++;
}
return true;
}

/*
* 返回结点的长度
*/
public int length() {
int length = 0;
Node tmp = head;
while (tmp != null) {
length++;
tmp = tmp.next;

}

return length;

}

/*
* 打印链表的所有节点
*/
public void print() {
Node tmp = head;
while (tmp != null) {
System.out.print(tmp.data + "——>");
tmp = tmp.next;
}
System.out.print("尾部" + "\n");

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