仅仅只是记录自己学习的过程
链表是一种物理存储单元上非连续、非顺序的存储结构,数据元素的逻辑顺序是通过链表中的指针链接次序实现的。
先建立一个内部类
class Node{
int data; //用以保存链表中每个结点存储的数据
Node next = null;//存储下一个结点的地址
public Node(){}
public Node(int data){
this.data = data;
}
}
实现链表结点的添加
public void add(int data){
Node node = new Node(data);
n.next = node;
n = n.next;
}
实现链表结点的删除
public void delete(int data){
Node n = head;
while(n.next != null){ /*判断下一个结点值是否为当前要删除的值,
这样可以少定义一个变量去保存删除结点的前一个结点*/
if(n.next.data == data){
n.next = n.next.next;
System.out.println("删除成功!");
return;
}
n = n.next;
}
System.out.println("链表中不包含当前数值!");
}
实现链表的反转
public void reverse(){
Node n1 = head.next; //用以保存以反转那部分第一个结点
Node n2 = n1.next; //用以保存当前正要反转的结点
Node n3 = n2.next;//保存当前正要反转结点的下一个结点
n1.next = null;
while(n3 != null){
n2.next = n1;
n1 = n2;
n2 = n3;
n3 = n3.next;
}
/*
当 n3 == null 的时候n2所处的位置正好是最后一个未反转结点的位置,故退出循环后需将
n2的next指向以反转的链表头,及n1所保存的结点。
*/
n2.next = n1;
head.next = n2;
System.out.println("反转成功!");
}
实现链表的排序
/*
* 链表排序
* 新建一个dummyHead作为链表头,再从原来的链表中依次选出最小的结点加入以dummyHead为
* 头节点的链表中,最后将dummyHead.next 赋值给 head.next;
* */
public void sort(){
Node dummyHead = new Node();
Node d = dummyHead;
Node n1 = head.next;
Node n2 = head.next;
Node n3 = head;
while(head.next != null){
n1 = head.next;
n2 = head.next;
n3 = head;
while(n1 != null){
if(n1.data < n2.data){
n2 = n1;
while(n3.next != n2) n3 = n3.next;
}
n1 = n1.next;
}
n3.next = n2.next;
n2.next = null;
d.next = n2;
d = d.next;
}
head = dummyHead;
System.out.println("排序成功!");
}
以下是完整的代码
public class MyList {
private Node head = new Node();
private Node n = head;
class Node{
int data;
Node next = null;
public Node(){}
public Node(int data){
this.data = data;
}
}
public void add(int data){
Node node = new Node(data);
n.next = node;
n = n.next;
}
public void delete(int data){
Node n = head;
while(n.next != null){
if(n.next.data == data){
n.next = n.next.next;
System.out.println("删除成功!");
return;
}
n = n.next;
}
System.out.println("链表中不包含当前数值!");
}
public void show(){
Node n = head.next;
while(n != null){
System.out.print(n.data + " ");
n = n.next;
}
System.out.println();
}
public void reverse(){
Node n1 = head.next;
Node n2 = n1.next;
Node n3 = n2.next;
n1.next = null;
while(n3 != null){
n2.next = n1;
n1 = n2;
n2 = n3;
n3 = n3.next;
}
n2.next = n1;
head.next = n2;
System.out.println("反转成功!");
}
/*
* 链表排序
* 新建一个dummyHead作为链表头,再从原来的链表中依次选出最小的结点加入以dummyHead为
* 头节点的链表中,最后将dummyHead.next 赋值给 head.next;
* */
public void sort(){
Node dummyHead = new Node();
Node d = dummyHead;
Node n1 = head.next;
Node n2 = head.next;
Node n3 = head;
while(head.next != null){
n1 = head.next;
n2 = head.next;
n3 = head;
while(n1 != null){
if(n1.data < n2.data){
n2 = n1;
while(n3.next != n2) n3 = n3.next;
}
n1 = n1.next;
}
n3.next = n2.next;
n2.next = null;
d.next = n2;
d = d.next;
}
head = dummyHead;
System.out.println("排序成功!");
}
public static void main(String[] args) {
//创建一个链表
MyList myList = new MyList();
myList.add(10);
myList.add(9);
myList.add(6);
myList.add(8);
myList.add(5);
myList.show();
myList.delete(10);
myList.show();
myList.reverse();
myList.show();
myList.sort();
myList.show();
}
}
实现结果