'control pointers to structures and put it in a function?
I have a data structure task, where I must put for each node a following, a previous and a data of type string.
struct node {
string data;
struct node* prev;
struct node* next;
};
The problem is that I have to make a circular doubly linked list, which I have no problem creating as follows:
#include <iostream>
#include <stdlib.h>
using namespace std;
struct node {
string data;
struct node* prev;
struct node* next;
};
struct node* makeNode(string data)
{
struct node* newnode = new node();
newnode->prev = newnode;
newnode->data = data;
newnode->next = newnode;
return newnode;
}
void Print(struct node* head)
{
cout << "node->prev: " << head->prev << endl;
cout << "node->data: " << head->data << endl;
cout << "node->next: " << head->next << endl;
cout << endl;
}
int main()
{
struct node* head = makeNode("Juan");
struct node* newnode = makeNode("Carlos");
struct node* temp = head->next;
newnode->next = head;
head->next = newnode;
newnode->prev = head->next;
Print(head);
head = newnode;
Print(head);
return 0;
}
Example:
node->prev: 0x600000018240
node->data: Juan
node->next: 0x600000018270
node->prev: 0x600000018270
node->data: Carlos
node->next: 0x600000018240
where it is true that the 'next' of the last node is the address of the 'previous' of the first node.
but at the moment of putting it in a function, everything thunders as always in the functions I am not so skilled.
struct node* addEnd(struct node* head, string data)
{
struct node* newnode = makeNode();
if(head == NULL)
{
head = newnode;
return newnode;
}
else
{
struct node* temp = head->next;
newnode->next = head;
head->next = newnode;
newnode->prev = head->next;
return head;
}
}
any blessed soul that can help me please :( I've been trying to get it to work for two weeks, but the most I've achieved is without using functions.
Solution 1:[1]
Okay, a few things. This is your code. It's longer than it needs to be, and it has problems.
The biggest problem: you don't give newnode -> data the passed in data.
struct node* addEnd(struct node* head, string data)
{
struct node* newnode = makeNode();
if(head == NULL)
{
head = newnode;
return newnode;
}
else
{
struct node* temp = head->next;
newnode->next = head;
head->next = newnode;
newnode->prev = head->next;
return head;
}
}
So you'll want to start with this. Note that we're not program C, so you don't need to say struct node. This should work.
node * newnode = makenode();
newnode->data = data;
Let's also back up a bit. I'm going to rewrite your node a little.
struct node {
string data;
node * prev = nullptr;
node * next = nullptr;
};
On a stylistic note: please consider whitespace around operators. You'll see I added them before the *. As you work with older programmers with old, tired eyes, they'll appreciate a little bit more readability.
Now, for the rest of it. This is C++, not C. Please use nullptr. You said this is a circular list. Which means head->prev is the tail of the list, so if we want, we can insert after the tail, or we can insert at the head. Note that you were doing weird things with the pointers.
if (head == nullptr) {
head = newnode;
}
else {
newnode->next = head;
newnode->prev = head->prev;
head->prev = newnode;
newnode->prev->next = newnode;
// Do this last step if you want to insert at the
// front. Exclude this last step if you want to push
// at the back.
head = newnode;
}
return head;
Here's what I'm going to recommend you do... Write out on paper what you have. You just need a few boxes in a row. Head points to one of the middle boxes. That box points to next to the right and prev to the left. They all do that. The ones at the end circle around.
Now, plop in a new box and think about what has to happen to the pointers to make it work.
I literally do this when I get confused.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Joseph Larson |
