C++ Learning Community Forum
August 01, 2010, 03:27:08 AM *
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
News: Hello. Smiley
 
   Home   Help Search Login Register  
Pages: 1 [2]
  Print  
Author Topic: C: linked list problem  (Read 1159 times)
FrozenKnight
ASM Freak
Global Moderator
Dr. of C++ology
*****
Posts: 546


Do it yourself it's the only way to learn.


View Profile
« Reply #15 on: August 20, 2009, 12:22:27 PM »

Umm, why are you using memcpy() anyway? most people who use linked lists like this done have an need for memcpy() unless duplicating a node.
usually you would just move back one in the chain (which is why a lot of linked lists have a prev_node var) then use something like
Code:
prev_node->next_node = next_node;
then delete or free or return the current node. there really isn't a need for memcpy() in the pop function, unless i am misunderstanding what exactly you are doing. Give me a few more minutes i'll whip up a linked list example program to show you what I mean.
Logged


Imagine the impossible, then make it happen.
FrozenKnight
ASM Freak
Global Moderator
Dr. of C++ology
*****
Posts: 546


Do it yourself it's the only way to learn.


View Profile
« Reply #16 on: August 20, 2009, 01:47:23 PM »

hears an example of what I am talking about
Code:
#include <stdio.h>


struct node_data /*first define your variables i'm using a C++ compiler so i dont need the type_def*/
{
char *data;
node_data *prev, *next;
};

struct node_handler
{
node_data *first, *current;
int node_count;
};

/*** note with these functions you are responsiable for creating or deleting the data ***/
node_data* push(node_handler *handle, node_data *data)
{
data->prev = data->next = NULL;
if (handle->node_count == 0)
{
handle->first = data;
} else {
data->prev = handle->current;
data->next = handle->current->next;
}
handle->current = data;
handle->node_count++;
return handle->current;
}

node_data* pop(node_handler *handle)
{
node_data* temp;
temp = handle->current;
if (temp == NULL)
return NULL;
if (temp->prev != NULL)
temp->prev->next = temp->next;
handle->current = temp->prev;
if (temp->next != NULL)
temp->next->prev = temp->prev;
handle->node_count--;
return temp;
}

/*this is a refrence of a way to cleanup, i didn't use this because i locally allocated everything for convience but you may need something like it*/
void cleanup_all(node_handler *handle)
{
node_data *currentnode, *nextnode;

if (handle->first != NULL)
currentnode = handle->first;
else if (handle->current != NULL)
currentnode = handle->current;
else
return;
while (currentnode->next != NULL)
{
nextnode = currentnode->next;
delete currentnode;
nextnode->prev = NULL;
currentnode = nextnode;
}
}

int main()
{
node_handler handle;  /*create and fill your node handler function*/
handle.node_count = 0;
handle.first = NULL;
handle.current = NULL;

node_data  one, two, three, *temp;

one.data = "This is the first node\n";
two.data = "This is the second node\n";
three.data = "This is the third node\n";

push(&handle, &one);
push(&handle, &two);
push(&handle, &three);

printf ("there are %d nodes stored\n", handle.node_count);

while (handle.node_count > 0) {
temp = pop(&handle);
    if (temp != NULL)
printf( temp->data);
printf ("there are %d nodes stored\n", handle.node_count);
}

/*i did not need to delete anything because i allocated everything locally. EG: not using the new and delete or maloc or free*/
return 1;

}
Logged


Imagine the impossible, then make it happen.
cfk
N00b!!1
*
Posts: 13


View Profile
« Reply #17 on: August 20, 2009, 02:38:24 PM »

Well, it works..... but "cheating" using a size count variable isn't helping you learn pointers is it?

and your linked list has a node_count but my only problem is that the data doesn't get copied
i wanted to make smth similar to this but in just C http://svn.assembla.com/svn/WAAD_Ascent/trunk/2_4_3_trunk/src/ascent-shared/FastQueue.h
Logged
FrozenKnight
ASM Freak
Global Moderator
Dr. of C++ology
*****
Posts: 546


Do it yourself it's the only way to learn.


View Profile
« Reply #18 on: August 20, 2009, 10:57:51 PM »

Ok, there is a lot more going on there than a simple C program could do. to match that program you would have to intertwine a lot of code. second the reason memcpy() isn't copying your data is because in your struct you are using a pointer to your data. so what memcpy() is copying is your pointer, to actually copy your data you would need to redirect memcpy() to the memory pointed at by your data pointer.

In your example the code
Code:
struct node
{
T element;
node * next;
};
the T element is actually storing a complete class of type T into the node struct, allowing functions like memcpy() to work correctly.
Logged


Imagine the impossible, then make it happen.
Pages: 1 [2]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.11 | SMF © 2006-2009, Simple Machines LLC Valid XHTML 1.0! Valid CSS!