Code of Java linked list

Code of Java linked list

  1. Use of single chain table
  2. The use of double linked list
  3. josephus problem
  4. Use of stack

Use of single chain table

Functions:

  1. Adding link list nodes
  2. Deletion of linked list nodes
  3. Modification of linked list node
  4. Nodes traversing linked list
  5. Get the number of nodes in a single chain table
  6. Querying the last k nodes in a single chain table
  7. Inversion of single chain table
class Data{
	private int no;
	private Data next;  //Point to next node	
	public Data(int no) {
		this.no = no;
	}
	public int getNo() {
		return no;
	}
	public void setNo(int no) {
		this.no = no;
	}
	public Data getNext() {
		return next;
	}
	public void setNext(Data next) {
		this.next = next;
	}
	@Override
	public String toString() {
		return "Data [no=" + no + "]";
	}
}
/**
 * Addition, deletion, modification and search of linked list
 * @author dell
 *
 */
class UserLinkedList{
	Data head = new Data(-1);  //Set head pointer to - 1
	/*
	 * Adding link list nodes
	 */
	public void add(Data data) {
		//Insert method 1: insert nodes in order
//		Data temp = head; / / the head node cannot be moved. Set the secondary node
//		//Last node found
//		while(true) {
//			if(temp.getNext() == null) {/ / if the next node is empty, the last node will be found
//				break;
//			}
//			temp = temp.getNext(); / / points to the next node
//		}
//		temp.setNext(data); / / add a node to the end

		//Insert method 2: insert nodes in the order from small to large
		Data temp = head;  //The head node cannot be moved. Set the auxiliary node
		//Find a location for the node to insert
		while(true) {
			if(temp.getNext() == null) {  //If the next node is empty, the last node will be found
				break;
			}
			if(temp.getNo() < data.getNo() && data.getNo() <= temp.getNext().getNo()) {  //The inserted value is greater than this node and less than or equal to the next node
				break;
			}
			temp = temp.getNext();  //Point to next node
		}
		data.setNext(temp.getNext());
		temp.setNext(data);
	}
	/*
	 * Deletion of linked list nodes
	 */
	public void delete(Data data) {
		if(head.getNext() == null) {  //Linked list is empty.
			System.out.println("Linked list is empty.");
			return ;
		}
		Data temp = head;  //The head node cannot be moved. Set the auxiliary node
		while(true) {
			if(temp.getNext() == null) {  //Ergodic list
				System.out.println("No information found");
				return ;
			}
			if(temp.getNext().getNo() == data.getNo()) {  //Find a node with the same no value as data
				temp.setNext(temp.getNext().getNext());
				return ;
			}
			temp = temp.getNext();  //Point to next node
		}
	}
	/*
	 * Modification of linked list node
	 * Modify the value of no according to Data
	 */
	public void upData(Data data, int x) {
		if(head.getNext() == null) {  //Linked list is empty.
			System.out.println("Linked list is empty.");
			return ;
		}
		Data temp = head.getNext();  //The head node cannot be moved. Set the auxiliary node
		while(true) {
			if(temp == null) {  //Ergodic list
				System.out.println("No information found");
				return ;
			}
			if(temp.getNo() == data.getNo()) {  //Find a node with the same no value as data
				temp.setNo(x);
				return ;
			}
			temp = temp.getNext();
		}
	}
	/*
	 * Nodes traversing linked list
	 */
	public void list() {
		if(head.getNext() == null) {  //Linked list is empty.
			System.out.println("Linked list is empty.");
			return ;
		}
		Data temp = head.getNext();  //Set secondary node
		//Traversal list output
		while(true) {
			if(temp == null) {  //Pointer to the end of the list
				break;
			}
			System.out.println(temp.toString());
			temp = temp.getNext();
		}
	}
	/*
	 * Get the number of nodes in a single chain table
	 */
	public int countNode() {
		Data temp = head.getNext();  //Set secondary node
		int count = 0;
		while(temp != null) {  //Pointer to the end of the list
			count++;
			temp = temp.getNext();
		}
		return count;
	}
	/*
	 * Querying the last k nodes in a single chain table
	 */
	public void reNode(int k) {
		Data temp = head.getNext();  //Set secondary node
		int i = countNode() - k;  //Define the variable to move i nodes backward for temp
		if(i < 0) {
			System.out.println("The input value is greater than the number of linked list nodes");
			return ;
		}
		while(i-- > 0) {  //temp moves i nodes backward
			temp = temp.getNext();
		}
		System.out.println("Countdown" + k + "Nodes are:" + "Data [no = " + temp.getNo() + "]");
	}
	/*
	 * Inversion of single chain table
	 */
	public void reverseNode() {
		if(head.getNext() == null || head.getNext().getNext() == null) {  //When there are only 0 or 1 nodes, you do not need to reverse
			return ;
		}
		Data temp = head.getNext();  //Set secondary node
		Data first = new Data(-1);  //Set reversed head node
		Data next = null;  //Record the next node of temp
		while(temp != null) {  //Pointer to the end of the list
			next = temp.getNext();  //Record the next node of temp, which is useful later. If you do not set this, then temp will not get the value behind the head list
			//Insert temp into the first node of the first linked list
			temp.setNext(first.getNext());  
			first.setNext(temp);
			temp = next;  //Get the next node of temp in the head linked list
		}
		head.setNext(first.getNext());
	}
}

