Thread: linked list.......

  1. #1
    left crog... back when? incognito's Avatar
    Join Date
    Oct 2001
    Posts
    1,427

    linked list.......

    Can you show me a simple code to add a node after the current one? Please I need your help.
    There are some real morons in this world please do not become one of them, do not become a victim of moronitis. PROGRAMMING IS THE FUTURE...THE FUTURE IS NOW!!!!!!!!!

    "...The only real game I thank in the world is baseball..." --Babe Ruth

    "Life is beautiful"-Don Corleone right before he died.

    "The expert on anything was once a beginner" -Baseball poster I own.


    Left cprog on 1-3-2005. Don't know when I am coming back. Thanks to those who helped me over the years.

  2. #2
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    heres one from something i was doing earlier. Pervert it to your needs.
    Code:
    Node* NewNode(char* Value,size_t Len)
    {
    	Node* NodePtr=malloc(sizeof (Node));
    	if(!NodePtr) Bail();
    	NodePtr->Value=malloc(Len);
    	if(!NodePtr->Value) 
    	{
    		free(NodePtr);
    		Bail();
    	}
    	strcpy(NodePtr->Value,Value);
    	NodePtr->Next=NULL;
    	return NodePtr;
    }
    
    
    /* Adds a node to the tail of the list so list will act as a queue */
    Node* AddNode(Node* Head,char* Value,size_t Len)
    {
    	Node* Current;
    	Node* New=NewNode(Value,Len);
    	if (!Head)Head=New;
    	else
    	{
    		Current=Head;
    		while(Current->Next != NULL) Current=Current->Next;
    		Current->Next=New;
    	}
    	return Head;
    }
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Here's some simple code to insert node t after node x.
    Code:
    void insertNode ( node *x, node *t )
    { 
      t->next = x->next; x->next = t; 
    }
    -Prelude
    My best code is written with the delete key.

  4. #4
    left crog... back when? incognito's Avatar
    Join Date
    Oct 2001
    Posts
    1,427
    Ok, I am back to square one the same problem I had when I was reading the tutorial........take a look at the image I've attached where I've tried to recreate that linked list, I have a question on that one.
    There are some real morons in this world please do not become one of them, do not become a victim of moronitis. PROGRAMMING IS THE FUTURE...THE FUTURE IS NOW!!!!!!!!!

    "...The only real game I thank in the world is baseball..." --Babe Ruth

    "Life is beautiful"-Don Corleone right before he died.

    "The expert on anything was once a beginner" -Baseball poster I own.


    Left cprog on 1-3-2005. Don't know when I am coming back. Thanks to those who helped me over the years.

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Code:
    Start:
    Node-->x-->Node-->Node
    
           t-->NULL
    
    t->next = x->next;
    Node-->x-->Node-->Node
               |
           t---^
    
    x->next = t;
    Node-->x   Node-->Node
           |   |
           t---^
    
    End:
    Node-->x-->t-->Node-->Node
    -Prelude
    My best code is written with the delete key.

  6. #6
    left crog... back when? incognito's Avatar
    Join Date
    Oct 2001
    Posts
    1,427
    sorry but that got me a little confused but I mean is the graphic that I posted the right idea? or am I going all wrong about i?
    There are some real morons in this world please do not become one of them, do not become a victim of moronitis. PROGRAMMING IS THE FUTURE...THE FUTURE IS NOW!!!!!!!!!

    "...The only real game I thank in the world is baseball..." --Babe Ruth

    "Life is beautiful"-Don Corleone right before he died.

    "The expert on anything was once a beginner" -Baseball poster I own.


    Left cprog on 1-3-2005. Don't know when I am coming back. Thanks to those who helped me over the years.

  7. #7
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >or am I going all wrong about i?
    Got it in one. Don't think of t as already being a part of the list, it's completely separated so the first statement will assign t's next pointer to point at the node that x points to. The second statement causes t to be fully inserted in the list by pointing x's next pointer to t.

    The end result is that t is insterted right between x and the node that x points to. I promise it's not as hard as it sounds, just difficult to explain. Get out some change from your pocket/purse and make a list out of them, then insert a coin.

    Quarter--Nickel--Penny

    Work it out so that you insert a dime between the quarter and nickel using the code I gave you. The quarter is x and the dime is t, that's how I figured out linked lists.

    -Prelude
    My best code is written with the delete key.

  8. #8
    left crog... back when? incognito's Avatar
    Join Date
    Oct 2001
    Posts
    1,427
    Hold one this is read from LEFT TO RIGHT or FROM RIGHT TO LEFT?
    There are some real morons in this world please do not become one of them, do not become a victim of moronitis. PROGRAMMING IS THE FUTURE...THE FUTURE IS NOW!!!!!!!!!

    "...The only real game I thank in the world is baseball..." --Babe Ruth

    "Life is beautiful"-Don Corleone right before he died.

    "The expert on anything was once a beginner" -Baseball poster I own.


    Left cprog on 1-3-2005. Don't know when I am coming back. Thanks to those who helped me over the years.

  9. #9
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Left to right

    -Prelude
    My best code is written with the delete key.

  10. #10
    left crog... back when? incognito's Avatar
    Join Date
    Oct 2001
    Posts
    1,427
    oooooooooooooooooooooooh damn now I know that.
    There are some real morons in this world please do not become one of them, do not become a victim of moronitis. PROGRAMMING IS THE FUTURE...THE FUTURE IS NOW!!!!!!!!!

    "...The only real game I thank in the world is baseball..." --Babe Ruth

    "Life is beautiful"-Don Corleone right before he died.

    "The expert on anything was once a beginner" -Baseball poster I own.


    Left cprog on 1-3-2005. Don't know when I am coming back. Thanks to those who helped me over the years.

  11. #11
    left crog... back when? incognito's Avatar
    Join Date
    Oct 2001
    Posts
    1,427
    house->driveway=apartment->driveway;
    apartment->driveway=house;//for some reason I see this as being the joining link between the house and the apartment, this being the driveway.
    There are some real morons in this world please do not become one of them, do not become a victim of moronitis. PROGRAMMING IS THE FUTURE...THE FUTURE IS NOW!!!!!!!!!

    "...The only real game I thank in the world is baseball..." --Babe Ruth

    "Life is beautiful"-Don Corleone right before he died.

    "The expert on anything was once a beginner" -Baseball poster I own.


    Left cprog on 1-3-2005. Don't know when I am coming back. Thanks to those who helped me over the years.

  12. #12
    left crog... back when? incognito's Avatar
    Join Date
    Oct 2001
    Posts
    1,427
    Please I just want to know if this is right before I go to bed and see if I can get some sleep.
    There are some real morons in this world please do not become one of them, do not become a victim of moronitis. PROGRAMMING IS THE FUTURE...THE FUTURE IS NOW!!!!!!!!!

    "...The only real game I thank in the world is baseball..." --Babe Ruth

    "Life is beautiful"-Don Corleone right before he died.

    "The expert on anything was once a beginner" -Baseball poster I own.


    Left cprog on 1-3-2005. Don't know when I am coming back. Thanks to those who helped me over the years.

  13. #13
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >house->driveway=apartment->driveway;
    >apartment->driveway=house;
    The apartment is first, the apartment's driveway leads to the house. The house's driveway leads to whatever the apartment's driveway lead to before the house got in the way.

    This is not a very good abstraction because you think in construction terms and no sane contractor would build things like this.

    >//for some reason I see this as being the joining link between the house and the apartment
    It is, the joining link from the apartment to the house to be precise. But driveways aren't good as links because a driveway should be a link from the house to the road or the apartment to the road, not the apartment to the house. But if this confuses you just ignore it because I'm blabbering now.

    -Prelude
    My best code is written with the delete key.

  14. #14
    left crog... back when? incognito's Avatar
    Join Date
    Oct 2001
    Posts
    1,427
    Thanks both Stoned_Coder and Prelude
    There are some real morons in this world please do not become one of them, do not become a victim of moronitis. PROGRAMMING IS THE FUTURE...THE FUTURE IS NOW!!!!!!!!!

    "...The only real game I thank in the world is baseball..." --Babe Ruth

    "Life is beautiful"-Don Corleone right before he died.

    "The expert on anything was once a beginner" -Baseball poster I own.


    Left cprog on 1-3-2005. Don't know when I am coming back. Thanks to those who helped me over the years.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ Linked list program need help !!!
    By dcoll025 in forum C++ Programming
    Replies: 1
    Last Post: 04-20-2009, 10:03 AM
  2. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 10:10 AM
  3. Reverse function for linked list
    By Brigs76 in forum C++ Programming
    Replies: 1
    Last Post: 10-25-2006, 10:01 AM
  4. Template Class for Linked List
    By pecymanski in forum C++ Programming
    Replies: 2
    Last Post: 12-04-2001, 09:07 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM