无头单向非循环链表:结构简单,一般不会单独用来存数据。实际中更多是作为其他数据结构的子结构。
//写一个接口
public interface ILinked {
//头插法
void addFirst(int data);
//尾插法
void addLast(int data);
//任意位置插入,第一个数据节点为0号下标
boolean addindex(int index,int data);
//查找是否包含关键字key是否在单链表当中
boolean contains(int key);
//删除第一次出现关键字为key的节点
int remove(int key);
//删除所有值为key的节点
void removeAllKey(int key);
//得到单链表的长度
int getLength();
//打印链表
void display();
//删除链表
void clear();
}
实现上边的接口
public class ILinkedImpl implements ILinked {
//创造一个Node类
class Node {
private int data;
private Node next;
//成员内部类
public Node(int data){
this.data=data;
this.next=null;
}
}
private Node head;
public ILinkedImpl(){
this.head=null;
}
//----------------------------------------------
//头插法
@Override
public void addFirst(int data) {
Node node=new Node(data);
if(this.head==null){
this.head=node;
}else{
node.next=this.head;
this.head=node;
}
}
//------------------------------------------
//尾插法
@Override
public void addLast(int data) {
Node node=new Node(data);
Node cur=this.head;
if(this.head==null){
this.head=node;
}else{
while(cur.next!=null){
cur=cur.next;
}
cur.next=node;
}
}
//------------------------------------------
//任意位置插入,第一个数据节点为0号下标
//判断输入的index是否合法
public void checkindex(int index){
if(index>getLength()||index<0){
throw new UnsupportedOperationException("index数值非法");
}
}
//找出index-1的位置
public Node indexbefore(int index){
checkindex(index);
Node cur=this.head;
int count=0;
if(index==0){
return null;
}
while(cur.next!=null && count<index-1){
cur=cur.next;
count++;
}
return cur;
}
@Override
public boolean addindex(int index,int data){
Node node=new Node(data);
Node cur=indexbefore(index);
if(cur==null){
node.next=this.head;
this.head=node;
}else{
node.next=cur.next;
cur.next=node;
}
return true;
}
//------------------------------------------
//查找是否包含关键字key是否在单链表当中
@Override
public boolean contains(int key) {
Node node=this.head;
while(node!=null){
if(node.data==key){
return true;
}
node=node.next;
}
return false;
}
//------------------------------------------
//删除第一次出现关键字为key的节点
//找到key以前的位置
private Node keybefore(int key){
Node cur=this.head;
while(cur.next!=null){
if(cur.next.data==key){
return cur;
}
cur=cur.next;
}
return null;
}
@Override
public int remove(int key) {
Node node=this.head;
Node cur=keybefore(key);
int oldData=0;
if(node.data==key){
oldData=this.head.data;
this.head=this.head.next;
return oldData;
}
if(cur==null){
throw new UnsupportedOperationException("无key的节点");
}
Node delNode=cur.next;
cur.next=delNode.next;
return oldData;
}
//------------------------------------------
//删除所有值为key的节点
@Override
public void removeAllKey(int key) {
Node cur=this.head;
Node pre=this.head.next;
if(this.head.data==key){
this.head=this.head.next;
}
while(cur!=null){
if(cur.data==key){
cur.next=pre.next;
pre=pre.next;
}else{
cur=pre;
pre=pre.next;
}
}
}
//------------------------------------------
//得到单链表的长度
@Override
public int getLength() {
Node node=this.head;
int count=0;
while(node!=null){
node=node.next;
count++;
}
return count;
}
//------------------------------------------
//打印链表
@Override
public void display() {
Node cur=this.head;
while(cur!=null){
System.out.println(cur);
cur=cur.next;
}
System.out.println();
}
//------------------------------------------
//删除链表
@Override
public void clear() {
while(this.head!=null){
Node cur=this.head.next;
this.head.next=null;
this.head=null;
}
}
}