Nasif Imtiaz Ohi
Nasif Imtiaz Ohi

Reputation: 1721

linked list using class in c++

I am trying this linked list using class in C++. I have implemented tree using the same approach before. But in the below code, it seems that the next pointer in the linked_list isn't working as example. The line in the main function has been commented, is where the main problem lies.

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

Upvotes: 0

Views: 200

Answers (1)

HelloWorld123456789
HelloWorld123456789

Reputation: 5369

In class Linked_List, modify 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;
}

In class node, add set_next_node() method:

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

In a linked list, every next of a node should contain the next node. But what you've done is loop till Cur is NULL. If you do that, you cannot set the next of the last node to the new node.

To add a new node after the current node, the set_next_node() method is used.

Upvotes: 2

Related Questions