Single chain table operation of leading node in Java data structure

Posted by lyonsperf on Tue, 21 Jan 2020 15:56:48 +0100

Today, I will introduce the addition, deletion, query and modification of single chain table, plus three small operations: reversing single chain table, printing single chain table nodes in reverse order, and merging two ordered chain tables. Here is a small case of using single chain table to store heroes of the marsh, but in fact, the core operations of single chain table are as follows:

First, create a node class to store the hero number, name and nickname, including the construction method and rewriting the toString method

class HeroNode{
	int no;
	String name;
	String nickname;
	HeroNode next;//Next defaults to null, pointing to the next node
	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 + "]";
	}
	
}

Create a header node without storing specific data:

	//First, initialize a header node. The header node does not move and does not store specific data
	private HeroNode head = new HeroNode(0, "", "");

Add node:
There are two methods given here. One is to add directly to the end of the chain list, the other is to insert nodes according to the number. Auxiliary pointers are used for traversal, and the other is to use auxiliary pointers to find the previous node where the position is added and then insert

	//Add node, method 1, find the last node and point to the new node next
	public void add(HeroNode heroNode) {
		//The head node cannot move, so it uses auxiliary pointer
		HeroNode temp = head;
		//Traverse the list to the end
		while(true) {
			if(temp.next==null) {
				break;
			}
			temp=temp.next;
		}
		temp.next = heroNode;
	}
	//The key to add node method 2 is to find the previous node in the add location
	public void addByOrder(HeroNode heroNode) {
		HeroNode temp = head;
		boolean flag=false;//Identify whether the added number exists, default false
		while(true) {
			if(temp.next==null) {//At the end of the list
				break;
			}
			if(temp.next.no>heroNode.no) {//Find the location and insert it after temp
				break;
			}
			else if(temp.next.no==heroNode.no) {
				flag=true;
				break;
			}
			temp = temp.next;
		}
		//Judge flag
		if(flag) {
			System.out.printf("Hero number to be inserted%d Already exist,Can not be inserted\n",heroNode.no);
		}
		else {
			heroNode.next=temp.next;
			temp.next=heroNode;
		}
	}

Delete node:
Use the auxiliary pointer to find the previous node of the node to be deleted, directly cross the node to be deleted, and make it become garbage

	//Delete node, find the previous node of the node to be deleted
	public void del(int no) {
		HeroNode temp=head;
		boolean flag=false;
		while(true) {
			if(temp.next==null) {//At the end of the list
				break;
			}
			if(temp.next.no==no) {
				//Find the previous node of the node to be deleted
				flag=true;
				break;
			}
			temp=temp.next;
		}
		if(flag) {
			temp.next=temp.next.next;
		}else {
			System.out.printf("Deleted%d Node does not exist\n",no);
		}
	}

Find node:
It can be divided into direct search and search for the last index node according to the number. The search for the last index node needs to traverse to the length index node with the for loop, and the length must be obtained by another method

	//Get the number of nodes in a single chain table
	public int getLength() {
		HeroNode cur = head.next;
		int length = 0;
		while(cur!=null) {
			length++;
			cur=cur.next;
		}
		return length;
	}
	//Find directly by number
	public HeroNode findIndexNode(int index) {

		HeroNode temp = head;
		boolean flag=false;//Identify whether the added number is found, default false
		while(true) {
			if(temp.next==null) {//At the end of the list
				break;
			}
			else if(temp.next.no==index) {
				flag=true;
				break;
			}
			temp = temp.next;
		}
		//Judge flag
		if(flag) {
			return temp.next;
		}
		else {
			System.out.printf("Hero number%d Non-existent\n",index);
		}
		return null;
	}
	//Find the last index node
	//First get the total length, and then traverse the length index from the first
	public HeroNode findLastIndexNode(int index) {

		HeroNode cur = head.next;
		if(cur==null) {
			return null;
		}
		int length = getLength();
		//Check the data first
		if(index<=0||index>length) {
			return null;
		}
		//for loop to index
		for(int i=0;i<length-index;i++) {
			cur=cur.next;
		}
		return cur;
	}

