473,762 Members | 8,011 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Question about usage of pointer in trees, linked list (The double indirection)

When implementing Linked list, stack, or trees, we always use pointers
to 'link' the nodes.
And every node is always defined as:
struct node
{
type data; //data this node contains
...
node * nPtr; //the next node's pointer
}

And we also define operations as insert, delete, etc. on the data
structure.
MY QUESTION IS:
why we always pass node**(pointer to the pointer to the node) as
argument to these operations? Why we can not just use the node *?

Thank you!

Ji

Mar 10 '07 #1
9 2844
william wrote:
When implementing Linked list, stack, or trees, we always use pointers
to 'link' the nodes.
And every node is always defined as:
struct node
{
type data; //data this node contains
...
node * nPtr; //the next node's pointer
}

And we also define operations as insert, delete, etc. on the data
structure.
MY QUESTION IS:
why we always pass node**(pointer to the pointer to the node) as
argument to these operations? Why we can not just use the node *?
So the value of the pointer can be changed, as well as the value the
pointer point to.

--
Ian Collins.
Mar 10 '07 #2
william wrote:
When implementing Linked list, stack, or trees, we always use pointers
to 'link' the nodes.
And every node is always defined as:
struct node
{
type data; //data this node contains
...
node * nPtr; //the next node's pointer
}

And we also define operations as insert, delete, etc. on the data
structure.
MY QUESTION IS:
why we always pass node**(pointer to the pointer to the node) as
argument to these operations? Why we can not just use the node *?

Thank you!

Ji
In a good implementation of a linked list, the nodes are completely
hidden. You would never pass node* or node** to any operations.

What you are looking at is a bad implementation of a linked list. I
can't tell you why it uses node** or node* without seeing the code. It
shouldn't use either.

john
Mar 10 '07 #3
On Mar 10, 3:48 pm, John Harrison <john_androni.. .@hotmail.comwr ote:
william wrote:
When implementing Linked list, stack, or trees, we always use pointers
to 'link' the nodes.
And every node is always defined as:
struct node
{
type data; //data this node contains
...
node * nPtr; //the next node's pointer
}
And we also define operations as insert, delete, etc. on the data
structure.
MY QUESTION IS:
why we always pass node**(pointer to the pointer to the node) as
argument to these operations? Why we can not just use the node *?
Thank you!
Ji

In a good implementation of a linked list, the nodes are completely
hidden. You would never pass node* or node** to any operations.
What does it mean by nodes are completely hidden(as being refered to,
nodes means the pointers to the actual struct block in memory, or the
structs themselves?)
>
What you are looking at is a bad implementation of a linked list. I
can't tell you why it uses node** or node* without seeing the code. It
shouldn't use either.

john
I paste the sample code below, which came from the book 'C how to
program'. John, could you please offer me a good example of
implementation of linked list? Thank you.

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

struct listNode
{
char data;
struct listNode *nextPtr;
};

typedef struct listNode ListNode;
typedef ListNode *ListNodePtr;

void insert(ListNode Ptr *, char);
char delete(ListNode Ptr *, char);
int isEmpty(ListNod ePtr);
void printList(ListN odePtr);
void instructions(vo id);

int main()
{
ListNodePtr startPtr=NULL;
int choice;
char item;

instructions(); //display the menu;
printf(">");
scanf("%d",&cho ice);

while(choice!=3 )
{
switch (choice)
{
case 1:
printf("Enter a character: ");
scanf("\n%c",&i tem);
insert(&startPt r,item);
printList(start Ptr);
break;

case 2:
if(!isEmpty(sta rtPtr))
{
printf("Enter character to be deleted: ");
scanf("\n%c",&i tem);

if(delete(&star tPtr,item))
{
printf("%c deleted.\n",ite m);
printList(start Ptr);
}
else
printf("%c not found.\n\n",ite m);
}
else
printf("List is empty.\n\n");
break;
default:
printf("Invalid choice.\n\n");
instructions();
break;
}
printf(">");
scanf("%d",&cho ice);
}
printf("End of run.\n");
return 0;
}

void instructions(vo id)
{
printf("Enter your choice:\n");
printf("1 to insert an element into the list.\n"
"2 to delete an element from the list.\n"
"3 to exit the program.\n");
}

