I was wondering was *& means.
Context:
A function is implemented as follows:
void headInsert( Node*& head, int info )
{
Node* temp = new Node(info);
temp->link = head;
head = temp;
}
WHy not just use Node& ?
Thanks
Node*& means "a reference to a pointer to Node" while Node& means "a reference to Node".
Why not just use Node& ?
Because the headInsert function needs to change what the head is pointing to.
You may want to look at the specific call, where reference pointers reveal their use:
Node* pHead = somewhere;
headInsert(pHead, info);
// pHead does now point to the newly allocated node, generated inside headInser,
// by new Node(info), but NOT to 'somewhere'
Let me comment on your example, maybe that makes it more clear:
void headInsert( Node*& head, int info )
{
Node* temp = new Node(info); // generate a new head, the future head
temp->link = head; // let the former head be a member/child of the new head
head = temp; // 'overwrite' the former head pointer outside of the call by the new head
}
Related
The Definition of the structure is as follows.
//Structure of the linked list node is as follows:
struct Node {
int data;
struct Node* next;
Node(int x) {
data = x;
next = NULL;
}
};
I have to complete this function which I have completed this way. I am trying to create a Node using the newData parameter passed in the function definition. But it shows the error which I have attached below.
// function inserts the data in front of the list
Node* insertAtBegining(Node *head, int newData) {
//Your code here
struct Node* newNode(newData);
struct Node* temp;
temp=head;
head=newNode;
newNode->next=temp;
}
I get this error while I am create a node by passing newData as parameter to struct Node *newNode(newData);
In function Node* insertAtBegining(Node*, int):
prog.cpp:67:32: error: invalid conversion from int to Node* [-fpermissive]
struct Node *newNode(newData);
Thank You for your help.
Your constructor returns a Node, not a Node*, so when you try to initialize newNode, the compiler thinks you're trying to create a pointer using an int. Instead you should be expecting your constructor to give you a Node:
Node newNode(newData);
Your insertAtBegining() implementation needs to create a new Node object. You aren't doing that. Right after your teacher's Your code here comment, your attempt has defined a "Node pointer" variable (whose type is Node*), but your attempt hasn't initialized that to any Node object (data that would have been passed to an object's constructor isn't the same as the object itself).
Also, you don't need to keep repeating struct that way that us old guys used to do with old-fashioned C code.
Lastly, you seem to also want to return the list's new head node back to the function's caller, but are unclear how you want to achieve that. There are two ways. The way that your code seems to be leaning towards is returning the new head in the same head parameter. In that case, it should look like this:
void insertAtBegining(Node** head, int newData)
{
//Your code here
Node* newNode = new Node(newData);
Node* temp;
temp = *head;
*head = newNode;
newNode->next = temp;
}
(The head parameter could also have been a "reference to a Node*", instead of this "pointer to a Node*", by making appropriate changes to the code.)
The second option (which maintains your teacher's recommended function signature) is to return the new head via the function's return value:
Node* insertAtBegining(Node* head, int newData)
{
//Your code here
Node* newNode = new Node(newData);
newNode->next = head;
return newNode;
}
My mind is confused at the moment:
struct Node {
int data;
struct Node *next;
}
void Print(Node *head) {
}
This is a code snippet from HackerRank. While this is easy, I just started wondering something: If I modify the head in the Print function, does it modify the original head in the main as well, or is it just the local variable head that is modified?
You passed in a pointer by value, if you modify that pointer then it will not affect the original.
However if you modify what is pointed to by that pointer then it will affect the original.
For instance head = nullptr; would not, while head->data = 1; would.
Also note that any recursion you do will similarly change the original data, for instance an algorithm to add to the end of the list:
Node* previous = head
Node* current = head->next;
while (current != nullptr)
{
previous = current;
current = previous->next;
}
previous->next = new Node(); //However you create one.
Since it uses head->next and eventually modifies the result it will modify the original list.
Could someone please tell me why are pointers head and tail different when exiting function reverse?
struct elem{
int val;
elem* prev;
elem* next;
...
};
void print(elem* head,elem* tail){...}
void insertAtEnd(elem* e,elem* tail){...}
void reverse(elem* head,elem* tail){
elem* headref = head;
elem* temp = head;
while(temp != NULL){
elem* t = temp->prev;
temp->prev = temp->next;
temp->next = t;
temp = temp->prev;
}
head = tail;
tail = headref;
print(head,tail);
}
int main(){
elem* head = new elem();
elem* tail = new elem();
...
print(head,tail);
reverse(head,tail);
print(head,tail);
return 0;
}
print() inside function reverse works fine. Next print (just before return 0 in main) causes segmentation fault (head->next points to NULL).
With void reverse(elem* head,elem* tail), you don't modify pointer (you may modify content).
You probably mean
void reverse(elem*& head, elem*& tail)
to modify head and tail.
Calling reverse doesn't change head and tail because they are passed by value (reverse only modifies its private copies). If you change the declaration of reverse to
void reverse(elem *&head, elem *&tail)
It should work.
The pointers in main() themselves don't change after calling reverse(), because you pass them to reverse() by value, and the code in the function only modifies its own copies of the pointers. However, the contents of the elem objects they point to have been changed by the reverse() function.
That is, head in main() still points to the same elem object it used to point to before, but now that elem object is the tail of the list (because you changed its contents in the reverse() function and now its next member is nullptr). Similarly, tail in main() points to an elem that is now the head of the list.
Without analysing your code in detail, When you are reversing you are passing pointers to your elements, so the one called head is now the tail and the one called tail is now the head.
So after you will need to print( tail, head )
Your print function obviously does not check for null that is, it will cause an access violation if next is nullptr before it reaches your tail. And it will be because head is the tail so its next will indeed be nullptr.
If you actually want the reverse function to change the pointers themselves as well as what they point to, so you get a reversed list with the names head and tail swapped, pass them by reference.
Your exchange of pointers inside reverse() is local. It will be discarded once you leave the function scope. To achieve the exchange, yo0u have to use double pointers:
void reverse(elem** head, elem** tail){
elem* headref = *head;
elem* temp = *head;
while(temp != NULL){
elem* t = temp->prev;
temp->prev = temp->next;
temp->next = t;
temp = temp->prev;
}
*head = *tail;
*tail = headref;
print(*head,*tail);
}
And pass addresses of head and tail:
reverse(&head, &tail);
P.S. Sorry for possible bugs - I didn't test the code
I'm working on a project and I was given this function to complete
void addToEnd(node*& head, string newVal)
Effect: adds new node to tail end of list
Precondition: head is a pointer to first node in the list (list MAY be empty)
Postcondition: list contains one more node
My question is what is the string newVal for?
The value_type of this class is of type DOUBLE so I'm confused what string newVal is for. So I can't set the newVal in the node because it is of two different types.
This is what I have so far. I'm not sure if im going in the right direction.
node *temp = new node;
temp = head;
while(temp->link() != NULL){
temp = temp->link();
}
head->set_link(temp);
I'm not even sure where to use the string in this block of code.
link() returns the member variable node* link_field
set_link() sets the new link to the link_field
Well, we're guessing that they somehow expect you to turn a string into a double with a function like std::stod.
As for your list manipulation code, there's a few problems:
node *temp = new node;
temp = head;
This creates a new node, puts its pointer in temp, then immediately overwrites temp with head, losing (leaking) the new node. Don't do that.
while(temp->link() != NULL){
temp = temp->link();
}
This is close, but might not work. The problem is that you need to keep track of the real node pointer, not a copy.
Normally, in a linked list API using pointers instead of references, the "add node" function looks like:
void addToEnd(node** head, string newVal)
{
while(*head)
head = &((*head)->next);
*head = new node;
(*head)->value = newVal;
(*head)->next = 0;
}
Note that if the list is empty, the passed-in head pointer is altered to point to the new node. If the list is not empty, the last next pointer is altered instead.
The API you're given (i.e. the link and set_link methods) doesn't allow this, because the head pointer is not a node and those functions require a node. So you've got to do it a little differently, namely you have to handle the empty list case separately.
void addToEnd(node*& head, string newVal)
{
// Create the node.
node* newNode = new node;
newNode->value = std::stod(newVal);
newNode->set_link(0);
if(!head) // Empty list?
{
head = newNode;
return;
}
// Find last node.
node* item = head;
while(item->link())
item = item->link();
item->set_link(newNode);
}
Insert is a method which appends an item to the end of my linked list.
Can't figure out how to code for the case where Node is null, and I just want to add to it.
struct Node{
int data;
Node *next;
Node(int data):data(data),next(NULL){}
void insert(int data){
if (this==NULL){
this=new Node(data); // compiler is complaining here.
// how would I go about setting the value of this (which is presently NULL) to a new Node?
}
}
}
you can not assign a value to this pointer which is a special keyword and should always point to a valid block of memory. by looking at your usage, could you be trying to mean this:
void insert(int data){
if (!next){
next = new Node(data);
}
Something like this:
void insert(int data)
{
Node* newNode = new Node(data);
if (next!=NULL)
newNode->next = next;
next = newNode;
}
You cannot assign directly to 'this'; what you need to consider is how to represent an empty list, most likely by:
Node* head = 0;
So you add the first node by
head = new Node(data);