LINK LIST

Hi . I want Add a Node To End List But my Program in during compile time Terminated . what is my fault ?
#include<iostream>
using namespace std;
class Node
{
public:
Node();
void newInsert(Node* );
void endInsert(Node* );


private:
int item;
Node* next;
};

int main()
{
Node* head;
Node *p;
p = new Node;
p->newInsert(head);
p->endInsert(head);
return 0;
}
Node::Node() : item(0), next(NULL)
{
}
void Node::newInsert(Node* head)
{
Node* newPtr;
newPtr = new Node;
cout << " Please Enter Item : ";
cin >> newPtr->item;
newPtr -> next = NULL;
head = newPtr;
}

void Node::endInsert(Node* head)
{
Node* Ptr = new Node;
Node* cur = head;
Node* prev = NULL;

cout << " Pleas Enter Item : ";
cin >> Ptr->item;
while(cur != NULL)
{
prev = cur;
cur = cur -> next;
}
if( cur == NULL)
{
Ptr->next = cur;
prev->next = Ptr;
}
}
I don't see anything wrong with your algorithm. Though the if statement in endInsert is redundant. It doesn't work because your functions don't modify the head pointer, as it is passed by value. I added some debug code so that you can see that head's value doesn't change. I also added (in comments) the changes you have to make to your functions signatures so that it works.

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
#include<iostream>
using namespace std;
class Node
{
    public:
    Node();

    void newInsert(Node*);
    void endInsert(Node*);

    //change the above into
    //void newInsert(Node*&);
    //void endInsert(Node*&);

    private:
    int item;
    Node* next;
};

int main()
{
    Node* head;
    Node *p;
    p->newInsert(head);
    p->endInsert(head);
    return 0;
}

Node::Node() : item(0), next(0) {}

void Node::newInsert(Node * head)
//change this into
//void Node::newInsert(Node * & head)
{
    Node* newPtr;
    newPtr = new Node;
    cout << " Please Enter Item : ";
    cin >> newPtr->item;
    newPtr -> next = 0;
    head = newPtr;

    /////////////////////////////////////////////
    cout << "head: " << head << endl;
    cout << "head->next: " << head->next << endl;
    /////////////////////////////////////////////
}

void Node::endInsert(Node * head)
//change this into
//void Node::endInsert(Node * & head)
{
    Node* Ptr = new Node;
    Node* cur = head;
    Node* prev = 0;

    cout << " Please Enter Item : ";
    cin >> Ptr->item;

    /////////////////////////////////////////////
    cout << "head: " << head << endl;
    cout << "head->next: " << head->next << endl;
    /////////////////////////////////////////////

    while(cur != 0)
    {
        prev = cur;
        cur = cur -> next;
    }

    if( cur == 0)
    {
        Ptr->next = cur;
        prev->next = Ptr;
    }
}
Last edited on
Hi .
thanks . it's worke , but i have question why the NULL character doesn't work IT ?
NULL is ok, I just replaced it with zero because, well... I don't really like NULL. It's a macro and its value is 0.

EDIT:

In C, NULL is usually defined like this -> #define NULL ((void*)0)

In C++, NULL is usually defined like this -> #define NULL 0

If you try:

1
2
#undef NULL
#define NULL  ((void*)0) 

you'll see that it won't work, because in C++ implicit casts from void* to other pointer types are not allowed
Last edited on
Thanks
Topic archived. No new replies allowed.