Java data structure (Linked List)

Source: Internet
Author: User

A linked list is a non-sequential storage structure on a physical storage unit. The logical sequence of data elements is achieved through the pointer links in the linked list. A linked list consists of a series of nodes (each element in a linked list is called a node), which can be dynamically generated at runtime. Each node consists of two parts: one is the data domain that stores data elements, and the other is the pointer domain that stores the next node address. Compared with the ordered structure of a linear table, the linked list is easier to insert and delete.

 


Connection point:


[Java]
Package ch04Link;
 
Public class Link {
 
// Data domain
Private long data;

// Pointer Field
Private Link next;

// Constructor
Public Link (long data ){
This. data = data;
}
 
Public long getData (){
Return data;
}
 
Public void setData (long data ){
This. data = data;
}
 
Public Link getNext (){
Return next;
}
 
Public void setNext (Link next ){
This. next = next;
}



}

Package ch04Link;

Public class Link {

// Data domain
Private long data;
 
// Pointer Field
Private Link next;
 
// Constructor
Public Link (long data ){
This. data = data;
}

Public long getData (){
Return data;
}

Public void setData (long data ){
This. data = data;
}

Public Link getNext (){
Return next;
}

Public void setNext (Link next ){
This. next = next;
}
 
 
 
}

Test connection point:


[Java]
Package ch04Link;
 
Public class TestLink {
 
Public static void main (String [] args ){
Link link1 = new Link (10 );
Link link2 = new Link (50 );
Link link3 = new Link (20 );
Link link4 = new Link (100 );

Link1.setNext (link2 );
Link2.setNext (link3 );
Link3.setNext (link4 );

System. out. println (link1.getData ());
System. out. println (link1.getNext (). getData ());
System. out. println (link1.getNext (). getNext (). getData ());
System. out. println (link1.getNext (). getNext (). getNext (). getData ());
}
}

Package ch04Link;

Public class TestLink {

Public static void main (String [] args ){
Link link1 = new Link (10 );
Link link2 = new Link (50 );
Link link3 = new Link (20 );
Link link4 = new Link (100 );

Link1.setNext (link2 );
Link2.setNext (link3 );
Link3.setNext (link4 );

System. out. println (link1.getData ());
System. out. println (link1.getNext (). getData ());
System. out. println (link1.getNext (). getNext (). getData ());
System. out. println (link1.getNext (). getNext (). getNext (). getData ());
}
}

Linked List:


[Java]
Package ch04Link;
 
/**
* Linked list: the core idea is that a linked list contains only one data item, that is, reference to the first connection point.
* @ Author hadoop
*
*/
Public class LinkList {
 
Private Link first;

// Insert
Public void insert (long value ){
Link link = new Link (value );
If (first = null)
First = link;
Else {
Link. setNext (first );
First = link;
}
}

// Print the linked list
Public void display (){
Link current = first;
While (current! = Null ){
System. out. print (current. getData () + "");
Current = current. getNext ();
}
}

// Query the linked list
Public Link find (long key ){
Link current = first;
While (current. getData ()! = Key ){
If (current. getNext () = null)
Return null;
Current = current. getNext ();
}
Return current;
}

// Insert a node to a specified position
Public void insert (long value, int pos ){
If (pos = 0)
Insert (value );
Else {
Link current = first;
For (int I = 0; I <pos-1; I ++)
Current = current. getNext ();
Link link = new Link (value );
Link. setNext (current. getNext ());
Current. setNext (link );
}
}

// Delete a node
Public void delete (long value ){
Link current = first;
Link ago = first;
While (current. getData ()! = Value ){
If (current. getNext () = null)
Return;
Else {
Ago = current;
Current = current. getNext ();
}
}
If (current = first)
First = first. getNext ();
Else
Ago. setNext (current. getNext ());
}
}

Package ch04Link;

/**
* Linked list: the core idea is that a linked list contains only one data item, that is, reference to the first connection point.
* @ Author hadoop
*
*/
Public class LinkList {

Private Link first;
 
// Insert
Public void insert (long value ){
Link link = new Link (value );
If (first = null)
First = link;
Else {
Link. setNext (first );
First = link;
}
}
 
// Print the linked list
Public void display (){
Link current = first;
While (current! = Null ){
System. out. print (current. getData () + "");
Current = current. getNext ();
}
}
 
// Query the linked list
Public Link find (long key ){
Link current = first;
While (current. getData ()! = Key ){
If (current. getNext () = null)
Return null;
Current = current. getNext ();
}
Return current;
}
 
// Insert a node to a specified position
Public void insert (long value, int pos ){
If (pos = 0)
Insert (value );
Else {
Link current = first;
For (int I = 0; I <pos-1; I ++)
Current = current. getNext ();
Link link = new Link (value );
Link. setNext (current. getNext ());
Current. setNext (link );
}
}
 
// Delete a node
Public void delete (long value ){
Link current = first;
Link ago = first;
While (current. getData ()! = Value ){
If (current. getNext () = null)
Return;
Else {
Ago = current;
Current = current. getNext ();
}
}
If (current = first)
First = first. getNext ();
Else
Ago. setNext (current. getNext ());
}
}

Test:


[Java]
Package ch04Link;
 
Public class TestLinkList {
 
Public static void main (String [] args ){
LinkList linkList = new LinkList ();

LinkList. insert (20 );
LinkList. insert (80 );
LinkList. insert (40 );
LinkList. insert (60 );
LinkList. display ();
System. out. println ();
 
System. out. println ("find node, data:" + linkList. find (80). getData ());

LinkList. insert (100, 0 );
LinkList. display ();

System. out. println ();

LinkList. delete (80 );
LinkList. display ();
}
}

Package ch04Link;

Public class TestLinkList {

Public static void main (String [] args ){
LinkList linkList = new LinkList ();

LinkList. insert (20 );
LinkList. insert (80 );
LinkList. insert (40 );
LinkList. insert (60 );
LinkList. display ();
System. out. println ();

System. out. println ("find node, data:" + linkList. find (80). getData ());

LinkList. insert (100, 0 );
LinkList. display ();

System. out. println ();

LinkList. delete (80 );
LinkList. display ();
}
}

Compare linked list and sequence table:


[Java]
Package ch04Link;
 
Import ch01Array. MyArray;
 
Public class Test {
 
Public static void main (String [] args ){
// Create a linked list
Long date1 = System. currentTimeMillis ();
LinkList linkList = new LinkList ();
For (int I = 0; I <1000000; I ++)
LinkList. insert (I );
Long date2 = System. currentTimeMillis ();
System. out. println (date2-date1 );

// Construct an ordered table
Long date3 = System. currentTimeMillis ();
MyArray myArray = new MyArray (1000000 + 1 );
For (int I = 0; I <1000000; I ++)
MyArray. insert (I );
Long date4 = System. currentTimeMillis ();
System. out. println (date4-date3 );

// Delete a linked list
Long date5 = System. currentTimeMillis ();
LinkList. delete (60000 );
Long date6 = System. currentTimeMillis ();
System. out. println (date6-date5 );

// Delete an ordered table
Long date7 = System. currentTimeMillis ();
MyArray. delete (60000 );
Long date8 = System. currentTimeMillis ();
System. out. println (date8-date7 );
}
}

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.