List (LinkedList) Java language implementation __java

Source: Internet
Author: User

First of all, the Java API has provided a one-way list of classes, you can use directly. The purpose of this realization is to better understand the data structure of the linked list. This is mainly to introduce some common structure inside the use of methods, as well as the details of how the linked list is operated.

The People.java class is a custom data type

public class People {
	private String name;//name is
	private int age;//old
	private int id;//Unique identification ID public
	
	PEOP Le (String name, int age, int id) {
		this.name = name;
		This.age = age;
		This.id = ID;
	}
	
	Public String GetName () {return
		this.name;
	}
	
	public int getage () {return
		this.age;
	}
	
	public int getId () {return
		this.id;
	}
	
	public void Printmessage () {
		System.out.println ("Name:" + THIS.name + "Age:" + this.age + "ID:" + this.id);
	}
}

The Node.java class is the encapsulated node class

public class Node {public
	people people;//Data Domain public
	node next = null;//pointer field points to the next node's memory address public node
	
	(people People) {
		this.people = people;
	}
	
	public void Printmessage () {
		this.people.printMessage ();
	}
}

Linkedlist.java class is what we want to operate the list class, which implements some of the basic operating methods of the linked list.

public class LinkedList {private node head;//Header Node public linkedlist () {this.head = null;
		//Insert a header node public void Insertheadnode (people people) {node node = new node (people);
		Node.next = head;
	Head = node;
			//Delete Header node public people Deleteheadnode () {if (head = = null) {System.out.println ("empty list");
		return null;
		} people people = Head.people;
		head = Head.next;
	return people; ///Insert a node in any location public void Insertnode (People people, int index) {if (head = = null) {System.out.println ("empty list")
			;
		Return
			} if (index = = 0) {//Insert Head node Insertheadnode (people);
		Return
		Node node = new node (people);
		Node current = head;
		Int J;
			for (j = 0; J < (index-1) && current!= null; j +) {current = Current.next;
				if (current = = null) {System.out.println ("The inserted node does not exist");
			Return
		} System.out.println ("Insert ID" +people.getid () + "People:");
		Node.next = Current.next;
	Current.next = node; }//delete any location node publIC people deletenode (int index) {if (head = = null) {System.out.println ("empty list");
		return null;
		} if (index = = 0) {//Delete head node return deleteheadnode ();
		Int J;
		Node current = head;
		Node remove;
			for (j = 0; J < (index-1) && current!= null; j +) {current = Current.next;
				if (current = = null) {System.out.println ("Deleted node does not exist");
			return null;
		} remove = Current.next;
		People people = remove.people;
		System.out.println ("Delete the People with ID" +people.getid () + ");
		Current.next = Remove.next;
	return people;
			Find people Public people Findnodebyid (int id) {if (head = = null) {System.out.println ("empty list") by ID;
		return null;
		} System.out.println ("Find people with ID" +id + ");
		Node current = head;
				While (the current!= null) {if (current.people.getId () = = ID) {current.people.printMessage ();
			return current.people;
		current = Current.next;
		} System.out.println ("No people with ID + ID +");
	return null; }
	
	//Delete People public people Deletenodebyid (int id) {if (head = = null) {System.out.println ("empty list") by ID;
		return null;
		} System.out.println ("Delete the People with ID" +id + ");
			if (head.people.getId () = = ID) {//If deletion is the head node Head.people.printMessage ();
			People people;
			people = Head.people;
			head = Head.next;
		return people;
				else {node beforecurrent = head;//the previous node of node current = Head.next;//The current node while (present!= null) {
					if (current.people.getId () = = ID) {current.people.printMessage ();
					Beforecurrent.next = Current.next;
				return current.people;
				} beforecurrent = current;
			current = Current.next;
		} System.out.println ("No people with ID + ID +");
	return null;
		//Print the entire list of public void Printlinkedlist () {System.out.println ("value of each node in the printed list:");
		if (head = = null) {System.out.println ("empty list");
		Node current = head;
			while (current!= null) {current.printmessage ();
		current = Current.next; }
	}
}

Validation results:

public class HelloWorld {public static void main (string[] args) {//Create a linked list LinkedList LinkedList = new LinkedList ();
		Insert a header node Linkedlist.insertheadnode (New People ("Wanda", 18, 1));
		Linkedlist.insertheadnode (New People ("King II", 19, 2));
		Linkedlist.insertheadnode (New People ("Wang San", 28, 3));
		Linkedlist.insertheadnode (New People ("Wang Shi", 31, 4));
 		Linkedlist.insertheadnode (New People ("Harry", 42, 5));
 		Linkedlist.printlinkedlist ();
		
 		/* Name: Harry age:42 id:5 Name: Wang Shi age:31 id:4 name: Wang San age:28 id:3 name: Wang age:19 id:2 name: Wanda Age:18/Id:1
		Delete the id=3 node Linkedlist.deletenodebyid (3);
		Linkedlist.printlinkedlist (); /* Name: Harry age:42 id:5 Name: Wang Shi age:31 id:4 name: Wang age:19 id:2 name: Wanda age:18 \ *//Find id:1 Node link
		
		Edlist.findnodebyid (5);
		Inserts the node Linkedlist.insertnode at position 2 ("people", 12, 6), 2);
		Linkedlist.printlinkedlist (); /* Name: Harry age:42 id:5 Name: Wang Shi age:31 id:4 name: Wang Liu age:12 id:6 name: Wang age:19 id:2 name: Wanda AGe:18 id:1 *//delete at position 1 node Linkedlist.deletenode (1);
		Linkedlist.printlinkedlist (); /* Name: Harry age:42 id:5 Name: Wang Liu age:12 id:6 name: Wang age:19 id:2 name: Wanda Age:18 id:1/}}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.