The problem of adding, deleting, modifying and checking single chain table

Keywords: Java

package com.atguigu.linkedList;

import java.util.Stack;

public class SingleLinkedListDemo {
public static void main(String[] args) {
//Conduct the test
//Create node first
HeroNode heroNode1 = new HeroNode(1, "Songjiang", "timely rain");
HeroNode heroNode2 = new HeroNode(2, "Lu Junyi", "Yu Qilin");
HeroNode heroNode3 = new HeroNode(3, "useless", "wisdom star");
HeroNode heroNode4 = new HeroNode(4, "Linchong", "leopard head");

	SingleLinkedList singleLinkedList=new SingleLinkedList();
	
	singleLinkedList.addByOrder(heroNode1);
	singleLinkedList.addByOrder(heroNode3);
	singleLinkedList.addByOrder(heroNode4);
	singleLinkedList.addByOrder(heroNode2);
	//singleLinkedList.addByOrder(heroNode2);
	
	/*System.out.println("List before modification);
	singleLinkedList.list();
	
	HeroNode newHeroNode=new HeroNode(2,"Xiao Lu, Yu Qilin -- ");
	singleLinkedList.update(newHeroNode);
	
	System.out.println("Modified linked list ');
	singleLinkedList.list();
	
	singleLinkedList.del(1);
	singleLinkedList.del(4);
	System.out.println("Deleted linked list ');
	singleLinkedList.list();
	
	//Test the number of valid nodes in a single chain table
	System.out.println("Number of valid nodes: "+ getLength(singleLinkedList.getHead()));
	//Test the last K nodes
	HeroNode res=findLastIndexNode(singleLinkedList.getHead(), 2);
	System.out.println("res:"+res);*/
	System.out.println("List before reversal");
	singleLinkedList.list();
	//Inverted list
/*	System.out.println("Inverted list ');
	reversetList(singleLinkedList.getHead());
	singleLinkedList.list();*/
	
	System.out.println("Print single chain table in reverse order");
	reversePrint(singleLinkedList.getHead());
}

//Print data in single chain table in reverse order
//With the data structure of stack, each node is pressed into the stack, and then the reverse printing is realized by using the advanced and backward properties of stack
public static void reversePrint(HeroNode head) {
	if(head.next==null) {
		System.out.println("Empty table");
	}
	//Create a stack and push each node into the stack
	Stack<HeroNode>stack=new Stack<HeroNode>();
	HeroNode cur=head.next;
	//Stack linked list nodes
	while(cur!=null) {
		stack.push(cur);
		cur=cur.next;
	}
	//Nodes in print stack
	while(stack.size()>0) {
		System.out.println(stack.pop());
	}
}


//Reverse single chain table
public static void reversetList(HeroNode head) {
	//If the current linked list is empty or has only one node, there is no need to reverse
	if(head.next==null||head.next.next==null) {
		return;
	}
	HeroNode cur=head.next;
	HeroNode next=null;//Point to the next node of the current node
	HeroNode reverseHead=new HeroNode(0,"","");
	//Traverse the original list, and take out each node after traversing it
	//And put it at the front of the new linked list reverseHead
	while(cur!=null) {
		next=cur.next;
		cur.next=reverseHead.next;
		reverseHead.next=cur;
		cur=next;
	}
	head.next=reverseHead.next;
}


//Find the k-th node in the single chain table [Sina interview questions]
//1 write a method that accepts both the head node and the index
//2 index means the last index node
//3 get the length of the list first
//After getting the table length size, we can start to traverse the first valid node of the linked list (size index) times, and then we can get
//5 if found, return the node, otherwise return Null

public static HeroNode findLastIndexNode(HeroNode head,int index) {
	if(head.next==null) {
		return null;//Can't find
	}
	int size=getLength(head);
	if(index<=0||index>size) {
		return null;
	}
	
	HeroNode cur=head.next;
	for(int i=0;i<size-index;i++) {
		cur=cur.next;
	}
	return cur;
}


//Get the number of nodes in a single chain table (if it is a header node, it is not necessary to count the header nodes)
public static int getLength(HeroNode head) {
	if(head.next==null) {
		return 0;
	}
	int length=0;
	HeroNode cur=head.next;
	while(cur!=null) {
		length++;
		cur=cur.next;
	}
	return length;
}

}

