cannot conver 'node*' to 'node**'

I'm new to pointers and can't solve this error "cannot conver 'node*' to 'node**'". What is the problem here?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#include <iostream>

using namespace std;

    struct node{
        int num;
        node *next;
    };

    bool arTuscias(node *head);
    char menu();
    node* nodeKurimas(int num);
    int size=0;

    void sukurimasPradzioje(node *&head, node *&tail, int num);
    void spausdinimas(node *dabartinis);

    void sukurimasReplace(node **dabartinis, int num, int pos);


int main()
{
    node *head = NULL;
    node *tail = NULL;
    char pasirinkimas;
    int num;
    int pos;

    do{
    pasirinkimas = menu();

        switch(pasirinkimas){
            case '1':   cout<<"Iveskite skaiciu: ";
                        cin>>num;
                        sukurimasPradzioje(head,tail,num);
                        break;

            case '2':   cout<<"Spausdinamas visas sarasas: "<<endl;
                        spausdinimas(head);
                        break;

            case '3':   cout<<"Iveskite elemento pozicija"<<endl;
                        cin>>pos;
                        cout<<"Iveskite elementa"<<endl;
                        cin>>num;
                        sukurimasReplace(head,num,pos);
                        break;



            default: cout<<"Toks pasirinkimas neegzistuoja"<<endl;

        };

    }while(pasirinkimas !='7');

    return 0;
}

    bool arTuscias(node *head){
        if(head == NULL)
            return true;
        else
            return false;
    }

    node* nodeKurimas(int num){
        node* temp = new node();
        temp->num=num;
        temp->next=NULL;
        return temp;
    }

    char menu(){
        char pasirinkimas;
        cout<<endl<<"Pasirinkite kokia funkcija norite atlikti"<<endl<<
        "1. Creates linked list"<<endl<<
        "2. Prints linked list"<<endl<<
        "3. Inserts an element to a specified position "<<endl<<
        
        cin>>pasirinkimas;

        return pasirinkimas;
    }
    void sukurimasPradzioje(node *&head, node *&tail, int num){
        if(arTuscias(head)){
        node *temp = new node;
        temp->num = num;
        temp->next = NULL;
        head = temp;
        tail = temp;
        }
    else{
        node *temp = new node;
        temp->num = num;
        temp->next = NULL;
        tail->next = temp;
        tail = temp;
        }
        size++;
    }

    void sukurimasReplace(node **dabartinis, int num, int pos){


    // This condition to check whether the
    // postion given is valid or not.
    if (pos < 1 || pos > size + 1)
        cout << "Invalid postion!" << endl;
    else {

        // Keep looping until the pos is zero
        while (pos--) {

            if (pos == 0) {

                // adding Node at required postion
                node* temp = nodeKurimas(num);

                // Making the new Node to point to
                // the old Node at the same position
                temp->next = *dabartinis;

                // Changing the pointer of the Node previous
                // to the old Node to point to the new Node
                *dabartinis = temp;
            }
            else
              // Assign double pointer variable to point to the
              // pointer pointing to the address of next Node
              dabartinis = &(*dabartinis)->next;
        }
        size++;
    }
}

    void spausdinimas(node *dabartinis){
        if(arTuscias(dabartinis))
            cout<<"Sarasas tuscias"<<endl;
        else{
            cout<<"Saraso elementai:"<<endl;
            while(dabartinis != NULL){
                cout<<dabartinis->num<<endl;
                dabartinis = dabartinis->next;
            }
        }
    }
You pass a pointer as an argument where a 'pointer to pointer' is required. Solution: pass as '&head' instead o 'head'in line 46.

Beside some small other errors, your code is a good example why it's not good dumping the std library stuff into the global namespace: You cannot anymore use 'size' because it's still defined somewhere by including the <iostream> header. So don't use 'using namespace std'..
Last edited on
Topic archived. No new replies allowed.