单链表
head就是个引用,它保存的是234
//头插法
public void addFirst(int data) {
Node node = new Node(data);//有一个node节点了
class Node {
public int data;//0
public Node next;//null
public Node(int data) {
this.data =data;//传入的数据 这样一来节点就有值了
this.next = null;//可写可不写,引用类型默认为null
}
}
class Node {
public int data;//0
public Node next;//null
public Node(int data) {
this.data =data;
this.next = null;
}
}
public class MyLinkedList {
public Node head;//保存单链表的头节点的引用 null 用head标识头
//头插法
public void addFirst(int data) {
Node node = new Node(data);//有一个node节点了
if (this.head == null) {
//第一次插入节点
this.head = node;
return;
}
node.next = this.head;//node的next指向head
head = node;//head 指向头节点
}
//head表示头节点的地址
public void display() {
Node cur = this.head;//定义一个变量cur,它保存head的值(cur指向head的节点)
while (cur != null) {
System.out.print(cur.data + " ");
cur = cur.next;//新的cur表示cur.next 把cur.next赋给cur 新的cur就指向下一个节点
}
打印头插法
class Node {
public int data;//0
public Node next;//null
public Node(int data) {
this.data =data;
this.next = null;
}
}
public class MyLinkedList {
public Node head;//保存单链表的头节点的引用 null
//头插法
public void addFirst(int data) {
Node node = new Node(data);
if (this.head == null) {
//第一次插入节点
this.head = node;
return;
}
node.next = this.head;
head = node;
}
//打印单链表
public void display() {
Node cur = this.head;
while (cur != null) {
System.out.print(cur.data + " ");
cur = cur.next;
}
}
public class TestDemo {
public static void main(String[] args) {
MyLinkedList myLinkedList = new MyLinkedList();
myLinkedList.addFirst(10);
myLinkedList.addFirst(11);
myLinkedList.addFirst(12);
myLinkedList.addFirst(13);
myLinkedList.display();
}
}
打印头插法代码截图
尾插法
//尾插法
public void addLast(int data) {
Node node = new Node(data);//首先有一个节点
if (this.head == null){
//第一次插入时
this.head = node;
return;
}
Node cur = this.head;//定义一个cur从头开始走
while (cur.next != null) {
//cur.next = null时,cur指向最后一个节点
cur = cur.next;//
}//退出循环cur.next为空了,即就是尾节点
cur.next = node;
}
public class TestDemo {
public static void main(String[] args) {
MyLinkedList myLinkedList = new MyLinkedList();
myLinkedList.addLast(77);
myLinkedList.addLast(78);
myLinkedList.addLast(79);
myLinkedList.addLast(80);
/* myLinkedList.addFirst(10);
myLinkedList.addFirst(11);
myLinkedList.addFirst(12);
myLinkedList.addFirst(13);*/
myLinkedList.display();
}
}
代码截图
查找是否包含关键字key是否在单链表当中
//查找是否包含关键字key是否在单链表当中
public boolean contains(int key) {
Node cur = this.head;//cur从头开始找
while (cur != null) {
if (cur.data == key) {
return true;
}
cur = cur.next;
}
return false;
}
得到单链表的长度
//得到单链表的长度
public int size() {
int count = 0;
Node cur = this.head;
while (cur != null) {
count++;
cur = cur.next;
}
return count;
}
测试contines和size
public class TestDemo {
public static void main(String[] args) {
MyLinkedList myLinkedList = new MyLinkedList();
myLinkedList.addLast(77);
myLinkedList.addLast(78);
myLinkedList.addLast(79);
myLinkedList.addLast(80);
/* myLinkedList.addFirst(10);
myLinkedList.addFirst(11);
myLinkedList.addFirst(12);
myLinkedList.addFirst(13);*/
myLinkedList.display();
boolean flg = myLinkedList.contains(78);
System.out.println(flg);
System.out.println(myLinkedList.size());
}
}
任意位置插入,第一个数据结点为0号下标
//任意位置插入,第一个数据结点为0号下标
public void addIndex(int index, int data) {
if(index == 0) {
//头插法
addFirst(data);
return;
}
if (index == this.size()) {
//尾插法
addLast(data);
return;
}
Node node = new Node(data);
//先找到 index位置的前一个结点的地址
Node cur = searchIndex(index);//调用它之后 就拿到了一个index-1步这样的地址
//进行插入
node.next =cur.next;
cur.next = node;
}
private Node searchIndex(int index) {
//1.对index进行合法性检查
if (index < 0 || index > this.size()) {
throw new RuntimeException("index位置不合法");
}
Node cur = this.head;//让cur从头开始走 写个循环让cur 走index-1步 返回cur
while (index - 1 != 0) {
cur = cur.next;
index--;
}
return cur;
}
public class TestDemo {
public static void main(String[] args) {
MyLinkedList myLinkedList = new MyLinkedList();
myLinkedList.addLast(77);
myLinkedList.addLast(78);
myLinkedList.addLast(79);
myLinkedList.addLast(80);
/* myLinkedList.addFirst(10