package com.bit.bbb.SingleLinkList;
public interface ISingleLinkList {
void addFirst(int data);
void addLast(int data);
boolean addindex(int index,int data);
boolean contains(int key);
int remove(int key);
void removeAllkey(int key);
int getLength();
void display();
void clear();
}
public class MySingleLinkList implements ISingleLinkList {
class Node{
private int data;
private Node next;
public Node(int data) {
this.data=data;
this.next=null;
}
}
private Node head;
public MySingleLinkList(){
this.head=null;
}
@Override
public void addFirst(int data) {
Node node=new Node(data);
if(this.head!=null){
node.next=this.head;
this.head=node;
} else
this.head=node;
}
@Override
public void addLast(int data) {
Node node=new Node(data);
if(this.head==null){
this.head=node;
}
else {
Node cur = this.head;
while(cur.next!=null){
cur=cur.next;
}
cur.next=node;
}
}
@Override
public boolean addindex(int index, int data) {
Node node=new Node(data);
if(index<0){
System.out.println("插入位置不合法");
return false;
}
if(index==0){
node.next=this.head;
this.head=node;
return true;
}
else {
if (this.head == null) {
return false;
}
int count=0;
Node cur=this.head;
Node front =null;
while(cur!=null){
if(count==index) {
front.next=node;
node.next=cur;
return true;
}
else {
front=cur;
cur=cur.next;
count++;
}
}
if(index>=count){
System.out.println("插入位置不合法");
return false;
}
}
return false;
}
@Override
public boolean contains(int key) {
if(this.head==null){
return false;
}
if(this.head!=null){
Node cur=this.head;
while(cur!=null) {
if (cur.data == key) {
return true;
}
else {
cur = cur.next;
}
}
}
return false;
}
@Override
public int remove(int key) {
if(this.head==null){
System.out.println("单链表为空");
return -1;
}
if(this.head!=null) {
Node cur = this.head;
Node front = this.head;
while (cur != null) {
if (cur.data == key) {
front.next = cur.next;
return cur.data;
}
front = cur;
cur = cur.next;
}
}
return -1;
}
@Override
public void removeAllkey(int key) {
if (this.head == null) {
System.out.println("单链表为空");
}
if (this.head != null) {
Node cur = this.head;
Node front = this.head;
while (cur != null) {
if (cur.data == key) {
front.next = cur.next;
}
front = cur;
cur = cur.next;
}
}
}
@Override
public int getLength() {
if(this.head==null){
return 0;
}
int count=0;
if(this.head!=null){
Node cur=this.head;
while(cur!=null){
count++;
cur=cur.next;
}
}
return count;
}
@Override
public void display() {
if(this.head==null){
System.out.println("单链表为空");
}
if(this.head!=null){
Node cur=this.head;
while(cur!=null){
System.out.print(cur.data+" ");
cur=cur.next;
}
System.out.println();
}
}
@Override
public void clear() {
if (this.head == null) {
System.out.println("单链表为空");
}
if (this.head != null) {
this.head = null;
}
}
}
public class Test {
public static void main(String[] args) {
MySingleLinkList mySingleLinkList=new MySingleLinkList();
mySingleLinkList.addFirst(10);
mySingleLinkList.addFirst(20);
mySingleLinkList.addFirst(30);
mySingleLinkList.addFirst(40);
mySingleLinkList.addLast(50);
mySingleLinkList.addLast(20);
mySingleLinkList.addLast(70);
System.out.println(mySingleLinkList.getLength());
mySingleLinkList.display();
System.out.println( mySingleLinkList.addindex(-1,80));
mySingleLinkList.display();
System.out.println(mySingleLinkList.contains(70));
System.out.println(mySingleLinkList.remove(20));
//mySingleLinkList.removeAllkey(20);
mySingleLinkList.display();
mySingleLinkList.clear();
mySingleLinkList.display();
}
}