1.链表的概念和结构
链表是一种物理存储结构上非连续存储结构,数据元素的逻辑顺序是通过链表中的引用链接次序实现的 。
注:不会按线性的顺序存储数据,而是在每一个节点里存到下一个节点的指针(Pointer)。
2.1无头单向非循环链表实现
public class SeqList {
// 打印顺序表
public void display() { }
// 在 pos 位置新增元素
public void add(int pos, int data) { }
// 判定是否包含某个元素
public boolean contains(int toFind) { return true; }
// 查找某个元素对应的位置
public int search(int toFind) { return -1; }
// 获取 pos 位置的元素
public int getPos(int pos) { return -1; }
// 给 pos 位置的元素设为 value
public void setPos(int pos, int value) { }
//删除第一次出现的关键字key
public void remove(int toRemove) { }
// 获取顺序表长度
public int size() { return 0; }
// 清空顺序表
public void clear() { } }}
2.2底层实现
MySingleList中的代码
package linkedlist;
/*
无头单向非循环链表实现
*/
//节点都需要用node类型——>存储对象的引用
class Node{
public int data;//结点的值
public Node next;//指向下一个结点的next
//有参构造
public Node(int data){
this.data=data;
//没有初始化next:不知道当前指向那个节点;
}
}
public class MySingleList {
//定位头节点的引用
public Node head;
//头插法
public void addFirst(int data){
//1.通过data构造一个Node对象
Node node=new Node(data);
if(head==null){
//head保存node的值。head引用node的值
this.head=node;//head保存node的值。head引用node的值
return;
}
node.next=head;
head=node;//将head指向node;
}
//尾插法
public void addLast(int data){
Node node=new Node(data);
//为空直接插入
if(head==null){
head=node;
return;
}
//不为空遍历找出尾巴
Node cur=head;
while (cur.next!=null) {
cur=cur.next;
}
cur.next=node;
}
//找到想删除结点前面的结点也就是index-1位置的结点
public Node searchPrev(int index){
int count=0;
Node cur=head;
while(count<index-1){
cur=cur.next;
count++;
}
return cur;
}
//任意位置插入,第一个数据节点为0号下标
public void addIndex(int index,int data){
Node node=new Node(data);
if(index<0||index>size()){
System.out.println("index不合法");
return;
}
if(index==0){
addFirst(data);
return;
}
Node cur=searchPrev(index);
node.next=cur.next;//先将后面连接上
cur.next=node;
}
public int size(){
Node cur=head;
int count=0;
if(cur!=null){
count++;
cur=cur.next;
}
return count;
}
//查找是否包含关键字key是否在单链表当中
public boolean contains(int key){
Node cur=head;
while(cur!=null){
if(cur.data==key){
return true;
}
cur=cur.next;
}
return false;
}
public Node SearchPrevNode(int key){
Node cur=this.head;
while(cur.next!=null){
if(cur.next.data==key){
return cur;
}
}
return null;
}
//删除第一次出现关键字为key的节点
public void remove(int key){
// 1.考虑头结点
if(head.data==key){
head=head.next;
return;
}
//2.找需要删除结点的前驱
Node prev=SearchPrevNode(key);
if(prev==null){
return;
}
Node del=prev.next;
prev.next=del.next;
}
//删除所有值为key的节点
public void removeAllKey(int key){
Node cur=this.head;
Node prev=this.head.next;
while(cur!=null){
if(cur.data==key) {
prev.next = cur.next;
}else{
prev=cur;
}
cur=cur.next;
}
if(this.head.data==key){
this.head=this.head.next;
}
}
//打印链表
public void display(){
Node cur=head;
while (cur!=null) {
System.out.println(cur.data+"");
cur=cur.next;//向后移位置
}
System.out.println();
}
public void clear(){
this.head=null;
}
}
以上是我画的图,便于自己的理解,呜呜呜
TestMySingleList中的代码
public class TestSingleList {
public static void main(String[] args) {
MySingleList mySingleList=new MySingleList();
mySingleList.addLast(1);
mySingleList.addLast(2);
mySingleList.addLast(3);
mySingleList.addLast(4);
mySingleList.addFirst(2);
//mySingleList.remove(4);
// mySingleList.removeAllKey(2);
//mySingleList.clear();
mySingleList.display();
System.out.println("asdfghjkl");
}
}