C++以類的形式實現鏈表

#include<iostream>
using namespace std;
class node
{
private:
    
public:
    char data;
    node* next;
    node(){
        this->next = NULL; 
    }
    // void setData(char c){this->data = c;}
    // void setNext(node* p){this->next = p;}
    // char getData(){return this->data;}
    // node* getNext(){return this->next;}
};
int main(){
    node* head = NULL;
    node node1,node2,node3,node4,node5;
    head = &node1;
    node1.data='A';
    node2.data='b';
    node3.data='C';
    node4.data='D';
    node5.data='r';
    head->next = &node2;
    node2.next = &node3;
    node3.next = &node4;
    node4.next = &node5;
    //遍歷鏈表
    for(node* p = head;p->next!=NULL;){
        cout<<p->data<<endl;
        p = p->next;
    }
    system("pause");
}

上述沒有使用類似Java的get/set方法的形式對數據與指針進行賦值。

下面給出get/set方法形式賦值的類定義

#include<iostream>
using namespace std;
class node
{
private:
    char data;
    node* next;
public:
    node(){
        this->next = NULL; 
    }
    void setData(char c){this->data = c;}
    void setNext(node* p){this->next = p;}
    char getData(){return this->data;}
    node* getNext(){return this->next;}
};

int main(){
    node* head = NULL;
    node node1,node2,node3,node4,node5;
    head = &node1;
    node1.setData('A');
    node2.setData('b');
    node3.setData('C');
    node4.setData('D');
    node5.setData('r');
    head->setNext(&node2);
    node2.setNext(&node3);
    node3.setNext(&node4);
    node4.setNext(&node5);
    //遍歷鏈表
    for(node *p = head;p!=NULL;p = p->getNext()){
        cout<<p->getData()<<endl;
    }
    system("pause");
}

然後使用鏈表構建一個鏈式棧。

#include<iostream>
using namespace std;
class node
{
private:
    char data;
    node* next;
public:
    node(){
        this->next = NULL; 
    }
    void setData(char c);
    void setNext(node* p);
    char getData();
    node* getNext();
};
void node::setData(char c){this->data = c;}
void node::setNext(node* p){this->next = p;}
char node::getData(){return this->data;}
node* node::getNext(){return this->next;}

//元素壓棧
void push(node** headp,node* n,char c){
    n->setData(c);
    n->setNext(*headp);
    *headp = n;
}
//元素出棧
char pop(node** headp){
    char c = (*headp)->getData();
    *headp = (*headp)->getNext();
    return c;
}
int main(){
    node* head = new node();
    head->setData('A');
    //壓棧操作
    push(&head,(new node()),'B');
    push(&head,(new node()),'C');
    push(&head,(new node()),'D');
    //彈棧操作
    cout<<pop(&head)<<endl;
    cout<<pop(&head)<<endl;
    cout<<pop(&head)<<endl;
    cout<<pop(&head)<<endl;
    system("pause");
}

然後用鏈棧實現判斷括號組是否合法:

#include<iostream>
#include <cstdlib>
using namespace std;
class node
{
private:
    char data;
    node* next;
public:
    node(){
        this->next = NULL; 
    }
    void setData(char c);
    void setNext(node* p);
    char getData();
    node* getNext();
};
void node::setData(char c){this->data = c;}
void node::setNext(node* p){this->next = p;}
char node::getData(){return this->data;}
node* node::getNext(){return this->next;}

//元素壓棧
void push(node** headp,node* n,char c){
    n->setData(c);
    n->setNext(*headp);
    *headp = n;
}
//元素出棧
char pop(node** headp){
    *headp = (*headp)->getNext();
    char c = (*headp)->getData();
    return c;
}
//判斷兩個元素是否適配
bool checkp(char c1,char c2){
    if(c1=='{'&&c2=='}') return true;
    if(c1=='('&&c2==')') return true;
    if(c1=='['&&c2==']') return true;
    return false;
}
//然後這裏代碼有錯,我實在不想寫了
bool check(char *k,int size){
    bool result = false;
    node* head = new node();
    char jp = k[0];
    //開始壓棧
    for(int i=0;i<size;i++){
        if(i==0){
            push(&head,new node(),k[i]);
            continue;
        }
        if(checkp(jp,k[i])){  //期待得到滿足,將一個元素出棧,執行下一輪循環
            cout<<"元素"<<pop(&head)<<"出棧"<<endl;
            continue;
        }else    //期待不滿足,元素壓棧
        {
            cout<<"元素"<<k[i]<<"壓棧"<<endl;
            node* n = new node();
            push(&head,n,k[i]);
            jp = k[i];   
        }
    }
    //判斷棧是否爲空
    if(((head->getNext())==NULL)){
        result = true;
    }
    return result;
}
int main(){
    char s[6] = {'{','[','[',']',']','}'};
    cout<<check(&s[0],6)<<endl;
    system("pause");
}

 

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