Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5

Thread: Linked Lists

  1. #1
    New Coder
    Join Date
    Mar 2004
    Posts
    56
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Linked Lists

    could someone please explain to me how to edit a value in a linked list

  • #2
    Senior Coder
    Join Date
    Jun 2002
    Location
    Wichita
    Posts
    3,880
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Stowelly
    could someone please explain to me how to edit a value in a linked list
    Details! We can't help you if you don't provide enough details!
    Check out the Forum Search. It's the short path to getting great results from this forum.

  • #3
    New Coder
    Join Date
    Mar 2004
    Posts
    56
    Thanks
    0
    Thanked 0 Times in 0 Posts
    sorry ill paste the program in and try to explain it a bit better


    ok basically i have a linked list which outputs the value "i" from each node. but i want to add a function which adds a value to "x" and "y" based on a user inputting numbers. but i would like to be able to access any specific node to edit these values.... hope that makes sense



    Code:
    class Node
    {
    public:
    int i;
    int x;
    int y;
    Node *next;
    Node(){i = 0; next = NULL;}
    };
    class List
    {
    public:
    Node *Head;
    List() {Head = NULL;}
    List(int n);
    void Display();
    };
     
    int main(int argc, char* argv[])
    {
     List bob(10);
    	
     bob.Display();
     return 0;
    }
    List::List(int n)
    {
    
     Node *Temp;
     Node *Last;
     Head = NULL;
     for (int i = 0; i <n;i++)
     {
    
       if (Head == NULL)
       {
        Temp = new Node;
        Head = Temp;
        Temp->next = NULL;
        Temp->i = i;
    
    	
        Last = Temp;
       }
       else
       {Temp = new Node;
        Temp->i = i;
        Last->next = Temp;
        Temp->next = NULL;
        Last = Last->next;
       }
      
     }
    }
    void List::Display()
    {
    Node *Temp = Head;
    cout << Temp->i << "  x = " << Temp->x << "   y= " << Temp->y << "\n";
    
    while (Temp->next != NULL)
    {
     cout << Temp->next->i << "  x = " << Temp->next->x << "   y= " << Temp->next->y << "\n";
     Temp = Temp->next;
     
    }
    }

  • #4
    Regular Coder
    Join Date
    Feb 2004
    Location
    WA
    Posts
    213
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Try writing two functions

    FindNode & UpdateNode

    Node* FindNode(List *list,int index)
    - This will go through the list and find the Node with the correct index and return the pointer to the node

    void UpdateNode(Node *node, int x, int y)
    - This will update the node members x and y.

    Now in your main, ask the user to select a node by specifying an index, then ask him to enter new values for x and y. once you have them, call FindNode to locate the node he is interested in and update using the UpdateNode.

    Let us know how it goes.
    Nobody is Perfect. I am Nobody.

  • #5
    New Coder
    Join Date
    Mar 2004
    Posts
    56
    Thanks
    0
    Thanked 0 Times in 0 Posts
    thanks ill have a go at that abit later. im also really confused about another aspect of linked lists.

    i have a program that displays a worm made up of a chain 8 segmants stored as a linked list and i want to make it so these segments can be moved to anywhere in the chain...... thus the nodes will have to move position. this is really confusing me im sure its alot easier than it looks


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •