Mar 30, 2013 at 2:50pm Mar 30, 2013 at 2:50pm UTC
This function is invalid
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
void addNodeF( int data ){
node *newnode = new node;
newnode->data = data;
newnode->next = NULL;
node *temp;
temp = head;
if ( temp != NULL ){
while ( temp->next != NULL ){
temp = temp->next;
}
temp->next = newnode;
}
else {
head = newnode;
tail = newnode;
}
}
A new node has un undefined value in member prev.
Also it is not clear what is the difference in algorithms between functions addNodeF and asdNodeR.
Last edited on Mar 30, 2013 at 2:56pm Mar 30, 2013 at 2:56pm UTC
Mar 30, 2013 at 3:06pm Mar 30, 2013 at 3:06pm UTC
What is the difference in algorithms between functions addNodeF and asdNodeR? I am asking this question because it seems they do the same.
Mar 30, 2013 at 3:08pm Mar 30, 2013 at 3:08pm UTC
And why do not you want to define a constructor for Node? For example
1 2 3 4 5 6
struct node{
node( int data ) : data( data ), next( 0 ), prev( 0 ) {}
int data;
node *next;
node *prev;
}*head,*tail;
Last edited on Mar 30, 2013 at 3:08pm Mar 30, 2013 at 3:08pm UTC
Mar 30, 2013 at 3:09pm Mar 30, 2013 at 3:09pm UTC
i just trying to add node from behind.
but that doesn't matter and just ignore it.
because i don't think constructor is neccessary at here.
can u teach me for the algo reverse based on my code?
Mar 30, 2013 at 3:12pm Mar 30, 2013 at 3:12pm UTC
How are you going to reverse the list if it is not clear how you add elements in it?
Last edited on Mar 30, 2013 at 4:13pm Mar 30, 2013 at 4:13pm UTC
Mar 30, 2013 at 3:16pm Mar 30, 2013 at 3:16pm UTC
yea that's why.
that's why dad node from reverse just ignore it.
will try later after i know and understand fully..
becaus ei see some reference but i get it fail.
hope to get understand from my on..
mind teach reverse?
Mar 30, 2013 at 3:24pm Mar 30, 2013 at 3:24pm UTC
Your function displayReverse is correct except that variable c is not being changed.
Mar 30, 2013 at 4:06pm Mar 30, 2013 at 4:06pm UTC
function is right.
but i cannot show it.
because i didnt not assign my tail to the previous linked list.
this the problem when i run.
error msg come out..
how>
Mar 30, 2013 at 4:09pm Mar 30, 2013 at 4:09pm UTC
I said you that your functiions of adding elements are incorrect but you said that it is not important.:)
Mar 30, 2013 at 4:13pm Mar 30, 2013 at 4:13pm UTC
err
u mean assign my linked list to reverse list?
sorry . i can't get what you mean just now.
now only get it ..
Mar 30, 2013 at 4:39pm Mar 30, 2013 at 4:39pm UTC
As I said above function addNodeF is invalid.
Mar 30, 2013 at 4:41pm Mar 30, 2013 at 4:41pm UTC
okay.
i got it.
then what should i change?
at that function i didn't assign my tail position.
i should add what in my while loop there?
Mar 31, 2013 at 12:30pm Mar 31, 2013 at 12:30pm UTC
As i understand function addNodeF has to add a new element at the tail similar member function push_back of standard containers.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
struct node{
int data;
node *next;
node *prev;
node( int data ) : data( data ), next( 0 ), prev( 0 ) {}
}*head,*tail;
void addNodeF( int data ){
node *newnode = new node( data );
if ( tail != 0 ){
newnode->prev = tail;
tail->next = newnode;
}
else {
head = newnode;
}
tail = newnode
}
Last edited on Mar 31, 2013 at 12:53pm Mar 31, 2013 at 12:53pm UTC