Thread: Linked Lists

  1. #1
    Seņor Member
    Join Date
    Jan 2002
    Posts
    560

    Linked Lists

    I understand the concept of linked lists, but the book I have decided to make a linked list class, but it isn't very clear. Does anyone have some simple code implementing a linked list?

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Not with me at the moment, but there's a nice simple implementation of a linked list in the cprogramming.com tutorials.

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

  3. #3
    left crog... back when? incognito's Avatar
    Join Date
    Oct 2001
    Posts
    1,427
    Hey tim are you by any chance using Sams Teach Yourself C++ in 21.........well if so you're about the third person I know with the same problem.........I am one of them. For a better explanation keep going to the week review and that one will be much easier.
    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.

  4. #4
    left crog... back when? incognito's Avatar
    Join Date
    Oct 2001
    Posts
    1,427
    BTW if not.....ignore what I said.
    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
    left crog... back when? incognito's Avatar
    Join Date
    Oct 2001
    Posts
    1,427
    I found this.......this is where I got the basic ideas of linked lists.......click here .
    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.

  6. #6
    Registered User
    Join Date
    Dec 2001
    Posts
    38
    This might not be as simple as you want, but it is a linked list class using a double template type. One for the list and one for the key(search object). If you go through it a few times it might help. The main thing to think is connected to any data entry(or node) there is a link. That link points to the next data entry(or node). The last data entry will have a link pointing to NULL. I hope this helps.
    SilasP

  7. #7
    Seņor Member
    Join Date
    Jan 2002
    Posts
    560
    I bought a VB book by Sams, and that's the last Sams book I will ever buy. I'm using Practical C++ by O'Reilly, which up ot this point, has done a very good job.

  8. #8
    Seņor Member
    Join Date
    Jan 2002
    Posts
    560
    What exactly does the -> operator do?

  9. #9
    Unregistered
    Guest
    the following mean the same thing:

    (*ptr).value;

    ptr->value;

    The meaning is dereference the pointer to an instance of a user defined struct/class and refer to the pertinent member of that object as indicated by the stuff following the dot/arrow. This syntax can be used to refer to either data members (variables) or member functions (methods). Most people find the latter syntax to be easier to read and type. Unforunately, you apparently need to use the former syntax when using iterators.

  10. #10
    left crog... back when? incognito's Avatar
    Join Date
    Oct 2001
    Posts
    1,427
    What exactly does the -> operator do?
    Pointer?


    Code:
    // Listing 14.4
    // Calling multiple constructors
    
    #include <iostream>
    using namespace std;
    
    typedef int HANDS;
    enum COLOR { Red, Green, Blue, Yellow, White, Black, Brown } ;
    
    class Horse
    {
    public:
    	Horse(COLOR color, HANDS height);
    	virtual ~Horse() { cout << "Horse destructor...\n"; }
    	virtual void Whinny()const { cout << "Whinny!... "; }
    	virtual HANDS GetHeight() const { return itsHeight; }
    	virtual COLOR GetColor() const { return itsColor; }
    private:
    	HANDS itsHeight;
    	COLOR itsColor;
    };
    
    Horse::Horse(COLOR color, HANDS height):
    itsColor(color),itsHeight(height)
    {
    	cout << "Horse constructor...\n";
    }
    
    class Bird
    {
    public:
    	Bird(COLOR color, bool migrates);
    	virtual ~Bird() {cout << "Bird destructor...\n";  }
    	virtual void Chirp()const { cout << "Chirp... ";  }
    	virtual void Fly()const   //same here doesn't really have to be
                                  //virtual in order for it to run
    	{
    		cout << "I can fly! I can fly! I can fly! ";
    	}
    	virtual COLOR GetColor()const { return itsColor; }
    	virtual bool GetMigration() const { return itsMigration; }
    
    private:
    	COLOR itsColor;
    	bool itsMigration;
    };
    
    Bird::Bird(COLOR color, bool migrates):
    itsColor(color), itsMigration(migrates)
    {
    	cout << "Bird constructor...\n";
    }
    
    class Pegasus : public Horse, public Bird
    {
    public:
    	void Chirp()const { Whinny(); }
    	Pegasus(COLOR, HANDS, bool,long);
    	~Pegasus() {cout << "Pegasus destructor...\n";}
    	virtual long GetNumberBelievers() const //don't see a need for this
                                                //to be virtual
    	{
    		return  itsNumberBelievers;
    	}
    
    private:
    	long itsNumberBelievers;
    };
    
    Pegasus::Pegasus(COLOR aColor,	HANDS height, bool migrates, long NumBelieve):
    	Horse(aColor, height),
    	Bird(aColor, migrates),
    	itsNumberBelievers(NumBelieve)
    {
    cout << "Pegasus constructor...\n";
    }
    
    int main()
    {
    	Pegasus *pPeg = new Pegasus(Red, 5, true, 10);
    pPeg->Fly();// Same as (*Peg).Fly();
    	pPeg->Whinny();
    	cout << "\nYour Pegasus is " << pPeg->GetHeight();
    	cout << " hands tall and ";
    	if (pPeg->GetMigration())
    		cout << "it does migrate.";
    	else
    		cout << "it does not migrate.";
    	cout << "\nA total of " << pPeg->GetNumberBelievers();
    	cout << " people believe it exists.\n";
    	delete pPeg;
        int exit;cin>>exit;
    
    	return 0;
    }

    (*SomePointer).GetAge(); == SomePointer->GetAge();


    //hope this helps.
    Last edited by incognito; 03-10-2002 at 03:58 PM.
    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. Linked Lists 101
    By The Brain in forum C++ Programming
    Replies: 5
    Last Post: 07-24-2004, 04:32 PM
  2. Map file formats and linked lists
    By Spitball in forum Game Programming
    Replies: 2
    Last Post: 03-04-2004, 11:32 PM
  3. Linked Lists Integer addition ? HELP Please??
    By green_eel in forum C Programming
    Replies: 3
    Last Post: 03-12-2003, 04:36 PM
  4. need help w/ linked lists
    By MKashlev in forum C++ Programming
    Replies: 11
    Last Post: 08-05-2002, 08:57 PM
  5. doubly linked lists
    By qwertiop in forum C++ Programming
    Replies: 3
    Last Post: 10-03-2001, 06:25 PM