單鏈表1(悲劇文本)
阿新 • • 發佈:2018-05-12
over delete stream oid type cout null out PE
#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; }
單鏈表1(悲劇文本)