public class CaoGao {
	public static void main(String[] args) {
		Data data1 = new Data(1);
		Data data2 = new Data(22);
		Data data3 = new Data(3);
		Data data4 = new Data(4);
		UserLinkedList userLinkedList = new UserLinkedList();
		userLinkedList.add(data1);  //Add node
		userLinkedList.add(data2);  //Add node
		userLinkedList.add(data3);  //Add node
		userLinkedList.add(data4);  //Add node
		System.out.println("Added nodes:");
		userLinkedList.list();  //Traversing node
		userLinkedList.upData(data1, 100);  //Modify data1 node information
		System.out.println("Modified node:");
		userLinkedList.list();  //Traversing node
		userLinkedList.delete(data1);  //Delete data1 node
		System.out.println("Deleted nodes:");
		userLinkedList.list();  //Traversing node
		System.out.println("Number of nodes:" + userLinkedList.countNode());  //Node number
		userLinkedList.reNode(2);
		userLinkedList.reverseNode();  //Linked list inversion
		System.out.println("Reversed node:");
		userLinkedList.list();
	}
}

Program running results:

  • Added nodes:
    Data [no=1] Data [no=3] Data [no=4] Data [no=22]
  • Modified nodes:
    Data [no=100] Data [no=3] Data [no=4] Data [no=22]
  • Deleted nodes:
    Data [no=3] Data [no=4] Data [no=22]
  • Number of nodes: 3
  • The penultimate node is:
    Data [no = 4]
  • Reversed nodes:
    Data [no=22] Data [no=4] Data [no=3]






The use of double linked list

Functions:

  1. Adding double line linked list node
  2. Deletion of double line linked list node
  3. Modification of double line linked list node
  4. Traversing the nodes of double linked list
class Data{
	private int no;
	private Data next;  //Point to next node	
	private Data pre;  //Point to previous node
	public Data(int no) {
		this.no = no;
	}
	public int getNo() {
		return no;
	}
	public void setNo(int no) {
		this.no = no;
	}
	public Data getNext() {
		return next;
	}
	public void setNext(Data next) {
		this.next = next;
	}
	public Data getPre() {
		return pre;
	}
	public void setPre(Data pre) {
		this.pre = pre;
	}
	@Override
	public String toString() {
		return "Data [no=" + no + "]";
	}
}

/*
 * Function implementation class
 */
class DoubleLinked{
	Data head = new Data(-1);  //Set head pointer
	/**
	 * Add data
	 * @param data Added objects
	 */
	public void add(Data data) {
		//Insert nodes in order
//		Data temp = head; / / set the secondary node
//		//Find the last node and insert it at the end
//		while(true) { 
//			if(temp.getNext() == null) {/ / the next node is empty. Find the last node
//				break;
//			}
//			temp = temp.getNext();
//		}
//		//Form a two-way list
//		temp.setNext(data);
//		data.setPre(temp);
		
		//Insert nodes by size
		Data temp = head;  //Set secondary node
		//Locate it and insert it in place
		while(true) {
			if(temp.getNext() == null) {  //The next node is empty. The last node was found. No suitable location was found
				temp.setNext(data);
				data.setPre(temp);
				return ;
			}
			//The inserted value is greater than this node and less than or equal to the next node
			if(temp.getNo() < data.getNo() && data.getNo() <= temp.getNext().getNo()) {  
				break;
			}
			temp = temp.getNext();
		}
		data.setNext(temp.getNext());
		temp.getNext().setPre(data);
		temp.setNext(data);
		data.setPre(temp);
	}
	
