PS: A linked list is a data structure, and a data structure is a way to store data.
Why do you need a linked list?
We know that arrays can also store data, so why do we need linked lists? Next, let's take a look at the difference between an array and a linked list:
1. An array is like a person who is numbered and standing in a row. It is easy to find the 10th person, and it can be found quickly according to the number on the person. However, insertion and deletion are slow. When inserting or deleting a person at a certain position, the number of the person behind must be changed. Of course, people who are added or removed always end faster.
2. A linked list is like people standing in a circle holding hands. It is not easy to find the 10th person. You must count them from the first person. But insertion and deletion are fast. When inserting, just untie the hands of the two people and re-took the hand of the newly added person. Delete the same reasoning.
Schematic diagram of linked list
- The establishment of the linked list
class TestLink{//创建一个外部类
private Entry head;//指向头结点的引用
public TestLink(){
head = new Entry();//用结点类 new 一个头结点
}
class Entry{//Entry 创建一个结点内部类
int data;//定义数据块
Entry next;//定义地址块
public Entry(){//构造方法1
data = -1;//对结点数据块初始化
next = null;//对地址初始化
}
public Entry(int val){//构造方法2
data = val;//对数据块赋值
next = null;
}
}
}
public class TestDemo2 {
public static void main(String[] args) {
TestLink testlink = new TestLink();
//创建一个 链表外部类 对象
}
}
- Head insertion method: Insert from scratch
public void insertHead(int val){
//有这么一个结点
Entry cur = new Entry(val);
cur.next = head.next;
head.next = cur;
}
Schematic diagram of head insertion method:
- Tail insertion method: insertion from the tail
public void insertTail(int val){
//找到尾巴
Entry cur = head;
while(cur.next != null){//遍历结点
cur = cur.next;
}
Entry entry = new Entry(val);//得到的结点
cur.next = entry;
}
Schematic diagram of tail insertion method:
- Insert from any node
public boolean insertPos(int val,int pos){
//1、判断pos的合法性
if(pos < 0 || pos >= getLength()+1){
return false;
}
Entry cur = head;
for(int i = 0;i <= pos-1;i++){
cur = cur.next;
}
//cur pos的前一个
Entry entry = new Entry(val);
entry.next = cur.next;
cur.next = entry;
return true;
}
Schematic:
Full code:
package LianBiao;
class TestLink1{
private Entry head;//指向头结点的引用
public TestLink1(){
head = new Entry();
}
class Entry{//Entry Node
int data;
Entry next;
public Entry(){
data = -1;
next = null;
}
public Entry(int val){
data = val;
next = null;
}
}
public void insertHead(int val){
//有这么一个结点
Entry cur = new Entry(val);
cur.next = head.next;
head.next = cur;
/*head.next = cur;
cur.next = head.next;*/
}
public void insertTail(int val){
//找到尾巴
Entry cur = head;
while(cur.next != null){
cur = cur.next;
}
Entry entry = new Entry(val);//得到的结点
cur.next = entry;
}
//得到单链表的长度:
public int getLength(){
int len = 0;
Entry cur = head.next;
while(cur != null){
len++;
cur = cur.next;
}
return len;
}
//将数据插入到指定位置
public boolean insertPos(int val,int pos){
//1、判断pos的合法性
if(pos < 0 || pos >= getLength()+1){
return false;
}
Entry cur = head;
for(int i = 0;i <= pos-1;i++){
cur = cur.next;
}
//cur pos的前一个
Entry entry = new Entry(val);
entry.next = cur.next;
cur.next = entry;
return true;
}
//
//show()
public void show(){
/*Entry cur = head;
while(cur.next != null){
System.out.println("data:"+cur.next.data);
cur = cur.next;
}*/
Entry cur = head.next;
while(cur != null){
System.out.println("data:"+cur.data);
cur = cur.next;
}
}
}
public class LianBiao1 {
public static void main(String[] args) {
// TODO Auto-generated method stub
TestLink1 testlink = new TestLink1();
testlink.insertTail(1330);
testlink.insertTail(110);
//1330 110
testlink.insertPos(10,0);
//10 1330 110
if(testlink.insertPos(32,10000)){
System.out.println("插入成功");
}else{
System.out.println("插入失败");
}
//10 32 1330 110
testlink.show();
System.out.println(testlink.getLength());
}
}
Output result: