单链表1(悲剧文本)

#include"iostream"
using namespace std;
typedef char element;
class List{
private:
    element data;
    
public:List *next;
    List(int data = 0){
        this->data = data;
        this->next = NULL;
    }
    void deleteNote(){            //删除后一个结点
        List *temp = this->next;
        this->next = this->next->next;
        delete temp;
    }
    void show(){
        List *p = this->next;
        while(p){
            cout<<p->data;
            p = p->next;
        }
        cout<<endl;
    }
    List *insertf(element data){
        List *newp = new List(data);
        if(!newp){
            cout<<"out of space!"<<endl;
            return NULL;
        }
        newp->next = this->next;
        this->next = newp;
        return newp;
    }
    List *insertl(element data){
        List *newp = new List(data);
        if(!newp){
            cout<<"out of space!"<<endl;
            return NULL;
        }
        List *last = this;
        while(last->next){
            last = last -> next;
        }
        newp->next = last->next;
        last->next = newp;
        return newp;
    }
    void recover(char *s){
        int i = 0;
        List *p = this;
        while(s[i]!='\0'){
            if(s[i]=='['){
                p = this->insertf(s[++i]);
            }
            else if(s[i] == ']'){
                p = p->insertl(s[++i]);
            }
            else{
                p = p->insertf(s[i]);
            }
            i++;
        }
    }
};
    
//kdg[gek]h[itj
//de[co]vs
int main(){
    List L,*p = &L;
    char *str = "kdg[gek]h[itj";
    L.recover(str);
    L.show();
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/oleolema/p/9028397.html