473,661 Members | 2,448 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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="somethin g"
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 1751
bitshadow (in 11************* *********@i42g2 00...legr oups.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="somethin g"
| 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********@yah oo.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="somethin g"
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="somethin g"
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********@yah oo.comwrote in message
news:11******** **************@ i42g2000cwa.goo glegroups.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="somethin g"
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
1902
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 PHP is running on a Win2000 machine and the ODBC connection in question links to an MS-Access database. 1/ I am trying to get my head around odbc_columns(). When using the
7
1505
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 understand better of how cascading works. in my .css file I created this tr#noLine{ border-top-width:0; border-left-width:0;
7
2358
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, outside of the website area - the idea being that I want staff to be able to copy files into folders that can be accessed by hyperlinks on the intranet pages. I've had a problem separating them from the intranet HTML and ASP pages, which is...
7
631
by: Shawn Windle | last post by:
----begin node.h-------- #ifndef NODE_H #define NODE_H #include <iostream> //NULL using namespace std; class node {
8
4161
by: sudhirlko2001 | last post by:
How to swap two nodes of doubly Linklist
7
1710
by: Leon Shaw | last post by:
Someone please help me understand the following error message: Server Error in '/solo' Application. ---------------------------------------------------------------------------- ---- Configuration Error Description: An error occurred during the processing of a configuration file required to service this request. Please review the specific error details below and modify your configuration file appropriately.
82
6301
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 of getting wrong it seems I am on very shaky ground . For example, pretty much every book and web course on html that I have read tells me I must include <html>, <head> and <body> tag pairs. I have always done that, and never questioned it. ...
18
2191
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 inserted/deleted at the end and at beginning and the std::list can have data inserted/deleted anywhere. But how can those differences be physically understood?
2
1854
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 dictionary that I've uploaded from elsewhere. These scripts have run hundreds of times in the last few weeks with no problems. But recently they continue to bail on the mycursor.execute('An SQL Statement') after the table has been created. I get the...
0
8428
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8341
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8851
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8630
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7362
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6181
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4343
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1984
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1740
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.