Skip to content

Instantly share code, notes, and snippets.

@iodiot
Created August 16, 2013 13:08
Show Gist options
  • Save iodiot/6249844 to your computer and use it in GitHub Desktop.
Save iodiot/6249844 to your computer and use it in GitHub Desktop.
#include <iostream >
using namespace std;
struct Node
{
Node() { next = 0; }
virtual void PrintValue() = 0;
Node *next;
};
struct IntNode : public Node
{
IntNode(int value) : intValue(value) {}
virtual void PrintValue() { cout << intValue << endl; }
int intValue;
};
struct FloatNode : public Node
{
FloatNode(float value) : floatValue(value) {}
virtual void PrintValue() { cout << floatValue << endl; }
float floatValue;
};
class List
{
public:
List()
{
current = first = 0;
}
void Add(Node *node)
{
if (!first)
{
first = node;
current = node;
}
else
{
current->next = node;
current = node;
}
}
void PrintAllNodes()
{
Node *node = first;
while (node)
{
node->PrintValue();
node = node->next;
}
}
private:
Node *current, *first;
};
int main()
{
List list;
list.Add(new IntNode(42));
list.Add(new IntNode(665));
list.Add(new IntNode(666));
list.Add(new FloatNode(3.1415f));
list.PrintAllNodes();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment