Write a function to insert a node to the head of the linked list. The function takes two arguments: a pointer to the first node in the double linked list and a string value. It should create a new node with the given value to the head of the double linked list.
This is the question I need to answer. I'm not looking for the answer I'm just unsure how to create a node in C++.
That should be what you wanted.
struct node{
node * next;
std::string value;
};
node * addToTheFront(node * front, std::string value){
node * temp = new node;
temp->next = front;
temp->value = value;
return temp;
}
Related
Let's make a list
struct node
{
node *next=nullptr;
int value=0;
};
Why do we start a list with a pointer node *head=nullptr, but not with an object node head? Is such an implementation faster than creating each node as an object whose fields are a value and a pointer to the next object? I don't understand how this implementation works.
I added function that adds new node to the front
void addFront(node *&head, int value)
{
node *nodeToAdd=new node;
nodeToAdd->value=value;
nodeToAdd->next=head;
head=nodeToAdd;
}
and another one that shows the list.
addFront(head1, 2);
showList(head1);
node *head2=new node;
addFront(head2, 2);
showList(head2);
Why first showList display 2 and the second one 2 0? The only difference is in head1 and head2 declaration.
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;
}
I'm creating a programme that needs to read data from a spreadsheet in CSV form and assigns it to a doubly linked list in C++.I have created a singly linked list but I'm at a loss as to how to use this idea to make a doubly linked list. I understand you require a previous pointer but I am unsure as to actually implementing the code.
code for my singly linked list:
to add to the list:
if (!m_head)
{
m_head = new Node(name, reference,latitude,longitude);
}
else
{
Node *current = m_head;
while (current->getNext() != 0)
{
current = current->getNext();
}
current->setNext(new Node(name, reference,latitude,longitude));
}
please note: Node is a separate class to store data about the node e.g. name.
Each List Node must a have a pointer to the previous and to the next List Node.
The List is then the container of List Nodes , linked together as a chain.
struct ListNode;
typedef struct ListNode {
struct ListNode *next;
struct ListNode *prev;
void *value;
} ListNode;
typedef struct List {
int count;
ListNode *first;
ListNode *last;
} List;
You need then to implement the methods push and pop accordingly.
The tricky part is the remove method. Store the two pointers of the previous and next Node that you want to delete, and then assign them accordingly to the previous and following node:
ListNode *after = node->next;
ListNode *before = node->prev;
after->prev = before;
before->next = after;
This article may help you, full code and explanation
http://c.learncodethehardway.org/book/ex32.html
So I'm trying to write a function that returns a newly made Huffman tree, the function finds the two smallest frequencies in a list then removes them (remove_smallest) and then makes them the children of a new treeNode (root) and inserts it into the list again (in the style of Huffman trees) and does it all over until there is 1 element left in the list. My program seems to be crashing around the line just after the second call to the remove_smallest function. The remove_smallest function returns the smallest treeNode in the list It should keep going until there is one element left. Where am I going wrong? Any why am I getting a segmentation fault? As I mentioned it seems to be crashing right after the two calls to remove_smallest.
Am I dereferencing a pointer that's pointing at random memory? or setting an incorrect pointer to something?
Any help is appreciated. Thank you.
Code:
typedef TreeNode* Element;
typedef Frequency* TreeElement;
struct TreeNode {
TreeElement data; //stores a pointer to the data in this tree node
TreeNode *left; //reference to the left child tree
TreeNode *right; //reference to the right child tree
};
struct ListNode {
Element data; // stores a pointer to data in node
ListNode *next; // reference to next node in list
};
struct List {
ListNode *head; // reference to the first node in the list
int numElements; // the number of nodes in the list
};
struct HuffmanTree {
TreeNode *root;
};
struct FrequencyList {
List *freqs;
};
struct Frequency {
char data; // the character being represented
int count; // the number of occurrences of the character
};
HuffmanTree *createHuffmanTree(FrequencyList *frequencies) {
List * newList = new List;
newList = frequencies->freqs;
TreeNode * newTree1;
TreeNode * newTree2;
TreeNode * root = new TreeNode;
while (frequencies->freqs->numElements != 1) {
newTree1 = remove_smallest(frequencies); // removes and returns smallest treeNode from list
newTree2 = remove_smallest(frequencies);
root->data->count = newTree1->data->count + newTree2->data->count;
root->left = newTree1;
root->right = newTree2;
insert(newList, root); // inserts back into list
}
HuffmanTree * newHuffmanTree = new HuffmanTree;
newHuffmanTree->root = root;
return newHuffmanTree;
}
Segmentation Fault is caused by trying to access memory that doesn't exist. This is a very common error for people that are new to C/C++. I would double check all your pointers and make sure you are trying to access the information that you intended (address/value).
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);
}