package Interface;
public interface ILinked {
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();
}
package DS;
import Interface.ILinked;
public class MySingleListImpl implements ILinked {
class Node{
private int data;
private Node next;
Node(int data){
this.data = data;
this.next = null;
}
}
private Node head ;
private MySingleListImpl(Node head){
this.head = head;
}
public Node getHead(){
return head;
}
@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;
}
}
@Override
public boolean addindex(int index, int data) {
Node cur = this.head;
if(index<0||index>=getLength()){
return false;
}
if(index == 0){
addFirst(data);
return true;
}
if(index == getLength()-1){
addLast(data);
return true;
}
int count = 0;
Node node = new Node(data);
while(index != count){
cur = cur.next;
count++;
}
node.next = cur.next;
cur.next = node;
return true;
}
@Override
public boolean contains(int key) {
Node cur = this.head;
while(cur!=null){
if(cur.data == key){
return true;
}
cur = cur.next;
}
return false;
}
public Node SearchPrevIndex(int key){
Node prev = this.head;
while(prev.next != null){
if(prev.next.data == key){
return prev;
}
prev = prev.next;
}
return null;
}
@Override
public int remove(int key) {
if(this.head == null){
throw new UnsupportedOperationException("单链表为空");
}
if(this.head.data == key){
this.head = this.head.next;
return key;
}
Node prev = SearchPrevIndex(key);
Node delete = prev.next;
prev.next = delete.next;
return delete.data;
}
@Override
public void removeAllKey(int key) {
if(this.head == null){
throw new UnsupportedOperationException("单链表为空");
}
if(this.head.data == key){
this.head = this.head.next;
}
Node cur = this.head;
while(cur != null){
if(cur.data == key){
Node prev = SearchPrevIndex(key);
prev.next = cur.next;
}
cur = cur.next;
}
}
@Override
public int getLength() {
Node cur = this.head;
int length = 0;
while(cur != null){
length++;
cur = cur.next;
}
return length;
}
@Override
public void display() {
Node cur = this.head;
while(cur != null){
System.out.print(cur.data +" ");
cur = cur.next;
}
System.out.println();
}
@Override
public void clear() {
while(this.head.next!=null){
Node del = this.head.next;
this.head.next = del.next;
}
this.head = null;
}
public static void main(String[] args) {
MySingleListImpl mySingleList = new MySingleListImpl(null);
mySingleList.addFirst(12);
mySingleList.addLast(13);
mySingleList.addFirst(11);
mySingleList.addLast(14);
mySingleList.addLast(16);
mySingleList.display();
System.out.println(mySingleList.contains(16));
System.out.println(mySingleList.getLength());
mySingleList.addLast(16);
mySingleList.addFirst(16);
mySingleList.display();
mySingleList.remove(16);
mySingleList.display();
mySingleList.removeAllKey(16);
mySingleList.display();
}
}
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/973fe9aaf58a2b02c1f4bdd871ac128f.png)