//Define a SingleLinkedList to manage our nodes
class SingleLinkedList{
//First, initialize a header node, and keep the header node still (in order to find the first position of the linked list forever)
private HeroNode head=new HeroNode(0,"","");

public HeroNode getHead() {
	return head;
}

//Add node to single chain table
//Thinking, when the numbering sequence is not considered
// 1 find the last node of the current linked list
// 2 point the Next of the last node to the newly added node
public void add(HeroNode heroNode) {
	//Because the head node cannot move, we need a secondary node, temp
	HeroNode temp=head;
	//Traverse the list to find the last node
	while(true) {
		if(temp.next==null) {
			break;
		}
		//If you don't find the last, just move back
		temp=temp.next;
	}
	//When exiting the while loop, temp points to the end of the list
	temp.next=heroNode;
}
//Display linked list
public void list() {
	//Judge whether the list is empty
	if(head.next==null) {
		System.out.println("Linked list is empty.");
		return;
	}
	//Because the head node cannot move, we need an auxiliary variable to traverse
	HeroNode temp=head.next;
	while(true) {
		if(temp==null) {
			//At the end of the list
			break;
		}
		//Output linked list information
		System.out.println(temp);
		//Move temp back one
		temp=temp.next;
	}
}

//The second way is to add nodes according to the node sequence number
public void addByOrder(HeroNode heroNode) {
	//The head node can't move, but it can still find the added position through a secondary node
	//Because this is a single chain table, the auxiliary node we are looking for should be the one we need
	//Previous position of insertion position
	HeroNode temp=head;
	boolean flag=false;//Identify whether the added number exists. The default value is false
	while(true) {
		if(temp.next==null) {//It indicates that temp is at the end of the list. Of course, the list may be empty
			break;			
		}
		if(temp.next.no>heroNode.no) {
			//Find insertion location
			break;
		}
		else if(temp.next.no==heroNode.no) {
			flag=true;//Description number already exists
			break;
		}
		temp=temp.next;
	}
	if(flag) {
		System.out.printf("number%d It already exists and cannot be added repeatedly",heroNode.no);
	}else {
		//Insert into linked list
		heroNode.next=temp.next;
		temp.next=heroNode;
	}
}

//Modify node information according to no number, i.e. no cannot be invalid
public void update(HeroNode newHeroNode) {
	if(head.next==null) {
		System.out.println("Empty table");
		return;
		
	}
	HeroNode temp=head.next;
	boolean flag=false;
	while(true) {
		if(temp==null) {
			break;//We've traversed the list
		}
		if(temp.no==newHeroNode.no) {
			flag=true;
			break;
		}
		temp=temp.next;
	}
	//Judge whether the node to be modified is found according to the flag
	if(flag) {
		temp.name=newHeroNode.name;
		temp.nickName=newHeroNode.nickName;
	}
	else {
		System.out.printf("No number found%d Node",newHeroNode.no);
	}
}
//Output node
//1 head node cannot be moved. It needs an auxiliary node, temp
//2 when we compare, we use temp.next.no to compare with no of the node
public void del(int no) {
	HeroNode temp=head;
	boolean flag=false;
	while(true) {
		if(temp.next==null) {
			break;
		}
		if(temp.next.no==no) {
			//Eureka
			flag=true;
			break;
		}
		temp=temp.next;
	}
	if(flag) {
		//Can export
		temp.next=temp.next.next;
	}else {
		System.out.printf("The node to be deleted does not exist%d",no);
	}
}

}

//Define a single chain table node, and each HeroNode object is a node
class HeroNode{
public int no;
public String name;
public String nickName;
public HeroNode next; / / points to the next node

//constructor
public HeroNode() {
	
}
public HeroNode(int no,String name,String nickName) {
	this.no=no;
	this.name=name;
	this.nickName=nickName;
}
@Override
public String toString() {
	return "HeroNode [no=" + no + ", name=" + name + ", nickName=" + nickName + "]";
}

}
//The code is written by Mr. Han. Share the good things with you

Published 3 original articles, won praise 2, visited 800
Private letter follow

Posted by zartzar on Mon, 27 Jan 2020 07:51:38 -0800