LinkedList Error

This is a discussion on LinkedList Error within the C++ Programming forums, part of the General Programming Boards category; Ill try that. I think the problem I was tring to do. Was make one function perform all three functions. ...

  1. #16
    Registered User
    Join Date
    Nov 2004
    Posts
    49
    Ill try that. I think the problem I was tring to do. Was make one function perform all three functions. but that will still cause an error wouldnt it. Lets see
    node->Next=Start; //make your added node point to start of list
    Start=node; // make the start pointer equal to your added node;
    [start]->Null
    [Node]->Null
    Node->Next=Start;
    Start=Node
    Start->[Node]->[ ]
    Now lets add it again.
    [Node]->Next=Start but now it will equal itself.
    Cause Start=Node.
    Then
    Start=Node;
    and it points to itself now.
    See the error I mean now?

  2. #17
    Registered User manofsteel972's Avatar
    Join Date
    Mar 2004
    Posts
    317
    each time you create a new node with the new operator the address stored in the pointer is different


    Code:
    Node*nodepointer;
    nodepointer= new Node;
    nodepointer->Name="Frank";
    nodepointer->Next=NULL;
     
    nodepointer=new Node; a completely different address is now stored in nodepointer; 
     
    nodepointer->Name="Sue";
    nodepointer->Next=NULL;
    so when you add this again it isn't pointing to the same node you created an entirely new one.
    "Knowledge is proud that she knows so much; Wisdom is humble that she knows no more."
    -- Cowper

    Operating Systems=Slackware Linux 9.1,Windows 98/Xp
    Compilers=gcc 3.2.3, Visual C++ 6.0, DevC++(Mingw)

    You may teach a person from now until doom's day, but that person will only know what he learns himself.

    Now I know what doesn't work.

    A problem is understood by solving it, not by pondering it.

    For a bit of humor check out xkcd web comic http://xkcd.com/235/

  3. #18
    Registered User
    Join Date
    Nov 2004
    Posts
    49
    I dont do that though. Im tring to relocate the same node. Can you do that without causing errors?

  4. #19
    Registered User manofsteel972's Avatar
    Join Date
    Mar 2004
    Posts
    317
    You have to create each node you want to have in your list. You can't just create one node for the whole list. If you have 10 nodes then you need to create 10 nodes using new.

    [edit]
    and of course you would then need to delete all 10 nodes when your done using the delete operator.

    Here is a visuallization that might help clear up things. Imagine that you are a pointer_to_node; You are standing there pointing at nothing because we haven't initialized you yet. So we create a node for you to point at. The node is a box with an arrow pointing at nothing. The nextpointer. Now we put data into the box and we make the arrow point to a null. A furry creature that is not quite a Gnoll but close. Now we want to add another node. So we create another node a big black box with an arrow the next pointer using new. We have this other nodepointer that is keeping track of this newly created node until you can take over for him. After the new node is created we make the arrow on the node(then next pointer) point to the node your pointing at (since when we make pointers == they both point to the same thing) and the make you point to this newly created node. Now the other nodepointer can go create another new node since you relieved him. See how that works now?
    Last edited by manofsteel972; 11-28-2004 at 07:57 AM.
    "Knowledge is proud that she knows so much; Wisdom is humble that she knows no more."
    -- Cowper

    Operating Systems=Slackware Linux 9.1,Windows 98/Xp
    Compilers=gcc 3.2.3, Visual C++ 6.0, DevC++(Mingw)

    You may teach a person from now until doom's day, but that person will only know what he learns himself.

    Now I know what doesn't work.

    A problem is understood by solving it, not by pondering it.

    For a bit of humor check out xkcd web comic http://xkcd.com/235/

  5. #20
    Registered User
    Join Date
    Nov 2004
    Posts
    49
    I knew that. I wasnt tring to make a List of all the same nodes. I was tring to say. When You take a Node that points at the beginging and you try to take that same node and point it at the same spot. It will cause an error. Maybe this isnt the behavoir it should have. You shouldnt add a node you already added to the same spot. but It causes an error. and I would like to fix that is there a way? It seems to cause an error whenever you try to take one node and change its position it had in the list to another one.

  6. #21
    Registered User manofsteel972's Avatar
    Join Date
    Mar 2004
    Posts
    317
    To move a node you just reassign the pointers. Save the node in a Temp pointer.
    reassign the pointers of the previous node to point to the node after the one your moving.
    then just travel to place in the list you want to insert it . Save the pointer to the next node.
    make the current node point to the node your moving make the moved node point to the next node.
    "Knowledge is proud that she knows so much; Wisdom is humble that she knows no more."
    -- Cowper

    Operating Systems=Slackware Linux 9.1,Windows 98/Xp
    Compilers=gcc 3.2.3, Visual C++ 6.0, DevC++(Mingw)

    You may teach a person from now until doom's day, but that person will only know what he learns himself.

    Now I know what doesn't work.

    A problem is understood by solving it, not by pondering it.

    For a bit of humor check out xkcd web comic http://xkcd.com/235/

  7. #22
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    Let's say we have an empty list and add our first node

    C

    This would often be a special case in our addNode function. Then we add another node before C

    A-C

    This would often be another special case in the addNode function. Then we add another node jsut before C

    A-B-C

    This would often be another special case in the addNode function. Then we add a fourth node after C

    A-B-C-D

    This would often be a fourth special case in the addNode function.

    Now could we remove B from it's current postion in the list and add it back between C and D. Yes. Could we remove B and put it before A. Yes. Could we remove B and put it after D. Yes. Cout we remove B and put it back where is came from. Yes. Coud we move any node beside B to a new spot in the list? Yes. But I would probably do this node swapping in a separate function from the one where I was just adding A, B, C, or D to the list in the first place. The node swapping function may or may not call the addNode function to put the removed node back into the list, that's up to you.

  8. #23
    Registered User manofsteel972's Avatar
    Join Date
    Mar 2004
    Posts
    317
    Listen to Elad he is much smarter then me. Foregive me I didn't mean to write a whole tutorial on Linked lists just trying to help. I used to work in a help desk so I tend to get verbose.
    "Knowledge is proud that she knows so much; Wisdom is humble that she knows no more."
    -- Cowper

    Operating Systems=Slackware Linux 9.1,Windows 98/Xp
    Compilers=gcc 3.2.3, Visual C++ 6.0, DevC++(Mingw)

    You may teach a person from now until doom's day, but that person will only know what he learns himself.

    Now I know what doesn't work.

    A problem is understood by solving it, not by pondering it.

    For a bit of humor check out xkcd web comic http://xkcd.com/235/

  9. #24
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    I'm glad I don't have to take a class with most of the people who post here, including manofsteel972, because I'd end up on the tail end of the curve. This is a great place to learn though.

    Artist_Of_Dream--I suspect that English may not be your native tongue and therefore trying to phrase your questions becomes doubly difficult. Therefore, to cover other possible aspects of your recent posts, I'll add the following. It is possible to have nodes with more than one link, and it is possible to have one node point to more than one other node, and it is also possible to have more than one node point to any given node. I think of these structures as follows.

    A linked container is where you have one node (which I call parent) pointing to other nodes (which I call children of the parent node, and which may be parents in their own right). A singly linked list then consists of a container where each parent may point to only one child. A doubly linked list is where each parent may point to only one child, and each child may only point to it's own parent. A tree is when each parent may point to more than one child and may or may not point back to it's own parent. A graph (map?) is when any parent may point to any child and any child may point to any parent. I don't know what you would call a container if a given node could point back to itself (degenerate?).

  10. #25
    Registered User
    Join Date
    Nov 2004
    Posts
    49
    English is my first language and only language. Raises eyebrow. My Question was not understood I guess. I guess I didnt explain myself well. Let me try again. How do I make a function to just add a node before the start, without causing errors. The error Im referring too, is when you take a pointer which you just added before start, and have it point to start again. Can you show me a function which catches this or never has this happen would be better. I know one wouldnt want to point a node to the same spot twice, but it causes an error never the lest. and that can cause a glitch later. I rather have my program be glitch free Hope everyone understands my question now. I would like to see such a function in a simple linked list so I can test it. and see why it works. I cant figure it out on my own. Im unsure why no one else seems to see this error. Maybe Im implementing everything wrong and thats causing the error. Thanks for all the feedback. but Im starting to think Im getting to the point ive become annoying. if so Dont need to reply to this anymore, I can always try to figure it out from a web page and self practice.

  11. #26
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    Level 1: Be able to create multiple singly linked nodes.

    Level 2: Be able to traverse and display your list of singly linked nodes.

    Level 3: Be able to delete a node from the list.. and re-attach the list where the node was deleted.

    Level 4: Inserting a new node into the middle of a list.

    Level 5: Swapping nodes with other nodes in a list.
    Last edited by The Brain; 11-28-2004 at 12:57 PM.
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  12. #27
    Registered User
    Join Date
    Nov 2004
    Posts
    49
    Here are my Source Files, Redone:
    LinkedList.h
    Code:
    #pragma once
    #include <string>
    #include <iostream>
    using namespace std;
    
    class Node {
    public:
    	Node::Node();
    	Node::~Node();
    string name;
    Node * next;
    };
    
    Node::Node(){
    next=NULL;
    }
    Node::~Node(){}
    
    class Link {
    public:
    void AddNode(Node *);
    void DisplayNodes();
    Link();
    ~Link();
    private:
    Node * start;
    };
    
    Link::Link(){
    start=NULL;
    }
    Link::~Link(){}
    void Link::AddNode(Node *W){
    W->next=start;
    start=W;
    }
    
    void Link::DisplayNodes(){
    cout<<"start->name: "<<start->name<<endl;
    cout<<"start->next->name: "<<start->next->name<<endl;
    }
    Test.cpp
    Code:
    #include "LinkedList.h"
    #include <iostream>
    using namespace std;
    
    
    int main(){
    Link L;
    Node *N=new Node;
    N->name="N";
    L.AddNode(N);
    L.AddNode(N);
    L.DisplayNodes();
    system("PAUSE");	
    
    return 0;
    }
    There, That will show you my up to date code and what im talkng about

  13. #28
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    Place this function in your list and check it out.
    Code:
    void Link::AddNode(Node *W){
    //Create a new node to add it to list each time function called.
    //Don't add W directly to list as it is created outside 
    //of list and therefore may be changed by factors 
    //outside of list which may screw up your list
    Node * newNode = new Node;
     
    //copy name from W to name in newNode
    newNode->name = W->name;
     
    //make newNode->next equal to NULL
    newNode = NULL;
     
    //place newNode, not W, into list in front of start
    newNode->next = start;
     
    //move start one node to the left to keep track of
    //first node in list.
    start = newNode;
    }
    here's an updated version of a display function to confirm the above code works.
    Code:
    void Link::displayList()
    {
    Node* current = start;
    while(current != NULL)
     {
    	cout << current->name << endl;
    	current = current->next;
     }
    }
    Last edited by elad; 11-28-2004 at 02:41 PM.

  14. #29
    Registered User
    Join Date
    Nov 2004
    Posts
    49
    Thanks. That Isnt what I was tring to do though. That created a totally new Node with the same name as the other one. And all. But I wanted it where I can just try to add nodes to the beginging and if it is the same node then Just dont add it there since its already there. I tried using if statements but it gave me access faults and didnt work. I dont think what Im tring to do is possible

  15. #30
    Registered User
    Join Date
    Nov 2004
    Posts
    49
    Its ok, You dont have to respond to this thread no more. Thanks for all the Help. I just dont want to waste your time anymore. and this thread is getting rediculously long and I kinda feel embrassed now lol.

Page 2 of 3 FirstFirst 123 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 09:18 PM
  2. An error is driving me nuts!
    By ulillillia in forum C Programming
    Replies: 5
    Last Post: 04-04-2009, 10:15 PM
  3. Making C DLL using MSVC++ 2005
    By chico1st in forum C Programming
    Replies: 26
    Last Post: 05-28-2008, 02:17 PM
  4. Connecting to a mysql server and querying problem
    By Diod in forum C++ Programming
    Replies: 8
    Last Post: 02-13-2006, 10:33 AM
  5. Couple C questions :)
    By Divx in forum C Programming
    Replies: 5
    Last Post: 01-28-2003, 01:10 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21