链表:
是一种物理存储结构上非连续存储结构,数据元素的逻辑顺序是通过链表中的引用链接次序实现的 。
实际中链表的结构非常多样,以下情况组合起来就有8种链表结构:
单向、双向
带头、不带头
循环、非循环
对这六种排列组合即可得到八种链表的名称;
我们今天实现的是 无头单向链表
```java
// 1、无头单向非循环链表实现
public class SingleLinkedList {
//头插法
public void addFirst(int data);
//尾插法
public void addLast(int data);
//任意位置插入,第一个数据节点为0号下标
public boolean addIndex(int index,int data);
//查找是否包含关键字key是否在单链表当中
public boolean contains(int key);
//删除第一次出现关键字为key的节点
public void remove(int key);
//删除所有值为key的节点
public void removeAllKey(int key);
//得到单链表的长度
public int size();
public void display();
public void clear(); }
`
```java
```java
class LinkedNode{
public int data;
public LinkedNode next = null;
public LinkedNode(int data) { //构造方法
this.data = data;
}
}
public class LinkedList { //头插
private LinkedNode head = null;
//创建第一个结点
public void addFirst(int elem) {
LinkedNode node = new LinkedNode(elem);
if (head == null) { //空链表
this.head = node;
return;
} else {
node.next = head;
this.head = node;
return;
}
}
//显示链表
public void display() {
LinkedNode prv;
if (this.head == null) {
System.out.println("空链表!");
return;
}
System.out.print("[");
for (prv = this.head; prv != null; prv = prv.next) {
if (prv.next != null) {
System.out.print(prv.data + ",");
} else System.out.print(prv.data);
}
System.out.print("]");
}
//尾插
public void addLast(int elem) {
LinkedNode node = new LinkedNode(elem);
if (this.head == null) { //空链表
this.head = node;
return;
}
LinkedNode cur = this.head;
while (cur.next != null) {
cur = cur.next;
}
cur.next = node;
}
//任意添加结点
public void addIndex(int index, int elem) {
LinkedNode node = new LinkedNode(elem);
int len = size();
if (index < 0 || index > len) {
return;
}
if (index == 0) { //头插
addFirst(elem);
return;
}
if (index == len) {//尾插
addLast(elem);
return;
}
//中间插入
LinkedNode cur = this.head;
for (int i = 0; i < index - 1; i++) {
cur = cur.next;
}
node.next = cur.next;
cur.next = node;
return;
}
public int size() {
int count = 0;
LinkedNode cur = this.head;
while (cur.next != null) {
cur = cur.next;
count++;
}
return count;
}
//判断元素是否存在
public boolean contains(int toFind) {
// 直接遍历链表, 依次比较每个元素就行了
if(this.head==null){
return false;
}
LinkedNode cur=this.head;
for(cur=this.head;cur!=null;cur=cur.next){
if(cur.data==toFind){
return true;
}
}
return false;
}
//删除指定值
public void remove(int toRemove) {
if(this.head==null){ //空链表情况
System.out.println("空链表!");
return ;
}
//头删除
if(toRemove==this.head.data){
this.head=head.next;
return;
}
//别的地方删除,先找前面结点
LinkedNode prv=getPlace(toRemove);
prv.next=prv.next.next;
}
private LinkedNode getPlace(int toRemove){
LinkedNode cur=this.head;
for(;cur!=null;cur=cur.next){
if(cur.next.data==toRemove){
return cur;
}
}
return null;
}
//删除所有key的值
public void removeAllKey(int elem){
if(this.head==null){
return;
}
LinkedNode prev=head;
LinkedNode cur=head.next;
while(cur!=null){
if(cur.data==elem){
prev.next=cur.next;
cur=prev.next;
}
else {
prev=cur;
cur=cur.next;
}
}
if(this.head.data==elem){
this.head=this.head.next;
}
}
public void clear(){
this.head=null;
}
}
``