Linked List Question

This is a discussion on Linked List Question within the C++ Programming forums, part of the General Programming Boards category; Hi There, this is my code Code: #include <iostream> #include <string> using namespace std; struct node { string data; node ...

  1. #1
    Registered User
    Join Date
    May 2005
    Posts
    12

    Linked List Question

    Hi There, this is my code

    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    struct node
      {
    	string data;     
             node *nxt;
      };
    
    node *start_ptr = NULL;
    node *current; 
    
    void add_node_at_end()
    {
    	node *temp, *temp2;   
    
        
        temp = new node;
        cout << "Please enter any data: ";
        cin >> temp->data;
        temp->nxt = NULL;
    
        if (start_ptr == NULL)
    	{
    		start_ptr = temp;
    		current = start_ptr;		
              }
    
        else
        {
    		temp2 = start_ptr;
           
    		while (temp2->nxt != NULL) 
            {
    			temp2 = temp2->nxt;		
            }
            temp2->nxt = temp;
         }
    	
    }
    
    void display_list()
    {
    	node *temp;
        temp = start_ptr;
        cout << endl;
        if (temp == NULL)
           cout << "The list is empty!" << endl;
        else
    	{
    		while (temp != NULL)
    		{
    			// Display details for what temp points to
                 cout << "Data : "<<temp->data;
    			 cout << endl;
    			 current = current->nxt;
    			 temp = temp->nxt;
    		}
    		cout << "<- Current node"<<"\n";
    		cout << "End of list!" << endl;
    	}
    }
    
    void main()
    {
    	int i;
    	for (i=0; i<5; i++)
    		add_node_at_end();
    
    	display_list();
    }
    my friend add these following lines into my code:

    Code:
    temp2 = start_ptr;
           
    		while (temp2->nxt != NULL) 
            {
    			temp2 = temp2->nxt;		
            }
            temp2->nxt = temp;
    What actually will the code above do ??

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Get the last node?

    nxt
    Seriously, give me one good reason why "next" should be shortened...
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  3. #3
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    The code in question is adding a new node to the end of the linked list. First have to find the last node, then set the nxt pointer of the last node to point to the new node. And don't forget to set the nxt pointer of the new node to NULL (or 0) so that the next time that code is executed it will indicate the last node in the list.

  4. #4
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    Quote Originally Posted by Magos
    Seriously, give me one good reason why "next" should be shortened...
    Not as much typing or thinking

  5. #5
    C/C++ homeyg's Avatar
    Join Date
    Nov 2004
    Location
    Louisiana, USA
    Posts
    209
    Quote Originally Posted by Magos
    Get the last node?


    Seriously, give me one good reason why "next" should be shortened...
    lol

    I still don't see why you would use a linked list rather than an array (linked lists seem like such a pain in the ass..)

  6. #6
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,069
    Quote Originally Posted by homeyg
    lol

    I still don't see why you would use a linked list rather than an array (linked lists seem like such a pain in the ass..)
    I still don't get why you'd use an array over any of the many, many, many standard data structures in C++...

    ...and as far as modern programming goes, linked lists are simply not used anymore. They're outdated. They are, however, the basis of more advanced structures such as Binary Search Trees and Heaps and what have you...
    Sent from my iPadŽ

  7. #7
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    >> ...and as far as modern programming goes, linked lists are simply not used anymore.

    their demise has been greatly exaggerated. lists are a very basic data structure still used in both modern and archaic systems everywhere.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  8. #8
    chococoder
    Join Date
    Nov 2004
    Posts
    515
    well said. Even though you might not see it directly in many applications these days they're there in the background everywhere empowering the more advanced APIs you're programming to.

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,739
    Seriously, give me one good reason why "next" should be shortened...
    To write a program without using the letter 'e'. A little difficult when you want to #include headers though.

    their demise has been greatly exaggerated. lists are a very basic data structure still used in both modern and archaic systems everywhere.
    That's kind of what SlyMaelstrom wrote, I think.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709
    Quote Originally Posted by SlyMaelstrom
    I still don't get why you'd use an array over any of the many, many, many standard data structures in C++...
    I agree. I used to use arrays for anything that required them but now I've gotten more comfortable with vectors and the other various STL containers I find they're a lot more convienient and generally less hassle to program from all angles. Almost.
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

  11. #11
    Registered User
    Join Date
    May 2005
    Posts
    12
    Quote Originally Posted by Ancient Dragon
    The code in question is adding a new node to the end of the linked list. First have to find the last node, then set the nxt pointer of the last node to point to the new node. And don't forget to set the nxt pointer of the new node to NULL (or 0) so that the next time that code is executed it will indicate the last node in the list.
    Thx for the answer. I get it now.

    Quote Originally Posted by Magos
    Seriously, give me one good reason why "next" should be shortened...
    Since this is only for testing only, i didn't really put my attention into the naming .

    Quote Originally Posted by SlyMaelstrom
    I still don't get why you'd use an array over any of the many, many, many standard data structures in C++...
    I don't have any other choice since i'm a student and i don't know any data structures beside stack and linked list. furthermore i have to use linked list to do expression evaluation for my assigment . any good advice for me

  12. #12
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,069
    Quote Originally Posted by isarapearl
    I don't have any other choice since i'm a student and i don't know any data structures beside stack and linked list. furthermore i have to use linked list to do expression evaluation for my assigment . any good advice for me
    If a teacher asks you to use a linked list, then you should use a linked list. They want you to know it for a reason. I'm not bashing them and whether or now you should know how to use them, because you should. I'm just saying when you're done with class you should look into the Standard Template Library. It's all standard C++ so you're free to use it without hassle. It's always good to have more tools in your toolbox.
    Sent from my iPadŽ

  13. #13
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    linked lists are simply not used anymore. They're outdated
    If you need a way to access all created objects, linked lists are unbeatable since they have 0(1) adding and O(1) removal (add to list in constructor, remove from list in destructor).
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  14. #14
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    Quote Originally Posted by homeyg
    I still don't see why you would use a linked list rather than an array (linked lists seem like such a pain in the ass..)
    I'm assuming you've never even tried writing anything like a B-tree...
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  15. #15
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,304
    Quote Originally Posted by SlyMaelstrom
    ...and as far as modern programming goes, linked lists are simply not used anymore. They're outdated.
    Oh so, that's why it says "Liberally stupid" under your name! Makes perfect sense now.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linked List Not Saving Value as Int
    By bar338 in forum C Programming
    Replies: 4
    Last Post: 05-04-2009, 07:53 PM
  2. linked list question
    By brb9412 in forum C Programming
    Replies: 16
    Last Post: 01-04-2009, 03:05 PM
  3. singly linked circular list
    By DarkDot in forum C++ Programming
    Replies: 0
    Last Post: 04-24-2007, 08:55 PM
  4. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM

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