这次的单链表中加入了尾插以及从指定位置插入,以及删除链表中所有为Key的元素。
public interface IMySingleList {
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 MySingleList implements IMySingleList.ILinked{
private Node head;
private int size;
public MySingleList(){
this.head=null;
this.size=0;
}
private class Node{
int data;
Node next;
public Node(int data){
this.data=data;
this.next=null;
}
}
public boolean isEmpty(){
return this.head==null;
}
@Override
public void addFirst(int data) {
Node newHead=new Node(data);
if(isEmpty()){
head=newHead;
}else{
newHead.next=head;
head=newHead;
}
size++;
}
@Override
public void addLast(int data) {
Node node=new Node(data);
Node cur=head;
while (cur.next!=null){
cur=cur.next;
}
cur.next=node;
size++;
}
@Override
public boolean addIndex(int index, int data) {
if(index<0||index>size) {
return false;
}else {
Node node=new Node(data);
Node dumyHead=new Node(-1);
dumyHead.next=head;
Node cur=head;
Node prev=dumyHead;
while (index!=0){
cur=cur.next;
prev=prev.next;
index--;
}
prev.next=node;
node.next=cur;
}
this.size++;
return true;
}
@Override
public boolean contains(int key) {
if (isEmpty()){
return false;
}
Node cur=head;
while (cur!=null){
if(cur.data==key){
return true;
}
cur=cur.next;
}
return false;
}
@Override
public int remove(int key) {
if (isEmpty()){
throw new UnsupportedOperationException("链表为空");
}
Node cur=head;
Node prev=new Node(-1);
prev.next=head;
while (cur.data!=key){
if(cur.next==null){
return 0;
}else {
prev=prev.next;
cur=cur.next;
}
}
if(cur==head){
head=head.next;
this.size--;
}else {
prev.next=cur.next;
this.size--;
}
return 0;
}
@Override
public void removeAllKey(int key) {
if (isEmpty()){
throw new UnsupportedOperationException("链表为空");
}
Node cur=head;
Node prev=new Node(-1);
prev.next=head;
int count=0;
while (cur!=null){
if (cur.data==key){
remove(key);
cur=cur.next;
}else {
cur=cur.next;
prev=prev.next;
}
}
}
@Override
public int getLength() {
return this.size;
}
@Override
public void display() {
Node cur = head;
if (isEmpty()) {
System.out.println("[]");
} else if (cur.next == null) {
System.out.println("[" + cur.data + "]");
} else {
for (cur = head; cur != null; cur = cur.next) {
if (cur == head) {
System.out.print("[" + cur.data + "->");
}else if(cur.next==null){
System.out.print(cur.data+"]");
}else {
System.out.print(cur.data+"->");
}
}
System.out.println();
}
}
@Override
public void clear() {
while (head!=null){
head=head.next;
this.size--;
}
}
}
public class TestMySingleList {
public static void main(String[] args) {
MySingleList mySingleList=new MySingleList();
mySingleList.addFirst(1);
mySingleList.addFirst(3);
mySingleList.addLast(2);
mySingleList.addLast(4);
mySingleList.addIndex(2,3);
mySingleList.display();
mySingleList.removeAllKey(3);
mySingleList.display();
mySingleList.remove(4);
mySingleList.display();
System.out.println(mySingleList.contains(1));
System.out.println(mySingleList.contains(6));
mySingleList.clear();
System.out.println(mySingleList.getLength());
}
}