void insert(ListNode Ptr *sPtr, char value)
{
ListNodePtr newPtr, previousPtr, currentPtr;

newPtr=malloc(s izeof(ListNode) );
if(newPtr !=NULL)
{
newPtr->data=value;
newPtr->nextPtr=NULL ;

//set the searching index(previousP tr and
//currentPtr)to the start of the list.
previousPtr=NUL L;
currentPtr=*sPt r;

while(currentPt r !=NULL && value>currentPt r->data)
{
previousPtr=cur rentPtr;
currentPtr=curr entPtr->nextPtr;
} //keep going

if(previousPtr= =NULL)
{
newPtr->nextPtr=*sPt r;
*sPtr=newPtr;//insert the node at the beginning of the list
}
else
{
previousPtr->nextPtr=newPtr ;
newPtr->nextPtr=curren tPtr;
}
}
else
{
printf("%c not inserted. No memory available.\n",v alue);
}
}

char delete(ListNode Ptr *sPtr, char value)
{
ListNodePtr previousPtr,cur rentPtr,tempPtr ;

if (value==(*sPtr)->data)
{
tempPtr=*sPtr;
*sPtr=(*sPtr)->nextPtr;
free(tempPtr);
return value;
}
else
{
previousPtr=*sP tr;
currentPtr=(*sP tr)->nextPtr;//setup the cursor at the beginning

//when the cursor didn't reach the tail and cursor didn't find the
//specified value, the cursor keep going along the list
while(currentPt r !=NULL && currentPtr->data !=value)
{
previousPtr=cur rentPtr;
currentPtr=curr entPtr->nextPtr;//move on to next node
}

if(currentPtr !=NULL)//if not reaching the tail
{
tempPtr=current Ptr;
previousPtr->nextPtr=curren tPtr->nextPtr;
free(tempPtr);
return value;
}
}
return '\0';//return null char;
}

int isEmpty(ListNod ePtr sPtr)
{
return sPtr==NULL;
}

void printList(ListN odePtr currentPtr)
{
if(currentPtr== NULL)
{
printf("List is Empty.\n\n");
}
else
{
printf("The list is:\n");
while(currentPt r !=NULL)
{
printf("%c--",currentPt r->data);
currentPtr=curr entPtr->nextPtr;
}
printf("NULL\n\ n");
}

}
*************** *************** *************** *************** ****
Mar 10 '07 #4
On Mar 10, 4:44 pm, "william" <william.m...@g mail.comwrote:
On Mar 10, 3:48 pm, John Harrison <john_androni.. .@hotmail.comwr ote:
william wrote:
When implementing Linked list, stack, or trees, we always use pointers
to 'link' the nodes.
And every node is always defined as:
struct node
{
type data; //data this node contains
...
node * nPtr; //the next node's pointer
}
And we also define operations as insert, delete, etc. on the data
structure.
MY QUESTION IS:
why we always pass node**(pointer to the pointer to the node) as
argument to these operations? Why we can not just use the node *?
Thank you!
Ji
In a good implementation of a linked list, the nodes are completely
hidden. You would never pass node* or node** to any operations.

What does it mean by nodes are completely hidden(as being refered to,
nodes means the pointers to the actual struct block in memory, or the
structs themselves?)
What you are looking at is a bad implementation of a linked list. I
can't tell you why it uses node** or node* without seeing the code. It
shouldn't use either.
john

I paste the sample code below, which came from the book 'C how to
program'. John, could you please offer me a good example of
implementation of linked list? Thank you.

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

struct listNode
{
char data;
struct listNode *nextPtr;

};

typedef struct listNode ListNode;
typedef ListNode *ListNodePtr;

void insert(ListNode Ptr *, char);
char delete(ListNode Ptr *, char);
int isEmpty(ListNod ePtr);
void printList(ListN odePtr);
void instructions(vo id);

int main()
{
ListNodePtr startPtr=NULL;
int choice;
char item;

instructions(); //display the menu;
printf(">");
scanf("%d",&cho ice);

while(choice!=3 )
{
switch (choice)
{
case 1:
printf("Enter a character: ");
scanf("\n%c",&i tem);
insert(&startPt r,item);
printList(start Ptr);
break;

case 2:
if(!isEmpty(sta rtPtr))
{
printf("Enter character to be deleted: ");
scanf("\n%c",&i tem);

if(delete(&star tPtr,item))
{
printf("%c deleted.\n",ite m);
printList(start Ptr);
}
else
printf("%c not found.\n\n",ite m);
}
else
printf("List is empty.\n\n");
break;
default:
printf("Invalid choice.\n\n");
instructions();
break;
}
printf(">");
scanf("%d",&cho ice);
}
printf("End of run.\n");
return 0;

}

