I have this code:
[PHP]struct node* BuildWithDummyNode() {
struct node dummy; // Dummy node is temporarily the first node
struct node* tail = &dummy; // Start the tail at the dummy.
// Build the list on dummy.next (aka tail->next)
int i;
dummy.next = NULL;
for (i=1; i<6; i++) {
Push(&(tail->next), i);
tail = tail->next;
}
// The real result list is now in dummy.next
// dummy.next == {1, 2, 3, 4, 5};
return(dummy.next);
}[/PHP]
and this:
[PHP]/*
Takes a list and a data value.
Creates a new link with the given data and pushes
it onto the front of the list.
The list is not passed in by its head pointer.
Instead the list is passed in as a "reference" pointer
to the head pointer -- this allows us
to modify the caller's memory.
*/
void Push(struct node** headRef, int data) {
struct node* newNode = malloc(sizeof(struct node));
newNode->data = data;
newNode->next = *headRef; // The '*' to dereferences back to the real head
*headRef = newNode; // ditto
}
[/PHP]
In the first code line 8, we call the function push with the argument &(tail->next).Next in the push function i can't understand what is happening.
Push function:
line 11 : allocate memory for newnode
line12: we pass the data in the newnode data field
line 13 and 14 is that i don't understand
In line 13 we have newNode->next = *(tail->next); or (*tail)->next ???
and what is happening we set the newNode->next to show where ??(i think tail->next shows to the newNode)
Also line 14 i don;t understand.Please help me.Thanks
4 1318
I have this code:
[PHP]struct node* BuildWithDummyNode() {
struct node dummy; // Dummy node is temporarily the first node
struct node* tail = &dummy; // Start the tail at the dummy.
// Build the list on dummy.next (aka tail->next)
int i;
dummy.next = NULL;
for (i=1; i<6; i++) {
Push(&(tail->next), i);
tail = tail->next;
}
// The real result list is now in dummy.next
// dummy.next == {1, 2, 3, 4, 5};
return(dummy.next);
}[/PHP]
and this:
[PHP]/*
Takes a list and a data value.
Creates a new link with the given data and pushes
it onto the front of the list.
The list is not passed in by its head pointer.
Instead the list is passed in as a "reference" pointer
to the head pointer -- this allows us
to modify the caller's memory.
*/
void Push(struct node** headRef, int data) {
struct node* newNode = malloc(sizeof(struct node));
newNode->data = data;
newNode->next = *headRef; // The '*' to dereferences back to the real head
*headRef = newNode; // ditto
}
[/PHP]
In the first code line 8, we call the function push with the argument &(tail->next).Next in the push function i can't understand what is happening.
Push function:
line 11 : allocate memory for newnode
line12: we pass the data in the newnode data field
line 13 and 14 is that i don't understand
In line 13 we have newNode->next = *(tail->next); or (*tail)->next ???
and what is happening we set the newNode->next to show where ??(i think tail->next shows to the newNode)
Also line 14 i don;t understand.Please help me.Thanks
Hi,
Push takes two parameters,
1) Node 2) data
You pass two values in Push function i.e. &(tail->next) and i, it puches new value i in node tail->next.
*headRef will refer to &(tail->next). When you create new node, then line 13 will assign *headRef to new node's next node. And in line 14, now, newNode will be the headNode.
Regards
Hi,
Push takes two parameters,
1) Node 2) data
You pass two values in Push function i.e. &(tail->next) and i, it puches new value i in node tail->next.
*headRef will refer to &(tail->next). When you create new node, then line 13 will assign *headRef to new node's next node. And in line 14, now, newNode will be the headNode.
Regards
I don't understand.There is no headRef (in our example is &(tail->next) ).With this code, we put the new node in the end of list and not in the beggining, if this help.
I don't understand.There is no headRef (in our example is &(tail->next) ).With this code, we put the new node in the end of list and not in the beggining, if this help.
I have just explained you the lines that you don't understand and what the code reveals. You better read the concept of linked list. Google is the best friend.
Regards
Hi,
And in line 14, now, newNode will be the headNode.
As you say newNode will be the headNode but the newNode should be the last element from the list according to the book ,where i find the code
Sign in to post your reply or Sign up for a free account.
Similar topics
by: C++fan |
last post by:
Suppose that I define the following class:
class example_class{
public:
example_class();
void funtion_1();
void function_2();
protected:
|
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...
|
by: Kent |
last post by:
Hi!
I want to store data (of enemys in a game) as a linked list, each node will
look something like the following:
struct node
{
double x,y; // x and y position coordinates
struct enemy...
|
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...
|
by: Eugen J. Sobchenko |
last post by:
Hi!
I'm writing function which swaps two arbitrary elements
of double-linked list. References to the next element of list
must be unique or NULL (even during swap procedure), the same condition...
|
by: joshd |
last post by:
Hello,
Im sorry if this question has been asked before, but I did search
before posting and couldnt find an answer to my problem. I have two
classes each with corresponding linked lists, list1...
|
by: Joerg Schoen |
last post by:
Hi folks!
Everyone knows how to sort arrays (e. g. quicksort, heapsort etc.)
For linked lists, mergesort is the typical choice.
While I was looking for a optimized implementation of mergesort...
|
by: theeverdead |
last post by:
Ok I have a file in it is a record of a persons first and last name. Format is like:
Trevor Johnson
Kevin Smith
Allan Harris
I need to read that file into program and then turn it into a linked...
|
by: Atos |
last post by:
SINGLE-LINKED LIST
Let's start with the simplest kind of linked list : the single-linked list which only has one link per node. That node except from the data it contains, which might be...
|
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...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
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: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
|
by: ryjfgjl |
last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
|
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,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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$) {
}
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
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
| |