473,320 Members | 1,862 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,320 software developers and data experts.

Linked list declaration

Something I don't really understand.

In http://maxnoy.com/interviews.html, Insert and Delete for linked list
are defined as:

int Insert(node **head, int data)

and

int Delete(node** head, int deleteMe)

I thought linked-list is always defined as something like struct node
*head. Why the author defined the linked-list as pointer to pointer?
And in the next question he defines head in Split() as node *head. This
is really confusing me

Sep 19 '06 #1
4 5601
Wo*********@gmail.com wrote:
Something I don't really understand.

In http://maxnoy.com/interviews.html, Insert and Delete for linked list
are defined as:

int Insert(node **head, int data)
If you have a node *head; and want to delete the first node,
you need to change that pointer (to e.g. point to the next node).

Passing 'head' to a delete function, it can't change that pointer
only what it points to. So for the delete function to change that
pointer you must pass the address of that pointer.
and

int Delete(node** head, int deleteMe)

I thought linked-list is always defined as something like struct node
*head. Why the author defined the linked-list as pointer to pointer?
And in the next question he defines head in Split() as node *head. This
is really confusing me
Likely the Split function doesn't need to change the pointers of the caller,
just what the nodes points to.
Sep 19 '06 #2


Wo*********@gmail.com wrote:
Something I don't really understand.

In http://maxnoy.com/interviews.html, Insert and Delete for linked list
are defined as:

int Insert(node **head, int data)

and

int Delete(node** head, int deleteMe)

I thought linked-list is always defined as something like struct node
*head. Why the author defined the linked-list as pointer to pointer?
And in the next question he defines head in Split() as node *head. This
is really confusing me
Passing *head will pass pointer by value..Anty changes inside function
will not be reflected in claiing function
>
Sep 19 '06 #3

Wo*********@gmail.com wrote:
Something I don't really understand.

In http://maxnoy.com/interviews.html, Insert and Delete for linked list
are defined as:

int Insert(node **head, int data)

and

int Delete(node** head, int deleteMe)

I thought linked-list is always defined as something like struct node
*head. Why the author defined the linked-list as pointer to pointer?
And in the next question he defines head in Split() as node *head. This
is really confusing me
This is something which confused me too as a newbie coder, but once you
understand it, it's like achieving zen, it really is a very beautiful
part of the C language =)

When you call a function, the arguments are only *copies* of the
originals. So if function1 has a pointer, node *myhead, in it, then
what myhead really is is a very small chunk, call it chunk1, of memory
in which is stored an address to some other chunk of memory. If you
pass myhead to function2, this actually creates a NEW very small chunk
of memory, call it chunk2, copies the contents of chunk1 into chunk2,
and sends chunk2 to function2. At this point, function2 can go totally
crazy with chunk2, writing whatever crazy stuff it wants there, but
when we return to function1, the address in chunk1 will not have
changed. But suppose you have another function, function3, and you
*want* it to change the contents of chunk1 (as is the case with a list
insertion function). Then what you do is pass &myhead, the *address*
of chunk1. This creates a new small chunk, chunk3, on which is written
the address to chunk1, and passes that to function3. Now function3
knows where chunk1 is located, and therefore has the power to edit it.
Whereas function2 couldn't edit chunk1 if it wanted -- it doesn't know
where chunk1 is located!

Sep 19 '06 #4
"Nils O. Selåsdal" wrote:
Wo*********@gmail.com wrote:
>Something I don't really understand.

In http://maxnoy.com/interviews.html, Insert and Delete for
linked list are defined as:

int Insert(node **head, int data)

If you have a node *head; and want to delete the first node,
you need to change that pointer (to e.g. point to the next node).

Passing 'head' to a delete function, it can't change that pointer
only what it points to. So for the delete function to change that
pointer you must pass the address of that pointer.
It is simpler to define:

node *insert(node *list, int datum)

and then use it as

node *list = NULL /* i.e. empty */

...
while (something) list = insert(list, somedata);

which will normally result in inserting data at the head of a
list. It is a simple matter to reverse the list later, if
desirable. That way the implementation of insert can be:

node *insert(node *list, int datum)
{
node *temp;

if (NULL == (temp = malloc(sizeof *temp))
temp = list;
else {
temp->next = list;
temp->data = datum;
}
return temp
}

and there are no initialization effects to worry about.

--
"The most amazing achievement of the computer software industry
is its continuing cancellation of the steady and staggering
gains made by the computer hardware industry..." - Petroski

--
Posted via a free Usenet account from http://www.teranews.com

Sep 21 '06 #5

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

Similar topics

7
by: Robert Bralic | last post by:
Dear, I wreated a small program that make a linked list of factorials of numbers, I don't have expirience with templates so I will bee thankfull if anybody can make the same with...
5
by: Dream Catcher | last post by:
1. I don't know once the node is located, how to return that node. Should I return pointer to that node or should I return the struct of that node. 2. Also how to do the fn call in main for that...
3
by: Andrew Clark | last post by:
*** post for FREE via your newsreader at post.newsfeed.com *** it's been a while since i have poseted to this newsgroup, but for a long time i was not programming at all. but now that i am out of...
6
by: Steve Lambert | last post by:
Hi, I've knocked up a number of small routines to create and manipulate a linked list of any structure. If anyone could take a look at this code and give me their opinion and details of any...
57
by: Xarky | last post by:
Hi, I am writing a linked list in the following way. struct list { struct list *next; char *mybuff; };
10
by: Martin Jørgensen | last post by:
Hi, I got this piece of code, but I won't compile: #include <iostream> using namespace std; //////////////////////////////////////////////////////////////// struct link ...
4
by: arnuld | last post by:
This program follows from the section 6.5 of K&R2 where authors created a doubly-linked list using a binary-tree based approach. The only thing I have rewritten myself is the getword function. I am...
11
by: Scott Stark | last post by:
Hello, The code below represents a singly-linked list that accepts any type of object. You can see I'm represting the Data variable a System.Object. How would I update this code to use...
7
by: QiongZ | last post by:
Hi, I just recently started studying C++ and basically copied an example in the textbook into VS2008, but it doesn't compile. I tried to modify the code by eliminating all the templates then it...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.