473,385 Members | 1,693 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

understanding link list

I've been working on a link list implementation and I'm driving myself
crazy trying to understand something.

assuming i have a list and i have the following quasi pseudocode:

add(list *head):
if(head==NULL)
head=newnode
else
while (head)
head->data="something"
head=head->nextNode
I assumed that since head was a ptr whatever was done to in the
function would be reflected once this function terminated? is this true
or is head just a local var of a address? The reason i ask this is
because on my liist if head is null, on exit from the function head
still remains null even though i specfied it should get the new ptr.

HOWEVERRRRR....if head is not null, the program iterates through the
list and updates the list accordingly. If its only supposed to be a
copy how come the second part of the function retains the value after
the function terminates, and how come the first part doesn't. more
precisely how comes all the memory from head+1 and on is updated
seemingly by reference and head is not.

in fact if i write the follwoing:

delete(list *head):
while(head)
temp = head->next
free(head)
head=temp
on exit. everyone one of them is freed. EXCEPT head. obviously i'm not
freeing head but why? Please let me know if i've explained this
properly. i just feel like i'm going crazy trying to understand this.
thanks

Aug 12 '06 #1
4 1738
bitshadow (in 11**********************@i42g2000cwa.googlegroups. com)
said:

| I've been working on a link list implementation and I'm driving
| myself crazy trying to understand something.
|
| assuming i have a list and i have the following quasi pseudocode:
|
| add(list *head):
| if(head==NULL)
| head=newnode
| else
| while (head)
| head->data="something"
| head=head->nextNode
|
|
| I assumed that since head was a ptr whatever was done to in the
| function would be reflected once this function terminated? is this
| true or is head just a local var of a address? The reason i ask
| this is because on my liist if head is null, on exit from the
| function head still remains null even though i specfied it should
| get the new ptr.
|
| HOWEVERRRRR....if head is not null, the program iterates through the
| list and updates the list accordingly. If its only supposed to be a
| copy how come the second part of the function retains the value
| after the function terminates, and how come the first part doesn't.
| more precisely how comes all the memory from head+1 and on is
| updated seemingly by reference and head is not.
|
| in fact if i write the follwoing:
|
| delete(list *head):
| while(head)
| temp = head->next
| free(head)
| head=temp
|
|
| on exit. everyone one of them is freed. EXCEPT head. obviously i'm
| not freeing head but why? Please let me know if i've explained this
| properly. i just feel like i'm going crazy trying to understand
| this. thanks

Your query would be much more clear if you showed your structure
definitions and an actual attempt at the C code. :-)

I have a set of simple list manipulation functions at the link below
that may be of some help.

--
Morris Dovey
DeSoto Solar
DeSoto, Iowa USA
http://www.iedu.com/mrd/c/queue.c

Aug 13 '06 #2
"bitshadow" <ca********@yahoo.comwrites:
assuming i have a list and i have the following quasi pseudocode:

add(list *head):
if(head==NULL)
head=newnode
else
while (head)
head->data="something"
head=head->nextNode
I assumed that since head was a ptr whatever was done to in the
function would be reflected once this function terminated? is this true
or is head just a local var of a address? The reason i ask this is
because on my liist if head is null, on exit from the function head
still remains null even though i specfied it should get the new ptr.
This part of your question, at least, is answered in the C FAQ:

4.8: I have a function which accepts, and is supposed to initialize,
a pointer:

void f(int *ip)
{
static int dummy = 5;
ip = &dummy;
}

But when I call it like this:

int *ip;
f(ip);

the pointer in the caller remains unchanged.

A: Are you sure the function initialized what you thought it did?
Remember that arguments in C are passed by value. The called
function altered only the passed copy of the pointer. You'll
either want to pass the address of the pointer (the function
will end up accepting a pointer-to-a-pointer), or have the
function return the pointer.

See also questions 4.9 and 4.11.

--
"...what folly I commit, I dedicate to you."
--William Shakespeare, _Troilus and Cressida_
Aug 13 '06 #3

bitshadow wrote:
I've been working on a link list implementation and I'm driving myself
crazy trying to understand something.

assuming i have a list and i have the following quasi pseudocode:

add(list *head):
if(head==NULL)
head=newnode
else
while (head)
head->data="something"
head=head->nextNode
I assumed that since head was a ptr whatever was done to in the
function would be reflected once this function terminated? is this true
or is head just a local var of a address? The reason i ask this is
because on my liist if head is null, on exit from the function head
still remains null even though i specfied it should get the new ptr.

HOWEVERRRRR....if head is not null, the program iterates through the
list and updates the list accordingly. If its only supposed to be a
copy how come the second part of the function retains the value after
the function terminates, and how come the first part doesn't. more
precisely how comes all the memory from head+1 and on is updated
seemingly by reference and head is not.
The trick is that the pointer is just a copy, but the thing it points
to is the original.
If you want your function to alter the original pointer, you'll have to
actually pass a copy of the address of the original pointer:

void add( list *newNode, list **head )
{
if ( *head == NULL )
*head = newNode;
else
while ( *head )
{
*head->data = "something";
*head = *head->nextNode;
}
}

And then, rather than
"add( mynode, myhead )",
you would call "add( mynode, &myhead )"

