03數據結構_單鏈表【帶頭循環單鏈表】

帶頭循環單鏈表
【詳細內容】https://blog.csdn.net/qq_36390039/article/details/89060878

//2、帶頭循環單鏈表實現 
public interface ICLinked { 
//頭插法 
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(); 
}

(1)初始化

class  Node{
    private Node next;
    private int data;

    public Node(){
        this.data = -1;
    }
    public Node(int data){
        this.data = data;
    }
}

private Node head;

public CLinkedImpl(){
    this.head = new Node();
    this.head.next = this.head;
}

(2)增加

@Override
public void addFirst(int data) {
    Node node = new Node(data);
    node.next = this.head.next;
    this.head.next = node;
}

@Override
public void addLast(int data) {
    Node node = new Node(data);
    Node cur = this.head;
    while(cur.next!=this.head){
        cur = cur.next;
    }
    node.next = this.head;
    cur.next = node;
}

private void checkIndex(int index){
    if(index<0||index>getLength()){
        throw new IndexOutOfBoundsException("下標非法");
    }
}

@Override
public boolean addindex(int index, int data) {
    Node node = new Node(data);
    Node cur = this.head;
    for(int i=0;i<index;i++){
        cur = cur.next;
    }
    node.next = this.head;
    cur.next = node;
    return true;
}

(3)刪除

@Override
public int remove(int key) {
    int old = 0;
    Node pre = searchPre(key);
    if(pre==null){
        System.out.println("無該節點");
    }
    old = pre.next.data;
    pre.next = pre.next.next;
    return old;
}

@Override
public void removeAllKey(int key) {
    int old = 0;
    Node pre = this.head;
    Node cur = this.head;
    while(cur!=this.head){
        if(cur.data==key){
            pre.next = cur.next;
            cur = cur.next;
        }else{
            cur = pre;
            cur = cur.next;
        }
    }
}

(4)查找

@Override
public boolean contains(int key) {
    if(searchPre(key)!=null){
        return true;
    }
    return false;
}

private Node searchPre(int key){
    Node pre = this.head;
    while(pre.next!=this.head){
       if(pre.next.data==key){
           return pre;
       }else{
           pre = pre.next;
       }
    }
    return null;
}
@Override
public int remove(int key) {
    int old = 0;
    Node pre = searchPre(key);
    if(pre==null){
        System.out.println("無該節點");
    }
    old = pre.next.data;
    pre.next = pre.next.next;
    return old;
}

@Override
public void removeAllKey(int key) {
    int old = 0;
    Node pre = this.head;
    Node cur = this.head;
    while(cur!=this.head){
        if(cur.data==key){
            pre.next = cur.next;
            cur = cur.next;
        }else{
            cur = pre;
            cur = cur.next;
        }
    }
}

(5)計算長度,打印,清空

@Override
public int getLength() {
   Node node = this.head.next;
   int count = 0;
   while(node!=this.head){
       count++;
   }
   return count;
}

@Override
public void display() {
    Node node = this.head.next;
    int count = 0;
    while(node!=this.head){
        System.out.println(node.data+" ");
        node = node.next;
    }
    System.out.println();
}

@Override
public void clear() {
    while(this.head.next!=this.head){
        Node cur = this.head.next;
        this.head.next = cur.next;
        cur.next = null;
    }
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章