在 C++ 中使用类的链表

linked list using class in c++

本文关键字:链表 C++      更新时间:2023-10-16

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

#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
*/

在类 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都应包含下一个节点。但是你所做的是循环直到Cur NULL。如果这样做,则无法将最后一个节点的next设置为新节点。

若要在当前节点之后添加新节点,请使用 set_next_node() 方法。