Linked list, add node to end

flowinwind

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

Mike DeSimone

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);
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Add end node to linked list

From Dev

Adding a node to the end of a linked list

From Dev

Inserting a node at the end of linked list

From Dev

Inserting a node at the end of a linked list

From Dev

Inserting a node in the end of a linked list

From Dev

Failed to add a node to linked list

From Dev

adding a node at the end of a Linked List (java)

From Dev

Deletion of node at end of linked list C++

From Dev

Removing the end node from a linked list

From Dev

Inserting a node at the end of linked list, .exe crashes

From Dev

linked list moving node to the end C

From Dev

Inserting a node at the end of a doubly linked list

From Dev

adding a node at the end of a Linked List (java)

From Dev

Adding a new node to the end of a linked list in C

From Dev

Deletion of node at end of linked list C++

From Dev

Removing Kth node from end of Linked List

From Dev

inserting a node at the end of linked list recursivly

From Dev

Deleting a node appended at the end of a linked list in C

From Dev

C program linked list add to end

From Dev

Doubly linked list, add node in-order

From Dev

How to add a node at the beginning of a single linked list?

From Dev

Recursively Add Node to Linked List at Specific Index

From Dev

Creating a recursive function to add a node to a linked list

From Dev

I'm getting a segmentation fault (core dumped) error when trying to add an Node to the end of a Linked List in c++

From Dev

Unexpected value after inserting node at the end of linked list

From Dev

Add to end of a single linked list Segmentation fault C

From Dev

arraylist vs linked list .why linked list is slower when we add in the end?

From Dev

arraylist vs linked list .why linked list is slower when we add in the end?

From Dev

Detecting the end of a linked list

Related Related

  1. 1

    Add end node to linked list

  2. 2

    Adding a node to the end of a linked list

  3. 3

    Inserting a node at the end of linked list

  4. 4

    Inserting a node at the end of a linked list

  5. 5

    Inserting a node in the end of a linked list

  6. 6

    Failed to add a node to linked list

  7. 7

    adding a node at the end of a Linked List (java)

  8. 8

    Deletion of node at end of linked list C++

  9. 9

    Removing the end node from a linked list

  10. 10

    Inserting a node at the end of linked list, .exe crashes

  11. 11

    linked list moving node to the end C

  12. 12

    Inserting a node at the end of a doubly linked list

  13. 13

    adding a node at the end of a Linked List (java)

  14. 14

    Adding a new node to the end of a linked list in C

  15. 15

    Deletion of node at end of linked list C++

  16. 16

    Removing Kth node from end of Linked List

  17. 17

    inserting a node at the end of linked list recursivly

  18. 18

    Deleting a node appended at the end of a linked list in C

  19. 19

    C program linked list add to end

  20. 20

    Doubly linked list, add node in-order

  21. 21

    How to add a node at the beginning of a single linked list?

  22. 22

    Recursively Add Node to Linked List at Specific Index

  23. 23

    Creating a recursive function to add a node to a linked list

  24. 24

    I'm getting a segmentation fault (core dumped) error when trying to add an Node to the end of a Linked List in c++

  25. 25

    Unexpected value after inserting node at the end of linked list

  26. 26

    Add to end of a single linked list Segmentation fault C

  27. 27

    arraylist vs linked list .why linked list is slower when we add in the end?

  28. 28

    arraylist vs linked list .why linked list is slower when we add in the end?

  29. 29

    Detecting the end of a linked list

HotTag

Archive