473,785 Members | 2,369 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 5620
Wo*********@gma il.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*********@gma il.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*********@gma il.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*********@gma il.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
2328
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 templates(change my small program), becouse I don't understand a templates. Program is commpilled with Visual C++ 6.0. (Escouse mee first version of program was incorect becouse variable :"i" was seted to 1,
5
859
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 LOCATE subroutine that returns a node???? Any help would be appreciated. Thanks
3
22075
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 college and facing the prospect of getting a real job, i need to get back into the game. anyway, i don't know if some of you know the stanford CS library. i took the problem set a while back and have recently rediscovered it adn have been trying...
6
4603
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 potential pitfalls I'd be extremely grateful. Cheers Steve
57
4305
by: Xarky | last post by:
Hi, I am writing a linked list in the following way. struct list { struct list *next; char *mybuff; };
10
2017
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 //one element of list { int data; //data item
4
2941
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 using it because there are many concepts involved in this program that I want to understand like: 1.) degree of efficiency of this program as compared to sort using hash-table based approach. 2.) Keeping the program well structured so...
11
2554
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 generics instead of System.Object. I want the code in Form1_Load to remain exactly the same, but in the background I want to use generics. I'm trying to get a better understanding of how it works and I'm a little stuck.
7
5773
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 compiled no problem. But I can't find the what the problem is with templates? Please help. The main is in test-linked-list.cpp. There are two template classes. One is List1, the other one is ListNode. The codes are below: // test-linked-list.cpp :...
0
9645
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
9481
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
10155
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10095
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9954
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
8979
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...
0
6741
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5383
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3656
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.