链表是以节点的方式来存储,是链式存储
注:添加、删除、修改、查询有效结点个数、输出倒数第几个结点、
反向遍历(会改变原来的链表)、反向遍历(栈)、合并两个链表并保持有序
import java.util.ArrayList;
import java.util.List;
import java.util.Stack;
public class eee {
public static void main(String[] args) {
PLinkedList p = new PLinkedList();
p.add(new Node(6, "zhangsan"));
p.add(new Node(2, "lisi"));
p.add(new Node(5, "wangwu"));
p.add(new Node(1, "zhaoliu"));
p.add(new Node(3, "qiqiqi"));
p.del(5);
p.set(3, "111");
System.out.println("遍历链表");
p.show(p.getHead());
System.out.println("链表有效数据");
System.out.println(p.size());
System.out.println("输出倒数第几个结点");
System.out.println(p.search(2));
System.out.println("栈:反向遍历链表1");
p.stacknode();
System.out.println("反向遍历链表2");
p.show(p.rollback());
System.out.println("p2合并p并遍历:");
PLinkedList p2 = new PLinkedList();
p2.add(new Node(8, "balala"));
p2.add(new Node(7, "qilala"));
List list = p.nodeList();
for (Object object : list) {
p2.add((Node) object);
}
p2.show(p2.getHead());
}
}
class PLinkedList {
private Node head = new Node(0, "");
// 把结点添加到一个List并 返回
public List nodeList() {
Node temp = head;
List list = new ArrayList<Node>();
while (temp.next != null) {
list.add(temp.next);
temp = temp.next;
}
return list;
}
// 添加
public void add(Node node) {
// 头节点不能更改,创建一个辅助变量
Node temp = head;
while (temp.next != null) {
// id 相同的不能添加
if (temp.next.id == node.id) {
System.out.println("添加失败");
return;
}
// 按顺序添加
if (temp.next.id > node.id) {
node.next = temp.next;
temp.next = node;
return;
}
temp = temp.next;
}
temp.next = node;
}
// 删除
public void del(int id) {
Node temp = head;
try {
while (temp.next.id != id) {
temp = temp.next;
}
temp.next = temp.next.next;
} catch (Exception e) {
// TODO: handle exception
if (temp.next == null) {
System.err.println("删除失败");
}
}
}
// 修改
public void set(int id, String name) {
Node temp = head;
try {
while (temp.next.id != id) {
temp = temp.next;
}
temp.next.name = name;
} catch (Exception e) {
// TODO: handle exception
if (temp.next == null) {
System.err.println("修改失败");
}
}
}
// 遍历单链表
public void show(Node node) {
if (node.next == null) {
System.out.println("为空");
return;
}
Node temp = node.next;
while (true) {
System.out.println(temp);
if (temp.next == null) {
break;
}
temp = temp.next;
}
}
// 返回单链表有效节点个数
public int size() {
int count = 0;
Node temp = head;
while (temp.next != null) {
count++;
temp = temp.next;
}
return count;
}
// 返回倒数第 i 个结点
public Node search(int i) {
Node temp = head;
if (0 < i && i <= size()) {
for (int j = 0; j < size() - i + 1; j++) {
temp = temp.next;
}
return temp;
} else {
System.out.println("查询错误");
return null;
}
}
// 返回 反转链表
public Node rollback() {
if (head.next == null && head.next.next == null) {
return head;
}
Node headNode = new Node(0, "");
Node temp = head.next;
Node sup;
while (temp != null) {
sup = temp.next;
temp.next = headNode.next;
headNode.next = temp;
temp = sup;
}
head.next = headNode.next;
return head;
}
// 反向遍历
public void stacknode() {
Node temp = head;
Stack s = new Stack<>();
while (temp.next != null) {
s.push(temp.next);
temp = temp.next;
}
while (s.size() > 0) {
System.out.println(s.pop());
}
}
public Node getHead() {
return head;
}
}
// 节点类
class Node {
public int id;
public String name;
public Node next;
public Node(int id, String name) {
this.id = id;
this.name = name;
}
@Override
public String toString() {
return "Node [id=" + id + ", name=" + name + "]";
}
}
输出:
遍历链表
Node [id=1, name=zhaoliu]
Node [id=2, name=lisi]
Node [id=3, name=111]
Node [id=6, name=zhangsan]
链表有效数据
4
输出倒数第几个结点
Node [id=3, name=111]
栈:反向遍历链表1
Node [id=6, name=zhangsan]
Node [id=3, name=111]
Node [id=2, name=lisi]
Node [id=1, name=zhaoliu]
反向遍历链表2
Node [id=6, name=zhangsan]
Node [id=3, name=111]
Node [id=2, name=lisi]
Node [id=1, name=zhaoliu]
p2合并p并遍历:
Node [id=1, name=zhaoliu]
Node [id=2, name=lisi]
Node [id=3, name=111]
Node [id=6, name=zhangsan]
Node [id=7, name=qilala]
Node [id=8, name=balala]