Node representation, insertion, deletion, reversal and concatenation of singly linked list of data structure (Java implementation)

A singly linked list is a commonly used type of list. All nodes are strung together in one column, and the pointers point to the same direction. In addition to storing the original data, each data in the list must also store the storage address of the next data. The following program example demonstrates how to build a linked list node, insert a node into a singly linked list, delete a node, reverse a singly linked list, and concatenate two singly linked lists:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Random;

class Node {
	int data;
	int np;
	String names;
	Node next;

	public Node(int data,String names,int np) {
		this.np=np;
		this.names=names;
		this.data=data;
		this.next=null;
	}
}

class LinkedList {
	public Node first;
	public Node last;
	public boolean isEmpty()
	{
		return first==null;
	}
	
	public void print()
	{
		Node current=first;
		while(current!=null)
		{
		System.out.println("["+current.data+" "+current.names+" "+current.np+"]");
		current=current.next;
		}
		System.out.println();
	}
	
	public void insert(int data,String names,int np) { //Insert a node into the linked list
		Node newNode=new Node(data,names,np);
		if(this.isEmpty())
		{
			first=newNode;
			last=newNode;
		}
		else
		{
			last.next=newNode;
			last=newNode;
		}
	}

	public void delete(Node delNode) { //Delete the node of the singly linked list
		Node newNode;
		Node tmp;
		if(first.data==delNode.data)
		{
			first=first.next;
		}
		else if(last.data==delNode.data) {
			System.out.println("I am here\n");
			newNode=first;
			while(newNode.next!=last) newNode=newNode.next;
			newNode.next=last.next;
			last=newNode;
	           }
		else {
			newNode=first;
			tmp=first;
			while(newNode.data!=delNode.data) {
				tmp=newNode;
				newNode=newNode.next;
			}
			tmp.next=delNode.next;
		}
	}
	public void reverse(){ //The reverse of the singly linked list
		Node current=first;
		Node before=null;
		Node nnext;
		System.out.println("Reversed list data:");
		while(current!=null){
			nnext=current.next;
			current.next=before;
			before=current;
			current=nnext;
		}	
		/*while(current!=null)
		{
			last=before;
			before=current;
			current=current.next;
			before.next=last;
		}*/
		current=before;
		while(current!=null) {
			System.out.println("["+current.data+" "+current.names+" "+current.np+"]");
			current=current.next;
		}
		System.out.println();
	}
	public static LinkedList connection(LinkedList head, LinkedList tail) {
		while(head.last.next !=null) //Maybe the last of the singly linked list does not point to the last node, so you need to find the last node of head
			head.last=head.last.next;
		head.last.next=tail.first;
		return head;
	}
}

public class ReverseLinkedListTest {
	public static void main(String args[]) throws IOException {
		BufferedReader buf;
		buf=new BufferedReader(new InputStreamReader(System.in));
		Random rand=new Random();
		LinkedList list =new LinkedList();
		LinkedList list2 =new LinkedList();
	    int i,j,findword=0,data[][]=new int[12][10];
	    String name[]=new String[] {"Allen","Scott","Marry","Jon","Mark","Ricky","Lisa","Jasica","Hanson","Amy","Bob","Jack"};
	    String name2[]=new String[] {"Blake","Paul","Peter","Poppy","Pony","Brenda","Bruce","Rey","Rehona","Remo","Wilson","Van"};
	    System.out.println("Student ID\t Score\t Student ID\t Score\t Student ID\t Score\t Student ID\t Score\n ");
	    System.out.println("Singly linked list 1:");
	    for (i=0;i<12;i++)
	    {
	    	data[i][0]=i+1;
			data[i][1]=(Math.abs(rand.nextInt(50)))+50;
			list.insert(data[i][0],name[i],data[i][1]);
		}
	    for (i=0;i<3;i++)
		{
			for(j=0;j<4;j++)
			System.out.print("["+data[j*3+i][0]+"]  ["+data[j*3+i][1]+"]  ");
			System.out.println();
		}
	    
	    System.out.println("Singly linked list 2:");
	    for (i=0;i<12;i++)
	    {
	    	data[i][0]=i+13;
			data[i][1]=(Math.abs(rand.nextInt(50)))+50;
			list2.insert(data[i][0],name[i],data[i][1]);
		}
	    for (i=0;i<3;i++)
		{
			for(j=0;j<4;j++)
			System.out.print("["+data[j*3+i][0]+"]  ["+data[j*3+i][1]+"]  ");
			System.out.println();
		}
	    
	    LinkedList.connection(list,list2).print(); //Concatenation of two singly linked lists
	    
	    while(true) //Test delete singly linked list node
	    {
	    	System.out.print("Enter the student number to delete the grade, and end input -1: ");
	    	findword=Integer.parseInt(buf.readLine());
	    	if(findword==-1)
	    		break;
	    	else
	    	{
	    		Node current=new Node(list.first.data,list.first.names,list.first.np);
	    		current.next=list.first.next;
	    		while(current.data!=findword) current=current.next;
	    		list.delete(current);
	    	}
	    	System.out.println("List of grades after deletion, please pay attention! The student number of the grade to be deleted must be in this list\n");
	    	list.print();
	    }
	    list.reverse(); //Test reverse singly linked list
	}
}

The connection method in the above program can also be modified as follows:

public LinkedList connection(LinkedList tail) {
		LinkedList head;
		head=this;
		while(head.last.next !=null) //Maybe the last of the singly linked list does not point to the last node, so you need to find the last node of head
			head.last=head.last.next;
		head.last.next=tail.first;
		return head;
	}
When called, it becomes list.connection(list2).print

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325906241&siteId=291194637