【问题标题】:linked list using class in c++在 C++ 中使用类的链表
【发布时间】:2014-02-27 17:47:43
【问题描述】:

我正在使用 C++ 中的类来尝试这个链表。我之前使用相同的方法实现了树。但在下面的代码中,linked_list 中的下一个指针似乎不能作为示例工作。 main 函数中的那行已经被注释掉了,是主要问题所在。

#include<iostream>
#include<cstdio>
using namespace std;
class node{
        node* next;
        char data;
    public:
        node(char x){
            data=x;
            next=NULL;
        }
        node(){
            data='~';
            next=NULL;
        }
        node* get_next_node(){
            return next;
        }
        char get_data(){
            return data;
        }
        void set_data(char x){
            data=x;
        }
};
class Linked_List{
        node *Head;
    public:
        Linked_List(char v){
            Head= new node(v);
        }
        Linked_List(){
            Head= new node();
        }
        void append(char v){
            node *Cur;
            for(Cur=Head;Cur!=NULL;Cur=Cur->get_next_node()){
                ;
            }
            Cur= new node(v);
            cout<<"appending"<<v<<"to"<<Head->get_data()<<endl;
        }
        node* get_Head(){
            return Head;
        }
        void clear(){
            Head=NULL;
        }
        void show(){
            node *Cur;
            for(Cur=Head;Cur!=NULL;Cur=Cur->get_next_node()){
                cout<<Cur->get_data()<<" ";
            }
        }
};
class Graph{
        int vertices;
    public:
        Linked_List *arr;
        Graph(int v){
            vertices=v;
            arr=new Linked_List[v];
        }
        void addEdge(char x, char y){
            int i;
            bool flag;
            bool flag2=false;
            for(i=0;i<vertices;i++){
                if(arr[i].get_Head()->get_data()==x){
                    arr[i].append(y);
                    flag=true;
                    break;
                }
                else if(arr[i].get_Head()->get_data()=='~'){
                    flag=false;
                    break;
                }
            }
            if(flag==false){
                arr[i].get_Head()->set_data(x);
                arr[i].append(y);
            }
            /*int j;
            for( j=0;j<vertices;j++){
                if(arr[j].get_Head()->get_data()==y){
                    flag2= true;
                    break;
                }
                if(arr[j].get_Head()->get_data()=='~'){
                    break;
                }
            }
            if(flag2==false){
                arr[j].get_Head()->set_data(y);
            }*/
        }
        void show(){
            for(int i=0;i<vertices;i++){
                arr[i].show();
                cout<< endl;
            }
        }
};
int main(){
    int v;
    char x,y;
    cin>>v;
    Graph bfs(v);
    int edge;
    cin>>edge;
    for(int i=0;i<edge;i++){
        cin>>x>>y;
        bfs.addEdge(x,y);
    }
    bfs.show();
    /*Linked_List ll('4');
    ll.append('5');
    ll.append('6');
    char a=ll.get_Head()->get_data();
    cout<<a;
     a=ll.get_Head()->get_next_node()->get_data();
    cout<<a;*/
    char a=bfs.arr[0].get_Head()->get_data();
    cout<<a<<endl;
    if(bfs.arr[0].get_Head()->get_next_node()){ //this condition should be true if there       
                                                //is other values. but it's not working.                
        a=bfs.arr[0].get_Head()->get_next_node()->get_data();
    }
    cout<<a;
    return 0;
}
/*
4
5
0 1
0 2
1 3
1 2
2 1
*/

【问题讨论】:

    标签: c++ class linked-list


    【解决方案1】:

    Linked_List类中,修改append()

    void append(char v){
        node *Cur;
        for(Cur=Head;Cur->get_next_node()!=NULL;Cur=Cur->get_next_node()){
            ;
        }
        Cur->set_next_node(new node(v));
        cout<<"appending"<<v<<"to"<<Head->get_data()<<endl;
    }
    

    node类中,添加set_next_node()方法:

    void set_next_node(node *n)
    {
        this->next=n;
    }
    

    在链表中,node 中的每个next 都应该包含下一个节点。但是你所做的是循环直到CurNULL。如果这样做,则无法将最后一个节点的next 设置为新节点。

    在当前节点之后添加一个新节点,使用set_next_node()方法。

    【讨论】:

    • 感谢它正在工作。但是你能解释一下这个问题背后的原因吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-11-06
    • 1970-01-01
    • 2016-06-10
    • 1970-01-01
    • 2013-07-11
    • 1970-01-01
    相关资源
    最近更新 更多