void instructions(vo id)
{
printf("Enter your choice:\n");
printf("1 to insert an element into the list.\n"
"2 to delete an element from the list.\n"
"3 to exit the program.\n");

}

void insert(ListNode Ptr *sPtr, char value)
{
ListNodePtr newPtr, previousPtr, currentPtr;

newPtr=malloc(s izeof(ListNode) );
if(newPtr !=NULL)
{
newPtr->data=value;
newPtr->nextPtr=NULL ;

//set the searching index(previousP tr and
//currentPtr)to the start of the list.
previousPtr=NUL L;
currentPtr=*sPt r;

while(currentPt r !=NULL && value>currentPt r->data)
{
previousPtr=cur rentPtr;
currentPtr=curr entPtr->nextPtr;
} //keep going

if(previousPtr= =NULL)
{
newPtr->nextPtr=*sPt r;
*sPtr=newPtr;//insert the node at the beginning of the list
}
else
{
previousPtr->nextPtr=newPtr ;
newPtr->nextPtr=curren tPtr;
}
}
else
{
printf("%c not inserted. No memory available.\n",v alue);
}

}

char delete(ListNode Ptr *sPtr, char value)
{
ListNodePtr previousPtr,cur rentPtr,tempPtr ;

if (value==(*sPtr)->data)
{
tempPtr=*sPtr;
*sPtr=(*sPtr)->nextPtr;
free(tempPtr);
return value;
}
else
{
previousPtr=*sP tr;
currentPtr=(*sP tr)->nextPtr;//setup the cursor at the beginning

//when the cursor didn't reach the tail and cursor didn't find the
//specified value, the cursor keep going along the list
while(currentPt r !=NULL && currentPtr->data !=value)
{
previousPtr=cur rentPtr;
currentPtr=curr entPtr->nextPtr;//move on to next node
}

if(currentPtr !=NULL)//if not reaching the tail
{
tempPtr=current Ptr;
previousPtr->nextPtr=curren tPtr->nextPtr;
free(tempPtr);
return value;
}
}
return '\0';//return null char;

}

int isEmpty(ListNod ePtr sPtr)
{
return sPtr==NULL;

}

void printList(ListN odePtr currentPtr)
{
if(currentPtr== NULL)
{
printf("List is Empty.\n\n");
}
else
{
printf("The list is:\n");
while(currentPt r !=NULL)
{
printf("%c--",currentPt r->data);
currentPtr=curr entPtr->nextPtr;
}
printf("NULL\n\ n");
}

}

*************** *************** *************** *************** ****
Above is all the code.

here is the segment that confuses me:

in 'main':
ListNodePtr startPtr=NULL;
....
....
insert(&startPt r,item);
....
And the prototype of 'insert' is:
void insert(ListNode Ptr *, char);
// you can find the content in the code segment I posted just now.

So, is there any CONVENTION that how programmers implement different
data structure:
LINKED LIST
TREE
QUEQUE
STACK
and define operations on those data structure?

Where can I find a good resource talking about this?

Mar 10 '07 #5
william wrote:
>
Above is all the code.

here is the segment that confuses me:

in 'main':
ListNodePtr startPtr=NULL;
....
....
insert(&startPt r,item);
....
And the prototype of 'insert' is:
void insert(ListNode Ptr *, char);
What part of this confuses you? If insert didn't take the address of
startPtr, how could it change it?

--
Ian Collins.
Mar 10 '07 #6
>
>
I paste the sample code below, which came from the book 'C how to
program'. John, could you please offer me a good example of
implementation of linked list? Thank you.
I didn't realise you were programming in C. C and C++ are different
languages, what is good in C not the same as what is good in C++.

The code below is probably fine in C, but it wouldn't be good C++. In
C++ it's much easier to seperate the interface from the implementation.

If you haven't got your answer already (Ian has given you the answer)
you should probably ask questions about this code on comp.lang.c, it's C
not C++.

john
Mar 10 '07 #7
william wrote:
[snip]
So, is there any CONVENTION that how programmers implement different
data structure:
LINKED LIST
TREE
QUEQUE
STACK
and define operations on those data structure?
Yes, there is: you don't implement them yourself, you use the standard
library instead. It provides (among other things):