	/**
	 * Modification of linked list node
	 * @param id  no to be modified
	 * @param x  Modified no
	 */
	public void upData(int id, int x) {
		if(head.getNext() == null) {  //Linked list is empty.
			System.out.println("Linked list is empty.");
			return ;
		}
		Data temp = head.getNext();  //The head node cannot be moved. Set the auxiliary node
		while(true) {
			if(temp == null) {  //Ergodic list
				System.out.println("No information found");
				return ;
			}
			if(temp.getNo() == id) {  //Find a node with the same no value as data
				temp.setNo(x);
				return ;
			}
			temp = temp.getNext();
			
		}
	}
	
	/**
	 * Delete Linked List 
	 * @param id no to be modified
	 */
	public void delete(int id) {
		if(head.getNext() == null) {  //Linked list is empty.
			System.out.println("Linked list is empty.");
			return ;
		}
		Data temp = head.getNext();  //Set secondary node
		while(true) {
			if(temp == null) {  //It's the end of the tour
				System.out.println("No information found");
				return ;
			}
			if(temp.getNo() == id) {  //Eureka
				if(temp.getNext() == null) {  //Next node is empty
					temp.getPre().setNext(null);
					break;
				}
				//Deleted statement
				temp.getPre().setNext(temp.getNext());
				temp.getNext().setPre(temp.getPre());
				return ;
			}
			temp = temp.getNext();
		}
	}
	
	/**
	 * Traversing linked list
	 */
	public void list() {
		if(head.getNext() == null) {  //Linked list is empty.
			System.out.println("Linked list is empty.");
			return ;
		}
		Data temp = head.getNext();  //Set secondary node
		//Traversal list output
		while(true) {
			if(temp == null) {  //Pointer to the end of the list
				break;
			}
			System.out.println(temp.toString());
			temp = temp.getNext();
		}
	}
}

/*
 * main function
 */
public class CaoGao {
	public static void main(String[] args) {
		Data data1 = new Data(1);
		Data data2 = new Data(22);
		Data data3 = new Data(3);
		Data data4 = new Data(4);
		DoubleLinked doubleLinked = new DoubleLinked();
		doubleLinked.add(data1);  //Add node
		doubleLinked.add(data2);  //Add node
		doubleLinked.add(data3);  //Add node
		doubleLinked.add(data4);  //Add node
		System.out.println("Added nodes:");
		doubleLinked.list();  //Traversing node
		
		//Modify node
		System.out.println("Modified node:");
		doubleLinked.upData(1, 5);  //Modify node
		doubleLinked.list();  //Traversing node
		
		//Delete node
		System.out.println("Deleted nodes:");
		doubleLinked.delete(22);  //Delete node
		doubleLinked.list();  //Traversing node
	}
}

Program running results:

  • Added nodes:
    Data [no=1] Data [no=3] Data [no=4] Data [no=22]
  • Modified nodes:
    Data [no=5] Data [no=3] Data [no=4] Data [no=22]
  • Deleted nodes:
    Data [no=5] Data [no=3] Data [no=4]






josephus problem

class Data{
	private int no;
	private Data next;  //Point to next node	
	public Data(int no) {
		this.no = no;
	}
	public int getNo() {
		return no;
	}
	public void setNo(int no) {
		this.no = no;
	}
	public Data getNext() {
		return next;
	}
	public void setNext(Data next) {
		this.next = next;
	}
	@Override
	public String toString() {
		return no + "->";
	}
}

/*
 * Function implementation class
 * 1,Create a circular list
 */
class LinkedList{
	private Data first = new Data(-1);
	/**
	 * Create a circular list
	 * @param no  Number of nodes
	 */
	public void add(int no) {
		if(no < 1) {  //Less than 1 node
			System.out.println("The number is wrong.");
			return ;
		}
		Data temp = null;  //Set secondary node
		//Loop create loop list
		for(int i = 1; i <= no; i++) {
			Data node = new Data(i);
			if(i == 1) {  //First node added
				first = node;  
				first.setNext(first);
				temp = first;
			}else {
				temp.setNext(node);
				node.setNext(first);
				temp = node;
			}
		}
	}
	
