linkedlist.h

来自「linked_list的简单实现 数据结构的经典问题」· C头文件 代码 · 共 87 行

H
87
字号
#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 two methods:
Compare
*/

class Data
{
public:
	Data(int val):myValue(val){}
	~Data(){}
	int Compare(const Data&);
	void Show(){cout<<myValue<<endl;}
private:
	int myValue;
};

class Node;
class HeadNode;
class TailNode;
class InternalNode;

class Node
{
public:
	Node()
	{}
	virtual ~Node(){}
	virtual Node* Insert(Data* theData)=0;
	virtual void Show()=0;
private:
};

//This is the node which holds the actual object
//In this case the object is of type Data
//We'll see how to make this more general when

class InternalNode: public Node
{
public:
	InternalNode(Data* theData,Node* next);
	~InternalNode(){delete myNext;delete myData;}
	virtual Node* Insert(Data* theData);
	virtual void Show(){myData->Show();myNext->Show();}
private:
	Data* myData;//the data itself
	Node* myNext;//points to next node in the linked list
};

class TailNode: public Node
{
public:
	TailNode(){}
	~TailNode(){}
	virtual Node* Insert(Data* theData);
	virtual void Show(){}
private:
};
// head node has no data,it just points to the very beginning of the list
class HeadNode: public Node
{
public:
	HeadNode();
	~HeadNode(){delete myNext;}
	virtual Node* Insert(Data* theData);
	virtual void Show(){myNext->Show();}
private:
	Node *myNext;
};

class LinkedList
{
public:
	LinkedList();
	~LinkedList()
	{
		delete myHead;
	}
	void Insert(Data* theData);
	void ShowAll(){myHead->Show();}
private:
	HeadNode* myHead;
};

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?