Modify node information:
Passing in a new node is similar to directly searching for a node. If it is found, it will overwrite the original node with the new node information

	//Modify node information according to no number, i.e. no number cannot be modified
	public void update(HeroNode newHeroNode) {
		//Judge whether it is empty
		if(head.next==null) {
			System.out.println("Linked list is empty.");
			return;
		}
		//Auxiliary pointer
		HeroNode temp = head.next;
		boolean flag = false;//Indicates whether the node is found
		while(true) {
			if(temp==null) {
				break;
			}
			if(temp.no==newHeroNode.no) {
				flag=true;
				break;
			}
			temp=temp.next;
		}
		if(flag) {
			temp.name=newHeroNode.name;
			temp.nickname=newHeroNode.nickname;
			
		}else {
			System.out.printf("No number found%d Cannot be modified\n",newHeroNode.no);
			
		}
	}

Reverse single chain table:
Define a new header node, traverse the original linked list, put each one in front of the previous node, and then cover the original header node with the new one to realize inversion

	//Reverse single chain table
	public void reverse() {
		HeroNode cur=head.next;
		//The list is empty or only one node does not need to be reversed
		if(cur==null||cur.next==null) {
			return;
		}
		HeroNode next = null;//Next node to store cur
		//New linked list
		HeroNode reverseHead = new HeroNode(0, "", "");
		//Traverse the original list, and each node will be taken out and placed at the front of the new list
		while(cur!=null) {
			next=cur.next;
			cur.next=reverseHead.next;//cur's next node points to the new linked list
			reverseHead.next=cur;
			cur=next;//cur backward shift
		}
		//Turn head.next to reverseHead.next to implement inversion
		head.next=reverseHead.next;
		
	}

Merge two ordered lists:
It also defines a new head node, circularly compares the number sizes of two linked lists, inserts new head nodes in turn, and finally forms a new linked list. Here, it should be noted that after traversing a linked list, another link should be added to the new linked list

	//Merge two ordered lists
	public SingleLinkedList mergeTwoLists(SingleLinkedList l1,SingleLinkedList l2)
	{
		SingleLinkedList s = new SingleLinkedList();
		HeroNode h = s.head;
		HeroNode h1 = l1.head.next;
		HeroNode h2 = l2.head.next;
		while(h1!=null&&h2!=null) {
			if(h1.no<=h2.no) {
				h.next=h1;
				h1=h1.next;
				h=h.next;
			}else {
				h.next=h2;
				h2=h2.next;
				h=h.next;
			}
		}
		while(h1!=null) {
			h.next=h1;
			h1=h1.next;
			h=h.next;
		}
		
		while(h2!=null) {
			h.next=h2;
			h2=h2.next;
			h=h.next;
		}
		return s;
	}

To print a single linked table node:
Here is divided into sequential printing and reverse printing, in which reverse printing is implemented by using the advanced stack. You can understand some push() and pop() methods of the stack

	//Display linked list
	public void list() {
		if(head.next==null) {
			System.out.println("Linked list is empty.");
			return;
		}
		HeroNode temp=head.next;
		while(true) {
			//Judge whether to the end of the list
			if(temp==null) {
				break;
			}
			//Output node information
			System.out.println(temp);
			//temp backward shift
			temp=temp.next;
		}
	}
	//Print single chain table in reverse order
	//Use the stack to push each node into the stack, and then use the advanced and backward features of the stack to achieve reverse printing
	public void reversePrint() {
		HeroNode cur = head.next;
		if(cur==null) {
			System.out.println("Linked list is empty.");
			return;
		}
		//Create a stack
		Stack<HeroNode> stack = new Stack<>();
		//Push all nodes of the list into the stack
		while(cur!=null) {
			stack.push(cur);
			cur=cur.next;
		}
		//Print the nodes in the stack
		while(stack.size()>0) {
			System.out.println(stack.pop());
		}
	}

The above are some basic operations of the single chain table of the leading node. The test can be implemented by itself

Published 3 original articles, won praise 0, visited 46
Private letter follow