// The project is based on the source file "LinkedList.cpp" showed in the textbook.
// This project is important for me to undstand the relevant data structure.
// by Hall_Wood
// Time :2016-08-07
/*
Demonstrates an object-oriented approach to
linked list.The list delegates to the node.
The node is an abstract data type.Three types
of nodes are used, head nodes, tail nodes and
internal nodes.Only the internal nodes hold data.
*/
/*
The data class is created to serve as an object to
hold in linked list.
*/
#include "stdafx.h"
#include <iostream>
using namespace std;
enum { kIsSmaller, kIsLarger, kIsSame };
/*
Data class to put into the linked list.
Any class in this linked list must support 2
functions: show (display the value) and compare
(return relative position).
*/
class Data
{
public:
Data(int newVal): value(newVal) {}
~Data(void) {}
int getValue(void) const { return value; }
int compare(const Data&);
void show(void) { cout << value << endl; }
private:
int value;
};
/*
Compare function is used to decide where in the list
a particular object belongs.
*/
int Data::compare(const Data& otherData)
{
if (value < otherData.getValue())
return kIsSmaller;
else if (value > otherData.getValue())
return kIsLarger;
else
return kIsSame;
}
// forward declarations
class Node;
class HeadNode;
class TailNode;
class InternalNode;
/*
ADT representing the node object in the list.
Every derived class must override insert and show.
*/
class Node
{
public:
Node(void) {}
virtual ~Node(void) {}
virtual Node* insert(Data* data) = 0;
virtual void show(void) = 0;
private:
// nothing
};
/*
This is the node that hold the actual object.
In this case the object is of type Data.
We'll see how to make this general when
we cover templates.
*/
class InternalNode :public Node
{
public:
InternalNode(Data* data, Node* next);
virtual ~InternalNode(void) { delete next;delete data; }
virtual Node* insert(Data* data);
virtual void show(void)
{
data->show();
next->show();
}// delegate!
private:
Data *data; // the data itself
Node *next; // points to the next node in the linked list
};
/*
The declaration of the function, constructor.
All the constructor of InternalNode dose is to initialize.
*/
InternalNode::InternalNode(Data* newData, Node* newNext):
data(newData),next(newNext)
{
// do nothing
}
/*
The declaration of the function insert.
The mean of the list.
When you put a new object into the list
it is passed to the node which figures out
where it gose and insert it into the list
*/
Node* InternalNode::insert(Data* otherData)
{
// is the value of the new member bigger or smaller than mine?
int result = data->compare(*otherData);
switch (result)
{
case kIsLarger: // fall through
case kIsSame: // new data comes before me
{
InternalNode* dataNode = new InternalNode(otherData, this);
return dataNode;
}
// It is bigger than I am so pass it on the next node
// and let IT handle it.
case kIsSmaller:
next = next->insert(otherData);
return this;
}
return this; // appease the compiler
}
/*
TailNode is just a sentinel.
*/
class TailNode: public Node
{
public:
TailNode(void) {}
virtual ~TailNode(void) {}
virtual Node* insert(Data* data);
virtual void show(void) {}
private:
// nothing
};
/*
The declaration of the function insert of the class
TailNode.If data comes to me, it must be inserted
before me as I'm the tail and NOTING comes after me.
*/
Node* TailNode::insert(Data* data)
{
InternalNode* dataNode = new InternalNode(data, this);
return dataNode;
}
/*
HeadNode has no data,it just points to every
beggining of the list.
*/
class HeadNode : public Node
{
public:
HeadNode(void);
virtual ~HeadNode(void) { delete next; }
virtual Node* insert(Data* data);
virtual void show(void) { next->show(); }
private:
Node* next;
};
/*
The declaration of constructor of HeadNode.
As soon as the head is created, it creats the tail.
*/
HeadNode::HeadNode(void)
{
next = new TailNode;
}
/*
The declaration of the insert function of the HeadNode.
Beacuz nothing comes before the head so just pass the
data to the next node.
*/
Node* HeadNode::insert(Data* data)
{
next = next->insert(data);
return this;
}
/*
The LinkedList class.
I get all the credit and do none of the work.
*/
class LinkedList
{
public:
LinkedList(void);
~LinkedList(void) { delete head; }
void insert(Data* data);
void showAll(void) { head->show(); }
private:
HeadNode* head;
};
/*
The declaration of the LinkedList constructor.
At birth,I creat the head node.
It creates the tail node.
So an empty list points to head which
points to the tail and has nothing between.
*/
LinkedList::LinkedList(void)
{
head = new HeadNode;
}
/*
The declaration of the insert function of the LinkedList.
Delegate.
*/
void LinkedList::insert(Data* pData)
{
head->insert(pData);
}
/*
The main function.
Test driver program
*/
int main(void)
{
int pause, val;
Data* pData = nullptr;
LinkedList ll;
// ask user to produce some values
// put the in the list
while (true)
{
cout << "What value(input 0 to stop): ";
cin >> val;
if (!val)
{
break;
}
pData = new Data(val);
ll.insert(pData);
}
// now walk the list and show the data
ll.showAll();
cout << "\n Please input any integer number to exit : ";
cin >> pause;
return 0; // ll falls out of the scope and is destroyed!
}
// This project is important for me to undstand the relevant data structure.
// by Hall_Wood
// Time :2016-08-07
/*
Demonstrates an object-oriented approach to
linked list.The list delegates to the node.
The node is an abstract data type.Three types
of nodes are used, head nodes, tail nodes and
internal nodes.Only the internal nodes hold data.
*/
/*
The data class is created to serve as an object to
hold in linked list.
*/
#include "stdafx.h"
#include <iostream>
using namespace std;
enum { kIsSmaller, kIsLarger, kIsSame };
/*
Data class to put into the linked list.
Any class in this linked list must support 2
functions: show (display the value) and compare
(return relative position).
*/
class Data
{
public:
Data(int newVal): value(newVal) {}
~Data(void) {}
int getValue(void) const { return value; }
int compare(const Data&);
void show(void) { cout << value << endl; }
private:
int value;
};
/*
Compare function is used to decide where in the list
a particular object belongs.
*/
int Data::compare(const Data& otherData)
{
if (value < otherData.getValue())
return kIsSmaller;
else if (value > otherData.getValue())
return kIsLarger;
else
return kIsSame;
}
// forward declarations
class Node;
class HeadNode;
class TailNode;
class InternalNode;
/*
ADT representing the node object in the list.
Every derived class must override insert and show.
*/
class Node
{
public:
Node(void) {}
virtual ~Node(void) {}
virtual Node* insert(Data* data) = 0;
virtual void show(void) = 0;
private:
// nothing
};
/*
This is the node that hold the actual object.
In this case the object is of type Data.
We'll see how to make this general when
we cover templates.
*/
class InternalNode :public Node
{
public:
InternalNode(Data* data, Node* next);
virtual ~InternalNode(void) { delete next;delete data; }
virtual Node* insert(Data* data);
virtual void show(void)
{
data->show();
next->show();
}// delegate!
private:
Data *data; // the data itself
Node *next; // points to the next node in the linked list
};
/*
The declaration of the function, constructor.
All the constructor of InternalNode dose is to initialize.
*/
InternalNode::InternalNode(Data* newData, Node* newNext):
data(newData),next(newNext)
{
// do nothing
}
/*
The declaration of the function insert.
The mean of the list.
When you put a new object into the list
it is passed to the node which figures out
where it gose and insert it into the list
*/
Node* InternalNode::insert(Data* otherData)
{
// is the value of the new member bigger or smaller than mine?
int result = data->compare(*otherData);
switch (result)
{
case kIsLarger: // fall through
case kIsSame: // new data comes before me
{
InternalNode* dataNode = new InternalNode(otherData, this);
return dataNode;
}
// It is bigger than I am so pass it on the next node
// and let IT handle it.
case kIsSmaller:
next = next->insert(otherData);
return this;
}
return this; // appease the compiler
}
/*
TailNode is just a sentinel.
*/
class TailNode: public Node
{
public:
TailNode(void) {}
virtual ~TailNode(void) {}
virtual Node* insert(Data* data);
virtual void show(void) {}
private:
// nothing
};
/*
The declaration of the function insert of the class
TailNode.If data comes to me, it must be inserted
before me as I'm the tail and NOTING comes after me.
*/
Node* TailNode::insert(Data* data)
{
InternalNode* dataNode = new InternalNode(data, this);
return dataNode;
}
/*
HeadNode has no data,it just points to every
beggining of the list.
*/
class HeadNode : public Node
{
public:
HeadNode(void);
virtual ~HeadNode(void) { delete next; }
virtual Node* insert(Data* data);
virtual void show(void) { next->show(); }
private:
Node* next;
};
/*
The declaration of constructor of HeadNode.
As soon as the head is created, it creats the tail.
*/
HeadNode::HeadNode(void)
{
next = new TailNode;
}
/*
The declaration of the insert function of the HeadNode.
Beacuz nothing comes before the head so just pass the
data to the next node.
*/
Node* HeadNode::insert(Data* data)
{
next = next->insert(data);
return this;
}
/*
The LinkedList class.
I get all the credit and do none of the work.
*/
class LinkedList
{
public:
LinkedList(void);
~LinkedList(void) { delete head; }
void insert(Data* data);
void showAll(void) { head->show(); }
private:
HeadNode* head;
};
/*
The declaration of the LinkedList constructor.
At birth,I creat the head node.
It creates the tail node.
So an empty list points to head which
points to the tail and has nothing between.
*/
LinkedList::LinkedList(void)
{
head = new HeadNode;
}
/*
The declaration of the insert function of the LinkedList.
Delegate.
*/
void LinkedList::insert(Data* pData)
{
head->insert(pData);
}
/*
The main function.
Test driver program
*/
int main(void)
{
int pause, val;
Data* pData = nullptr;
LinkedList ll;
// ask user to produce some values
// put the in the list
while (true)
{
cout << "What value(input 0 to stop): ";
cin >> val;
if (!val)
{
break;
}
pData = new Data(val);
ll.insert(pData);
}
// now walk the list and show the data
ll.showAll();
cout << "\n Please input any integer number to exit : ";
cin >> pause;
return 0; // ll falls out of the scope and is destroyed!
}