Mar 13, 2010 at 10:29pm UTC
My program complies through, however I am unable to create my Node nor List one. Is it possible I did my code for these wrong or the problem lies in main? And what can I do to make them work?
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 Mar 13, 2010 at 11:21pm UTC
Mar 14, 2010 at 7:33am UTC
No idea, sorry. From the little I understand about LinkedLists, your Node class (which you've called 'A') is correct.
If you add in cout in the section of code that you think the problem is, so that you know what value each of your variables hold at that point... it might help to work out what is causing the problem.
Just remember to remove the debugging cout's when you're done.
Mar 14, 2010 at 3:33pm UTC
Alright, thanks. I'll remember to take them out.