	/**
	 * Joseph problem: output nodes one by one
	 * @param startNo  Start with node number
	 * @param countNO  Number of interval nodes
	 * @param numsNo  Total nodes
	 */
	public void countData(int startNo, int countNo, int numsNo) {
		if(startNo < 1 || startNo > numsNo || countNo < 1) {
			System.out.println("Incorrect parameters");
			return ;
		}
		add(numsNo);
		Data temp = first;  //Set auxiliary pointer
		//Move the auxiliary pointer to the last node
		while(temp.getNext() != first) {
			temp = temp.getNext();
		}
		//Move first to the next location on the start node
		for(int i = 0; i < startNo; i++) {
			first = first.getNext();
			temp = temp.getNext();
		}
		//Cyclic output node
		while(true) {
			if(temp == first) {  //There is only one node left
				break;
			}
			//Move the first and temp nodes to countNo, and the node where the first node is located is the node to go out
			for(int i = 0; i < countNo - 1; i++) {
				first = first.getNext();
				temp = temp.getNext();
			}
			System.out.print(first);  //Output node
			//Deleted nodes
			first = first.getNext();
			temp.setNext(first);
		}
		System.out.print(first);
	}	
	
	/**
	 * Ergodic circular list
	 */
	public void list() {
		if(first == null) {  //Linked list is empty.
			System.out.println("Linked list is empty.");
			return ;
		}
		Data temp = first;  //Set secondary node
		//Circular output chain list information
		while(true) {
			System.out.print(temp.toString());
			if(temp.getNext() == first) {
				return ;
			}else {
				temp = temp.getNext();
			}
		}
	}
}

/*
 * main function
 */
public class CaoGao {
	public static void main(String[] args) {
		LinkedList linkedList = new LinkedList();
		linkedList.countData(2, 2, 10);
	}
}

Operation result:

  • 4->6->8->10->2->5->9->3->1->7->






Use of stack

Simple addition, subtraction, multiplication and division

/*
 * Class of operations
 */
class NumberStack{
	Stack<Integer> num = new Stack<Integer>();  //Define the stack of data
	Stack<Character> s = new Stack<Character>();  //Define the stack of operators
	/**
	 * Operation process and result
	 * @param str  String of operation
	 */
	public void fzk(String str) {
		//One by one value
		for(int i = 0; i < str.length(); i++) {
			if(isOper(str.charAt(i))) {  //Character is operator
				if(s.isEmpty()) {  //Empty stack for operator
					s.push(str.charAt(i));  //Push
				}else {  //Operator stack is not empty
					if(priority(str.charAt(i)) > priority(s.peek())) {  //The priority of this operator is higher than that of the operator stack
						s.push(str.charAt(i));  //Push
					}else {
						//Take the top symbol of the operator stack to operate with two numbers of the data stack
						//And stack the value of the operation
						//Stack the operator
						int x = num.pop();
						int y = num.pop();
						int n = count(y, x, s.pop());
						num.push(n);
						s.push(str.charAt(i));
					}
				}
			}else {  //Data entry
				num.push(Integer.parseInt(str.substring(i, i + 1)));
			}
		}
		//Operator stack is not empty
		//Take the top symbol of the operator stack to operate with two numbers of the data stack
		//And stack the value of the operation
		while(!s.isEmpty()) {
			int x = num.pop();
			int y = num.pop();
			int n = count(y, x, s.pop());
			num.push(n);
		}
		System.out.println(num.pop());
	}
	
	/**
	 * Determine whether it is an operator
	 * @param ch  Characters to judge
	 * @return  
	 */
	public static boolean isOper(char ch) {
		if(ch == '+' || ch == '-' || ch == '*' || ch == '/') {
			return true;
		}
		return false;
	}
	
	/**
	 * Simulation priority
	 * @param ch  Characters that need to be prioritized
	 * @return  priority
	 */
	public static int priority(char ch) {
		int p = 0;
		if(ch == '*' || ch == '/') {
			p = 1;
		}else {
			p = 0;
		}
		return p;
	}
	
	/**
	 * operation
	 * @param num1  Numerical value 1
	 * @param num2  Numerical value 2
	 * @param oper  operator
	 * @return  Operation result
	 */
	public static int count(int num1, int num2, char oper) {
		int n = 0;
		switch (oper) {
		case '+':
			n = num1 + num2;
			break;
		case '-':
			n = num1 - num2;
			break;
		case '*':
			n = num1 * num2;
			break;
		case '/':
			n = num1 / num2;
			break;
		default:
			break;
		}
		return n;
	}
}

/*
 * main function
 */
public class CaoGao {
	
	public static void main(String[] args) {
		NumberStack numberStack = new NumberStack();
		numberStack.fzk("4+4*4+4*4");
	}
}

Operation result:

  • 36







Published 7 original articles, praised 0, visited 220
Private letter follow

Keywords: less Java

Added by thebusinesslad on Mon, 27 Jan 2020 18:43:25 +0200