std::list
std::queue
std::stack

and for problems involving search trees:

std::set, std::multiset
std::map, std::multimap

Where can I find a good resource talking about this?
Any introduction to the standard library should cover the standard container
classes and adaptors.
Best

Kai-Uwe Bux
Mar 10 '07 #8
On Mar 10, 5:00 pm, Ian Collins <ian-n...@hotmail.co mwrote:
william wrote:
Above is all the code.
here is the segment that confuses me:
in 'main':
ListNodePtr startPtr=NULL;
....
....
insert(&startPt r,item);
....
And the prototype of 'insert' is:
void insert(ListNode Ptr *, char);

What part of this confuses you? If insert didn't take the address of
startPtr, how could it change it?
Thank you Ian, I understand it now. The whole structure is a
pointer(to the starting struct), so we need to change it if we insert
a new node at the beginning, right?
--
Ian Collins.

Mar 10 '07 #9
On Mar 10, 5:09 pm, Kai-Uwe Bux <jkherci...@gmx .netwrote:
william wrote:

[snip]
So, is there any CONVENTION that how programmers implement different
data structure:
LINKED LIST
TREE
QUEQUE
STACK
and define operations on those data structure?

Yes, there is: you don't implement them yourself, you use the standard
library instead. It provides (among other things):

std::list
std::queue
std::stack

and for problems involving search trees:

std::set, std::multiset
std::map, std::multimap
Where can I find a good resource talking about this?

Any introduction to the standard library should cover the standard container
classes and adaptors.

Best

Kai-Uwe Bux
Thank you for your reply! I got it.

regards

Ji

Mar 10 '07 #10

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

Similar topics

9
3505
by: kazio | last post by:
Hello, So, I need to have double linked, circular list (last element point to the first one and first one points to the last one). I thought maybe I could use list container from STL, but unfortunately probably not. I checked it, and when I increase (++) iterator pointing to the last element the program crashes. I know the standard says, this is a linear list (with beginning and the end), but I completely don't understand why they...
5
6060
by: John N. | last post by:
Hi All, Here I have a linked list each containing a char and is double linked. Then I have a pointer to an item in that list which is the current insertion point. In this funtion, the user hits the right and left keys to move this insertion point (cursor) Here is the problem:
11
3189
by: theshowmecanuck | last post by:
As a matter of academic interest only, is there a way to programmatically list the 'c' data types? I am not looking for detail, just if it is possible, and what function could be used to accomplish it. For example: int main void() { while there are more data types { print next data type; }
4
3604
by: JS | last post by:
I have a file called test.c. There I create a pointer to a pcb struct: struct pcb {   void *(*start_routine) (void *);   void *arg;   jmp_buf state;   int    stack; };   struct pcb *pcb_pointer;
204
13075
by: Alexei A. Frounze | last post by:
Hi all, I have a question regarding the gcc behavior (gcc version 3.3.4). On the following test program it emits a warning: #include <stdio.h> int aInt2 = {0,1,2,4,9,16}; int aInt3 = {0,1,2,4,9};
10
1304
by: Bonj | last post by:
I almost understand TSTs, to the point where I just need to know the answer to this: When making a TST (in C++) that will have as its leaf nodes words that make up SQL language and an categorising identifier for each one, and each layer of the tree will represent comparison of a further letter within the search string, what will happen when a particular node is a leaf node itself, but also has leaf nodes of its own? i.e. specifically, as...
7
1869
by: | last post by:
Hi, From 11.11 here, I know that member objects get their dtor's called autmatically: http://www.parashift.com/c++-faq-lite/dtors.html#faq-11.11 What if I have a pointer to a member object? e.g. (in C++)
24
1958
by: Kavya | last post by:
int main (){ int a={{1,2,3},{4,5,6}}; int (*ptr)=a; /* This should be fine and give 3 as output*/ printf("%d\n",(*ptr)); ++ptr;
8
1625
by: =?ISO-8859-1?Q?Konrad_M=FChler?= | last post by:
Hi, I've a list of objects. I iterate the list and read the value of each object for many operation like: x = myList.value1 + myList.value2 etc. My question: Is it efficient to always use myList or should I get the pointer to
0
9378
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
10137
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
9989
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
9927
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
6640
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
5268
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...
0
5405
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3914
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3510
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.