1.概念:
链表是一种物理结构上非连续的存储结构,数据元素的逻辑顺序是通过链表中的引用链接次序实现的。
2.一些基本方法:
1.void.addFirst();头插法
2.void.addLast();尾插法
3.void.addIndex(int index, int data);任意位置插入,第一个数据节点为0号下标;
4.boolean.contains(int key);判断是否包含元素 key
5.void.remove(int key);删除第一次出现为key的节点
6.void.removeAll(int key);删除所有值为key的节点
3. 实现代码(以int类型数据为例):
class ListNode{
public int val;
public ListNode next;
public ListNode(int val) {
this.val = val;
}
}
public class MyLinkedList {
public ListNode head;
public MyLinkedList() {
}
//头插
public void addFirst(int data){
ListNode node = new ListNode(data);
if(this.head == null){
this.head = node;
}else {
node.next = this.head;
this.head = node;
}
}
//尾插
public void addLast(int data){
ListNode node = new ListNode(data);
if(this.head == null){
this.head = node;
}else{
ListNode cur = this.head;
while(cur.next != null){
cur = cur.next;
}
cur.next = node;
}
}
//找前驱
public ListNode searchPrev(int index){
ListNode cur = this.head;
int count = 0;
while(count != index-1){
cur = cur.next;
count++;
}
return cur;
}
//指定位置插入
public void addIndex(int index,int data){
if(index < 0 || index > size()){
System.out.println("插入位置无效!!");
return;
}
ListNode node = new ListNode(data);
if(this.head == null){
return;
}
if(index == 0){
addFirst(data);
}else if(index == size()-1){
addLast(data);
}else {
ListNode prev = searchPrev(index);
node.next = prev.next;
prev .next = node;
}
}
//判断是否包含元素key
public boolean contains(int key){
ListNode cur = this.head;
if(this.head == null){
return false;
}
while(cur != null){
if(cur.val == key){
return true;
}
cur = cur.next;
}
return false;
}
//找前驱
public ListNode searchPrev2(int key){
ListNode cur = this.head;
while(cur.next != null){
if(cur.next.val == key){
return cur;
}
cur = cur.next;
}
return null;
}
//删除第一次出现的key元素
public void remove(int key){
ListNode cur = this.head;
if(this.head.val == key){
this.head = this.head.next;
}else {
ListNode prev = searchPrev2(key);
while (cur != null) {
if (cur.val == key) {
prev.next = cur.next;
return;
}
cur = cur.next;
}
}
}
//删除所有的key元素
public void removeAllKey(int key){
if(this.head == null){
return;
}
if(this.head.val == key){
this.head = this.head.next;
}
ListNode cur = this.head;
while (cur != null) {
if (cur.val == key) {
ListNode prev = searchPrev2(key);
prev.next = cur.next;
}
cur = cur.next;
}
}
public int size(){
int count = 0;
ListNode cur = this.head;
while(cur != null){
count++;
cur = cur.next;
}
return count;
}
//打印单链表
public void display(){
ListNode cur = this.head;
while(cur != null) {
System.out.print(cur.val+" ");
cur = cur.next;
}
System.out.println();
}
//清空链表
public void clear(){
this.head = null;
}
public static void main(String[] args) {
MyLinkedList myLinkedList = new MyLinkedList();
myLinkedList.addFirst(33);
myLinkedList.addFirst(33);
myLinkedList.addFirst(22);
myLinkedList.addFirst(44);
myLinkedList.addFirst(33);
myLinkedList.display();
myLinkedList.addLast(88);
myLinkedList.display();
System.out.println(myLinkedList.contains(111));
myLinkedList.remove(33);
myLinkedList.display();
myLinkedList.removeAllKey(33);
myLinkedList.display();
myLinkedList.addIndex(7,99);
myLinkedList.display();
System.out.println(myLinkedList.size());
}
}