创建空的成员变量并用构造函数初始化
class Node {
public int data;
public Node next;
public Node(int data) {
this.data = data;
}
}
遍历链表
public class MyLinkLest {
public Node head;//标识头节点
//遍历链表
public void display() {
Node cur = this.head;
while (cur != null) {
System.out.print(cur.data + " ");
cur = cur.next;
}
System.out.println();
}
头插法
//头插法
public void addFirst(int data) {
Node node = new Node(data);
node.next = this.head;
this.head = node;
}
尾插法
//尾插法
public void addLast(int data) {
//0、判断当前列表是否是第一次插入
//1、找尾巴,cur.next == null
Node node = new Node(data);
if (this.head == null) {
this.head = node;
} else {
Node cur = this.head;
while (cur.next != null) {
cur = cur.next;
}
cur.next = node;
}
}
在任意位置插入,第一个数据节点为0号下表
public boolean checkIndex(int index) {
if (index < 0 || index > getLength()) {
System.out.println("下标不合法");
return false;
}
return true;
}
//在任意位置插入,第一个数据节点为0号下表
public void addIndex(int index, int data) {
if(!checkIndex(index)) {
return;
}
if (index == 0) {
addFirst(data);
return;
}
if (index == this.getLength()) {
addLast(data);
return;
}
Node cur = searchPrev(index);
//cur此时保存的就是index-1位置的节点的引用
Node node = new Node(data);
node.next = cur.next;
cur.next = node;
}
public Node searchPrev(int index) {
Node cur = this.head;
int count = 0;
while (count < index -1 ) {
cur = cur.next;
count++;
}
return cur;
}
public int getLength() {
int count = 0;
Node cur = this.head;
while(cur != null) {
count ++;
cur = cur.next;
}
return count;
}
删除第一次出现关键字为key节点
public Node searchPrevNode (int key) {
Node cur = this.head;
if(cur.next != null) {
if(cur.next.data == key){
return cur;
}
cur = cur.next;
}
return null;
}
//删除第一次出现关键字为key节点
public void remove(int key) {
if(this.head == null)
return;
//头节点是删除的节点
if(this.head.data == key) {
this.head = this.head.next;
return;
}
Node cur =searchPrevNode(key);
if(cur == null) {
System.out.println("没有你要删除的数字");
return;
}
Node del = cur.next;//删除的节点
cur.next = del.next;
}
删除所有值为key的节点
public void removeAllkey(int key) {
Node cur = new Node(key);
cur.next = head;
while(cur.next != null) {
if (cur.next.data == key) {
cur.next = cur.next.next;
} else {
cur = cur.next;
}
}
}
查找是否包含关键字key
public boolean contains(int key) {
Node cur = this.head;
while( cur != null) {
if (cur.data == key) {
return true;
}
cur = cur.next;
}
return false;
}
}
测试代码
public class Test {
public static void main(String[] args) {
MyLinkLest myLinkLest = new MyLinkLest();
myLinkLest.addFirst(1);
myLinkLest.addFirst(2);
myLinkLest.addFirst(3);
myLinkLest.addFirst(2);
myLinkLest.display();
myLinkLest.addLast(1);
myLinkLest.addLast(2);
myLinkLest.addLast(3);
myLinkLest.display();
myLinkLest.addIndex(0,1);
myLinkLest.addIndex(1,2);
myLinkLest.addIndex(0,3);
myLinkLest.display();
myLinkLest.remove(1);
myLinkLest.display();
System.out.println(myLinkLest.contains(3));
myLinkLest.removeAllkey(2);
myLinkLest.display();
}
}
完整代码
/**
* @Author 袁媛
* @Date 2020/7/28
* @Time 10:04
*/
class Node {
public int data;
public Node next;
public Node(int data) {
this.data = data;
}
}
public class MyLinkLest {
public Node head;//标识头节点
//遍历链表
public void display() {
Node cur = this.head;
while (cur != null) {
System.out.print(cur.data + " ");
cur = cur.next;
}
System.out.println();
}
//头插法
public void addFirst(int data) {
Node node = new Node(data);
node.next = this.head;
this.head = node;
}
//尾插法
public void addLast(int data) {
//0、判断当前列表是否是第一次插入
//1、找尾巴,cur.next == null
Node node = new Node(data);
if (this.head == null) {
this.head = node;
} else {
Node cur = this.head;
while (cur.next != null) {
cur = cur.next;
}
cur.next = node;
}
}
public boolean checkIndex(int index) {
if (index < 0 || index > getLength()) {
System.out.println("下标不合法");
return false;
}
return true;
}
//在任意位置插入,第一个数据节点为0号下表
public void addIndex(int index, int data) {
if(!checkIndex(index)) {
return;
}
if (index == 0) {
addFirst(data);
return;
}
if (index == this.getLength()) {
addLast(data);
return;
}
Node cur = searchPrev(index);
//cur此时保存的就是index-1位置的节点的引用
Node node = new Node(data);
node.next = cur.next;
cur.next = node;
}
public Node searchPrev(int index) {
Node cur = this.head;
int count = 0;
while (count < index -1 ) {
cur = cur.next;
count++;
}
return cur;
}
public int getLength() {
int count = 0;
Node cur = this.head;
while(cur != null) {
count ++;
cur = cur.next;
}
return count;
}
public Node searchPrevNode (int key) {
Node cur = this.head;
if(cur.next != null) {
if(cur.next.data == key){
return cur;
}
cur = cur.next;
}
return null;
}
//删除第一次出现关键字为key节点
public void remove(int key) {
if(this.head == null)
return;
//头节点是删除的节点
if(this.head.data == key) {
this.head = this.head.next;
return;
}
Node cur =searchPrevNode(key);
if(cur == null) {
System.out.println("没有你要删除的数字");
return;
}
Node del = cur.next;//删除的节点
cur.next = del.next;
}
//删除所有值为key的节点
public void removeAllkey(int key) {
Node cur = new Node(key);
cur.next = head;
while(cur.next != null) {
if (cur.next.data == key) {
cur.next = cur.next.next;
} else {
cur = cur.next;
}
}
}
//查找是否包含关键字key
public boolean contains(int key) {
Node cur = this.head;
while( cur != null) {
if (cur.data == key) {
return true;
}
cur = cur.next;
}
return false;
}
}
public class Test {
public static void main(String[] args) {
MyLinkLest myLinkLest = new MyLinkLest();
myLinkLest.addFirst(1);
myLinkLest.addFirst(2);
myLinkLest.addFirst(3);
myLinkLest.addFirst(2);
myLinkLest.display();
myLinkLest.addLast(1);
myLinkLest.addLast(2);
myLinkLest.addLast(3);
myLinkLest.display();
myLinkLest.addIndex(0,1);
myLinkLest.addIndex(1,2);
myLinkLest.addIndex(0,3);
myLinkLest.display();
myLinkLest.remove(1);
myLinkLest.display();
System.out.println(myLinkLest.contains(3));
myLinkLest.removeAllkey(2);
myLinkLest.display();
}
}