Note that here I'm just copying your pseudocode (and translating it to
C), I'm not endorsing this as being a working "add" in any linked list
sort of sense. If head is not null then it doesnt actually add
anything at all, instead it sets each node's "data" field to point to a
string literal and sets head=NULL.
in fact if i write the follwoing:

delete(list *head):
while(head)
temp = head->next
free(head)
head=temp
on exit. everyone one of them is freed. EXCEPT head. obviously i'm not
freeing head but why? Please let me know if i've explained this
properly. i just feel like i'm going crazy trying to understand this.
thanks
That shouldnt be happening, but you should post the ACTUAL code, not
pseudocode. It's impossible to diagnose syntax errors from pseudocode.

Aug 13 '06 #4

"bitshadow" <ca********@yahoo.comwrote in message
news:11**********************@i42g2000cwa.googlegr oups.com...
I've been working on a link list implementation and I'm driving myself
crazy trying to understand something.

assuming i have a list and i have the following quasi pseudocode:

add(list *head):
if(head==NULL)
head=newnode
else
while (head)
head->data="something"
head=head->nextNode
I assumed that since head was a ptr whatever was done to in the
function would be reflected once this function terminated? is this true
or is head just a local var of a address? The reason i ask this is
because on my liist if head is null, on exit from the function head
still remains null even though i specfied it should get the new ptr.

HOWEVERRRRR....if head is not null, the program iterates through the
list and updates the list accordingly. If its only supposed to be a
copy how come the second part of the function retains the value after
the function terminates, and how come the first part doesn't. more
precisely how comes all the memory from head+1 and on is updated
seemingly by reference and head is not.

in fact if i write the follwoing:

delete(list *head):
while(head)
temp = head->next
free(head)
head=temp
on exit. everyone one of them is freed. EXCEPT head. obviously i'm not
freeing head but why? Please let me know if i've explained this
properly. i just feel like i'm going crazy trying to understand this.
thanks
It is a bit hard to know for sure without the actual sample code but it
looks like you are passing just a pointer to the list to the add and delete
functions. You need to pass the address of the list pointer in order to
update it. My test program was compiled with MSVC++ 2002 as an ANSI C
Program and runs as expected. I modified the add function to add another
node for each call.

#include <stdio.h>
#include <stdlib.h>

typedef struct list
{
struct list *next;
char data[20];
} list;

void add(list **phead);
void delete(list **phead);
void printlist(list *head, char *title);

int main()
{
int n;
list *head = NULL;
printlist(head, "At start:");
/* Add 3 nodes */
for (n = 1; n <= 3; n++)
{
add(&head);
printlist(head, "After add:");
}
delete(&head);
printlist(head, "After delete:");
return 0;
}

void add(list **phead)
{
list *head = *phead;
list* newnode = calloc(1, sizeof(list));
if (head == NULL)
{
sprintf(newnode->data, "1: head node");
*phead = newnode; /* will update ptr outside function */
}
else
{
/* Add another node at end of list */
int n;
for (n = 1; head->next; n++)
head = head->next;
sprintf(newnode->data, "%d: another node", n + 1);
head->next = newnode;
}
}

void delete(list **phead)
{
list *temp;
list *head = *phead;
while (head)
{
temp = head->next;
free(head);
head=temp;
}
*phead = NULL; /* updates ptr */
}

void printlist(list *head, char *info)
{
printf("%s%s\n", info, head ? "" : " <empty>");
for (; head; head = head->next)
printf("%s\n", head->data);
}

/* Expected output:

At start: <empty>
After add:
1: head node
After add:
1: head node
2: another node
After add:
1: head node
2: another node
3: another node
After delete: <empty>

*/

Hope this helps.
Regards,
Dag
Aug 13 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: evilbreadbin | last post by:
Greetings, My problem consists of two parts, the first of which I imagine is only a matter of syntax whilst the second might very well be impossible. I'm not sure if it is of consequence but...
7
by: CheGueVerra | last post by:
First of all Hello all you css freak. geeks and gurus. I just started using css for some web pages I had to do at work and I'im testing some stuff at home to understand more. Now, I wanted to...
7
by: laura | last post by:
I'm trying to understand fully Server.MapPath. I am writing an intranet for a small company and want to be able to put some files accessible to all, hyperlinked from the intranet and therefore,...
7
by: Shawn Windle | last post by:
----begin node.h-------- #ifndef NODE_H #define NODE_H #include <iostream> //NULL using namespace std; class node {
8
by: sudhirlko2001 | last post by:
How to swap two nodes of doubly Linklist
7
by: Leon Shaw | last post by:
Someone please help me understand the following error message: Server Error in '/solo' Application. ---------------------------------------------------------------------------- ---- ...
82
by: Eric Lindsay | last post by:
I have been trying to get a better understanding of simple HTML, but I am finding conflicting information is very common. Not only that, even in what seemed elementary and without any possibility...
18
by: Simon | last post by:
Hi, I understand what one the differences between std::vector, std::deque and std::list is, the std::vector can have data inserted/deleted at the end. The std::deque can have data...
2
by: Greg Corradini | last post by:
Hello All, A few weeks ago, I wrote two scripts using mx.ODBC on an Access DB. Among other things, both scripts create new tables, perform a query and then populate the tables with data in a...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.