linked list
May 14, 2008 at 1:57am UTC
i have this linked list which creates a list then displays it. it compiles but the append function is having problems, can ne1 c what im doing wrong.thanks
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
#include <iostream>
#include <cstdlib>
#include <cassert>
using namespace std;
struct nodeType
{
int info;
nodeType *link;
};
void printList(nodeType *first);
void append(nodeType * first, int x);
int main()
{
int x;
nodeType *head = NULL;
cout << "Enter some ints - terminate with control-Z " << endl;
while (cin >> x)
append(head, x);
cout << endl;
printList(head);
system("pause" );
return EXIT_SUCCESS;
}
void append(nodeType * first, int x)
{
// append a node containing x to
// the end of the list first
nodeType *temp = new nodeType;
temp ->info = x;
temp->link = NULL;
if (first == NULL)
first = temp;
else
{
nodeType *p = first;
assert (p != NULL);
while (p != NULL)
p = p -> link;
p->link = temp;
}
}
void printList(nodeType *first)
{
// displays the list
// pointed to by first
nodeType *current = first;
while (current != NULL)
{
cout << current->info << " " ;
current = current->link;
}
cout << endl;
}
May 14, 2008 at 3:45am UTC
I think you mean to check that (p->link != NULL)
[line 44].
May 14, 2008 at 6:52am UTC
Another thing I want to add...
U need to change the prototype of the below to methods
from
1 2
void printList(nodeType *first);
void append(nodeType * first, int x);
to
1 2
void printList(nodeType **first);
void append(nodeType ** first, int x);
Then make necessary changes in the corresponding methods..
May 14, 2008 at 6:57am UTC
I don't think he needs to change
printList
, and I think this will be better:
void append(nodeType *& first, int x);
Then he shouldn't need to change the code.
Last edited on May 14, 2008 at 7:00am UTC
May 14, 2008 at 8:05am UTC
Yes repez U r right, Sorry my mistake :-(.
Last edited on May 14, 2008 at 8:06am UTC
Topic archived. No new replies allowed.