Inner class ,List of unused inner classes You can click on the name.
This article mainly introduces how to write Java linked list using inner class.
The chain list is just like its name, just like a bead is linked, only the former bead and the latter bead are related to the current bead, which is a one-to-one data structure.
This kind of data structure is particularly convenient when inserting and deleting nodes, because it is not like an array to move a large number of elements, and only needs to change some references to achieve addition and deletion.
Increase of linked list
1) head insertion
As the name implies, the head insertion method and each insertion is inserted to the first node
Only two steps are needed for the head insertion method of linked list (New nodes have been generated)
1. Address element 1 to the inserted element
2. Give the address of the inserted element to the header node
2) Tail insertion
Only one step is needed for the tail insertion method of linked list (New nodes have been generated)
Give the address of the inserted element to the last element.
3) Insert to specified location
The principle of inserting to the specified position is basically the same as that of the head inserting method, except that when the element before the inserting position is regarded as the head node, the operation of inserting is basically the same, only the corresponding position needs to be found in advance.
Delete Linked List
To be continued
Traversal of linked list
The traversal of the linked list only needs to move the reference all the time, making the temporary reference variable move backward until the temporary reference variable is null, that is, all elements have been traversed.
Length of linked list
The principle is the same as traversal, except that the basic operation is changed to count and the result is return ed at last.
Code
package com.chenxixuexi;
/**
* Linked list
* @author Jiao Yan
*
*/
public class Link1 {
private Entry head = new Entry(); //Reference to head node
/**
* Node class
* @author Jiao Yan
*
*/
class Entry{//Entry Node
int data;
Entry next;
/**
* Head node space construction method
*/
public Entry() {
data = 0;
next = null;
}
/**
* Structural node
* @param a
*/
public Entry(int a) {
data = a;
next = null;
}
}
/**
* Head insertion
* @param val Data to insert
*/
public void insertHead(int val)
{
//There is such a node
Entry cur = new Entry(val);
cur.next = head.next;
head.next = cur;
}
/**
* Tail insertion method
* @param val Value to insert
*/
public void insertTail(int val)
{
Entry cur = head;
while(cur.next!= null) //Loop until cur.next is empty to eject cur
{
cur = cur.next;
}
cur.next = new Entry(val);//Assign cur.next the address of the new node
}
/**
* Insert to pos position with 0 subscript starting from data
* @param pos Location serial number
* @param val Inserted value
*/
public void insertPos(int pos,int val)
{
Entry cur = head; //Get header node
int count = 0;
if(pos<0||pos>getLength()) //If you need to interrupt the operation, just return here
System.out.println("subscript"+pos+"Wrongful,Auto insert to last position");
while(cur.next!= null) //Loop until cur.next is empty, pop up cur and insert the element to the back if it is not found
{
if(count == pos)
break;
count++;
cur = cur.next;
}
Entry cur1 = new Entry(val);
cur1.next = cur.next;
cur.next = cur1;
}
/**
* Traversal output array
*/
public void show()
{
Entry cur = head.next; //Get to first node
System.out.print("[");
while(cur!=null) //If cur is not null
{
System.out.print(cur.data+" "); //output
cur = cur.next; //Progressive downward
}
System.out.println("]");
}
/**
* Get chain length
* @return Length of linked list
*/
public int getLength()
{ int count=0;
Entry cur = head.next; //Get to first node
while(cur!=null) //If cur is not null
{
count++; //count
cur = cur.next; //Progressive downward
}
return count;
}
}