一個簡單的單向鏈表-C++

SinglyLinkedList.h

    #include<stdlib.h> 
    #include<iostream>
    class node{
        public:
            int data;//結點的值 
            node *next;//保存後繼結點的地址 
            node(){
                data = -1;
                next = NULL;
            }
            //構造方法,方便快速創建結點 
            node(int data,node *next){
                this->data = data;
                this->next = next;
            }
    };

    class List{
        private:
            node* head;//指向表頭(表頭恆爲空) 
            int lenth;//單鏈表當前最大下標值 
            bool check(int index);//工具方法,檢測index合法性 
        public:
            int size(){return lenth+1;}//返回鏈表長度 
            List();//構造函數
            List(const List& temp);//拷貝構造函數 
            ~List();//析構函數
            
            void append(const int num);//添加到表尾 
            void insert(int index,int num);//在index處添加元素num
            void delnum(int index);//刪除index處元素
            void clear();//清除操作 
            void combine(int index,const List& temp);//index後面插入另一個鏈表 
            
            int& operator[](int index);//重載[]運算符 
            int search(int aim);//搜索aim元素 
            void show();//顯示鏈表的全部元素
    };
    //檢查邊界 
    bool List::check(int index){
        if(index<0||index>lenth){
            std::cout << "INDEX_ERROR" << std::endl;
            return false;
        }
        return true;
    }
    //構造函數
    List::List(){
        head = new node;
        lenth = -1;
    }
    //拷貝構造函數
    List::List(const List& copy){
        node* p1 = head;
        node* p2 = copy.head->next;
        while(p2!=NULL){
            p1->next = new node(p2->data,NULL);
            p1 = p1->next;
            lenth++;
            p2 = p2->next;
        }
    } 
    //析構函數 
    List::~List(){
        node* p1;//p1在p2的前面,p1負責指向待刪除結點,p2負責向後遍歷 
        node* p2 = head;
        while(p2->next!=NULL){
            p1 = p2;
            p2 = p2->next;
            delete p1;
        }
        delete p2;
    }
    //附加操作 
    void List::append(const int num){
        node* p = head;//p用來遍歷鏈表 
        while(p->next!=NULL) p=p->next;//向後遍歷直到結尾 
        p->next = new node(num,NULL);//在結尾創建一個新的結點 
        lenth++;//最長下標+1
    }
    //插入操作 
    void List::insert(int index,int num){
        if(index<0||index>lenth+1){
            std::cout << "INDEX_ERROR" << std::endl;
            return;
        }
        node* p1 = head;//p1負責指向被插入結點的前繼(index-1)位置 
        node* p2 = new node(num,NULL);//p2存儲新結點 
        for(int i=0;i<index;i++) p1 = p1->next;//p1向後遍歷尋找index-1的位置 
        p2->next = p1->next;//新結點(p2)指向p1的後繼結點 
        p1->next = p2;//p1指向新結點(p2) 
        lenth++;
    }
    //刪除操作
    void List::delnum(int index){
        if(!check(index)) return;
        node* p1 = head;
        node* p2;
        for(int i=0;i<index;i++) p1 = p1->next;
        p2 = p1->next;
        p1->next = p2->next;
        delete p2;
        lenth--;
    } 
    //清除操作 
    void List::clear(){
        node* p1;
        node* p2 = head->next;
        while(p2->next!=NULL){
            p1 = p2;
            p2 = p2->next;
            delete p1;
        }
        delete p2;
        head->next = NULL;
        lenth = 0;
    }
    //插入另一鏈表 
    void List::combine(int index,const List& inserted){
        if(!check(index)) return;
        node* p1 = head;
        node* p2 = inserted.head->next;
        for(int i=0;i<index;i++) p1 = p1->next;
        while(p2!=NULL){
            node* temp = new node(p2->data,p1->next);
            p1->next = temp;
            p2 = p2->next;
            lenth++;
        }
    }
    //重載[] 
    int& List::operator[](int index){
        if(!check(index)) return this->head->data;
        node* p = this->head;
        for(int i=0;i<=index;i++) p=p->next;
        return p->data;
    }
    //搜索操作
    int List::search(int aim){
        node* p = head->next;
        int ans = 0;
        while(p!=NULL && p->data!=aim){
            p = p->next;
            ans++;
        }
        if(p==NULL) return -1; 
        return ans;
    } 
    //顯示操作 
    void List::show(){
        node* p = head->next;
        while(p!=NULL){
            std::cout << p->data << " ";
            p = p->next;
        }
        std::cout << std::endl;
    }


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