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?
Printable View
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?
Not with me at the moment, but there's a nice simple implementation of a linked list in the cprogramming.com tutorials.
-Prelude
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.
BTW if not.....ignore what I said.
I found this.......this is where I got the basic ideas of linked lists.......click here .
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.
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.
What exactly does the -> operator do?
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.
Pointer?Quote:
What exactly does the -> operator do?
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.