关于java链表的增加删除修改查询 以及反转 倒序输出的代码实现
import java.util.Stack;
/**
* @Author: taoqianlilang
* @Description:
* @Date: Created in 11:31 2020/4/3
* @Modified By:
*/
class Node{
public int no;
public String name;
public String nickName;
public Node next;
public Node(int no, String name, String nickName) {
this.no = no;
this.name = name;
this.nickName = nickName;
}
public Node() {
}
@Override
public String toString() {
return "Node{" +
"no=" + no +
", name='" + name + '\'' +
", nickName='" + nickName + '\'' + '}';
}
}
class SingLinkedList{
//定义一个头节点,不要动不存放任何数据。
/**
* head指针头
*/
public static Node head=new Node(0,"","");
/**
* @param node
*/
public void addNode(Node node){
Node temp=head;
//头节点不能动,新建一个指着头节点
while (true){
if (temp.next==null){
//为空就到末尾了
break;
}//不为空就后移动
temp=temp.next;
} //退出循环是就指向链表的最后一个节点了
//把新增加的节点添加到这个后面。
temp.next=node;
}
public void addNode2(Node node){
//头节点不动
Node temp =head;
//查一下标志添加的节点是否纯在,默认falas
Boolean flag=false;
while (true){
if (temp.next==null){
break;
}
if (temp.next.no>node.no){
break;
}
if (temp.next.no==node.no){
flag=true;
break;
}
temp=temp.next;
}
if (flag){
System.out.println("编号纯在");
}else{
node.next=temp.next;
temp.next=node;
}
}
public void uppDate(Node node){
if (node.next==null){
System.out.println("链表为空");
}
Boolean flag=false;
Node temp=head.next;
while (true){
if (temp.next==null){
break;
}
if (temp.no==node.no){
//找到修改节点
flag=true;
break;
}
temp=temp.next;
}
if (flag){
temp.name=node.name;
}
}
public void del(int no){
if (head.next==null){
return;
}
//找no所对应的节点是否存在
Boolean flag=false;
Node temp=head;
while (true){
if (temp.next==null){
break;
}
if (temp.next.no==no){
// 如果找到
flag=true;
break;
}
temp=temp.next;
}
if (flag){
temp.next=temp.next.next;
}else {
System.out.println("没有这个节点");
}
}
public int getLength(Node head) {
if (head == null) {
System.out.println("空链表 0");
return 0;
}
int count = 0;
Node temp = head.next;
while (true) {
count++;
if (temp.next == null) {
break;
}
temp = temp.next;
}
return count;
}
/**
* 查询倒数第k个节点信息
*/
public Node find(int k){
//获得链表长度
int length=getLength(head);
Node temp=head.next;
for (int i=0;i<length-k;i++){
temp=temp.next;
}
return temp;
}
public Node reverse(){
if (head==null){
return null;
}
Node Temp=null;
Node pre=null;
Node cut=head.next;
while (cut!=null){
Temp=cut.next;
cut.next=pre;
pre=cut;
cut=Temp;
}
Node hd=new Node();
hd.next=pre;
return hd;
}
/**
* 倒序输出链表
*/
public void end(){
Node temp=head.next;
Stack<Node> stack=new Stack<Node>();
while (temp!=null){
stack.push(temp);
temp=temp.next;
}
while (!stack.isEmpty()){
System.out.println(stack.pop());
}
}
public void showList(Node node){
//判断链表是否为空
if (node.next==null){
System.out.println("链表为空");
return;
}
//不为空至少有一个数据
Node temp=node.next;
while (true){
if (temp==null){
break;
}
//输出节点信息
System.out.println(temp);
//temp节点后移动
temp=temp.next;
}
}
}
/**
* @author taoqianlilang
*/
public class SingLinkedListDemo {
public static void main(String[] args) {
Node node1 = new Node(1, "宋江", "及时雨");
Node node2 = new Node(2, "吴君怡", "牛逼");
Node node3 = new Node(3, "吴用", "智多星");
Node node4 = new Node(4, "林聪", "豹子头");
SingLinkedList list = new SingLinkedList();
list.addNode(node1);
list.addNode(node2);
list.addNode(node3);
list.addNode(node4);
list.end();
}
}