Realization of basic operation of Java single-link list _java

Source: Internet
Author: User

Recently asked about the list, is a friend and I discussed Java when said. To tell the truth, I learn programming in nearly a year, the things I learned are quite few. Language is learning Java and C #, a little html+css+javascript about the web. Because the comparison preference, the study WinForm is more earnest, the database operation also has the research oneself. But the list of this thing I really did not learn and study, coupled with the recent I see WPF, and the curriculum has been JSP, relatively tight.

But I still smoked one night plus half a day to look at the one-way linked list. and use Java to try to write an example. No contact with the list of friends can be used as a reference, I hope that we have more valuable advice.

Let's first explain what a linked list is. As I know, a linked list is a data structure, and an array of siblings. For example, the ArrayList we use in Java is an array of implementations. and the realization principle of linkedlist is linked list. My teacher said that the linked list in the cycle of the time is not high efficiency, but the insertion and deletion of the advantages of obvious. So he has more than ten years of programming experience, I believe. But do not know whether he is said two-way linked list, we are here to do a one-way linked list to do an understanding.

Linked list (chain this article, the linked list is one-way linked list, hereinafter referred to as one-way linked list) is actually composed of nodes (node), a linked list has an indefinite number of nodes. The outward exposure is only a head node, and all of our operations on the linked list are made directly or indirectly through its head node.

Nodes are composed of an object that needs to be stored and a reference to the next node. That is, a node has two members: a stored object, a reference to the next node.

This may not be very clear to everyone, I put a picture of people may be easier to understand.

Java single linked list basic operation of the implementation of the key code as follows:

Package com.tyxh.link; Node class public class Node {protected node next;//pointer field protected int data;//data domain public Node (int data) {this. data = d 
Ata 
//Show this node public void display () {System. Out.print (Data + ""); 
}} package Com.tyxh.link; Single linked table public class Linklist {public Node first;//define a head node private int pos = 0;//node location public linklist () {this. fi 
rst = null; 
//Insert a header node public void Addfirstnode (int data) {node node = new node (data); Node. 
next = i; 
i = node; 
//Deletes a header node and returns the header node public node Deletefirstnode () {node Tempnode = first; i = Tempnode. 
Next 
return tempnode; 
///Insert the node at any position after index to insert public void Add (int index, int data) {node node = new node (data); 
Node current = i; 
Node previous = i; 
while (POS!= index) {previous = current; Current = current. 
Next 
pos++; node. 
Next = current; Previous. 
Next = node; 
pos = 0; 
//delete nodes in any location public node Deletebypos (int index) {node current = i; Node PREvious = A; 
while (POS!= index) {pos++; 
previous = current; Current = current. 
Next 
} if (current = = i) {a = i. Next; 
else {pos = 0; Previous. Next = current. 
Next 
return to current; 
///Per node data Delete node (delete first only) public node deletebydata (int data) {node: Node previous = i; 
Remember the previous node while (current. Data!= data) {if (current. Next = null) {return null; 
} previous = current; Current = current. 
Next 
} if (current = = i) {a = i. Next; 
else {Previous. Next = current. Next; 
return to current; 
//Display all node information public void Displayallnodes () {node current = i; 
while (current!= null) {Current.display (); Current = current. 
Next } System. 
Out.println (); 
///Find node information by location public node Findbypos (int index) {node current = i; 
if (POS!= index) {current = current. Next; 
pos++; 
return to current; 
////Based on data lookup node information public node findbydata (int data) {node current = i; while (curRent. 
Data!= Data {if (current. Next = null) return null; Current = current. 
Next 
return to current; 
}} package Com.tyxh.link; 
Test class public class Testlinklist {public static void main (string[] args) {linklist linklist = new linklist (); 
Linklist.addfirstnode (20); 
Linklist.addfirstnode (21); 
Linklist.addfirstnode (19); 19,21,20 Linklist.add (1, 22); 19,22,21,20 Linklist.add (2, 23); 19,22,23,21,20 Linklist.add (3, 99); 
19,22,23,99,21,20 linklist.displayallnodes (); 
Node node = Linklist.deletefirstnode (); 
System.out.println ("node:" + Node.data); 
Linklist.displayallnodes (); 
node = Linklist.deletebypos (2); 
System.out.println ("node:" + Node.data); 
Linklist.displayallnodes (); 
Linklist.deletefirstnode (); 
Node node = linklist.deletebydata (19); 
Node node = linklist.deletebypos (0); System. Out.println ("Node:" + node.) 
data); 
Linklist.displayallnodes (); 
Node Node1 = linklist.findbypos (0); System. Out.println ("Node1:" + node1. 
data); 
Node Node2 = Linklist.findbydata (22); System. Out.println ("Node2:" + node2.) 
data); } 
}

The above is a small series to introduce the Java single linked list of the implementation of basic operations, I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!

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.