Errors at CreateNode & ListNodes

My program complies through, however I keep getting errors after I type the ID in for CreateNode and after typing L, I get nothing for ListNodes and if i type something and press enter it keeps outputing 0's.

Would print work for listing the IDs other than ListNodes?
And what can I do to be able to create the ID in CreateNode?

Thank you for your time in advance.

Here is my code:

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
#include <iostream>
#include <string>  
using namespace std;

struct A
	{
	 int key;
	 int ID;
	 A *next;
    }; typedef A *APtr;
    
char Menu()
{
     char C;
    cout<<"\n C) Create Node"<<endl;
	cout<<"\n D) Delete Node"<<endl;
	cout<<"\n L) List Nodes"<<endl;
	cout<<"\n U) Update Node"<<endl;
	cout<<"\n E) Exit"<<endl;
	cin>>C;
	
	return C;
	
}

void DeleteNode(A *d, int key)
{
 A *t;
 if(d->next)
 {
            if(d->next->key == key)
            {
                            t = d->next; d->next = d->next->next; delete t;
            }
            else if(d->next->next&&d->next->key<key)DeleteNode(d->next, key);
 }
}

A *FindParentNode(A *node)
{
                A *p,*f,*l;
                p = f = l;
                while(f&&f->key<node->key)
                {
                        p = f; f = f->next;
                }
                return p;
}

void InsertNode(A *node)
{
            A *p;
            p = FindParentNode(node);
            node->next = p->next;
            p->next = node;
}

void ListNodes(A *list)
{
          while(list) cout<<list->key<<endl;
                      list->next;
}

void UpdateNode(A *node)
{
            A *p;
            p = FindParentNode(node);
            node->next = p->next;
            p->next = node;
}

A *CreateNode(int ID)
{
    A *N;
    N = new A; N->key = ID;
    N->next = 0;
    return N;           
}

int main()
{
    int x;
    A *N,*l;
    char C;
    C = Menu();
    while(1)
    {
     switch (C)
     {
     case 'C':
	 case 'c':
        cout<<"Please enter in ID:";
        cin>>x;
		l = CreateNode(0);
		if(0)
		{
		     InsertNode(l);
		     C = Menu();
        }
		else cout<<"Error."<<endl;
		     cout<<"Please try again."<<endl;
		     C = Menu();
		break;
	 case 'D':
	 case 'd':
        cout<<"Please enter in ID:"<<endl;
        cin>>x;
		DeleteNode(l,x);
		C = Menu();
		break;
	 case 'L':
	 case 'l':
        cin>>x;
        ListNodes(l);
        C = Menu();
		break;
	 case 'U':
	 case 'u':
        cout<<"Please enter in ID:"<<endl;
        cin>>x;
        l = CreateNode(0);
        if(0)
        {
		     UpdateNode(l);
		     C = Menu();
        }
		     else cout<<"Error."<<endl;
		     cout<<"Please try again."<<endl;
		     C = Menu();
		break;
	 case 'E':
	 case 'e':
		cout<<"\n Thank you."<<endl;
		cout<<"\n\n Have a nice day."<<endl;
		exit(0);
		break;
      }
     }
}
Last edited on
make the modification

1
2
3
4
5
6
   l = CreateNode(0);
		if(l != NULL)
		{
		     InsertNode(l);
		     C = Menu();
                 }


also int the FindParentNode

p = f = l;

is wrong.
soory to say that you have written this program in very complex way .. try to make it simples .
What does the findparentnode function do ?
I think it is wrong and you have to modify it .
The if(l !=NULL) makes it crash after the typing of a number then a press of enter. I am glad you got my attention of the FindParentNode. Earlier I was trying to use a tree for stacks and queues. Sadly I did not put them in and tried to use it as a find function. Silly me. I simply want to find the ID to check if it's already created and to find one to update one into a new ID.

InsertNode has FindParentNode in so that is perhaps the problem like you said. The error is lurking around that function whenever I try inserting so the FindParentNode inside it must be the cause.

I'm going to make it Find and see what I get.

Play with it more then come back here if it's still not working.

Thanks and sorry for making it complexed ^^;

Edit: Alright, I took the FindParentNode out the function and placed the "if(l != NULL)" in and.... it WORKS! Thank you so much!

I just need to get it to display the IDs and it will be finished. ^_^
Last edited on
Topic archived. No new replies allowed.