说明:单链表的定义我一直都没有弄明白,没有找到一种很好的定义方式,本文采用链表节点与链表分开定义的方式,采用友元来访问,还是比较明了,思路也并不繁琐。如果有人能够给我如何定义链表的更好建议,本人非常感谢!本文虽然看起来较长,但是并不杂乱。
本实现例程的几个不足之处:
1、不能确定几个构造函数,拷贝构造函数是否正确,更不确定是否合适;
2、由于节点类中不涉及存储释放问题,因此在测试时候会发现在堆中分配的节点根本不需要自己释放,造成只见new不见delete的尴尬境地,但是链表类保证了内存并未泄漏。
3、成员函数的声明感觉也并不合适,尤其是关于常量引用参数问题以及该传入什么样的参数问题。
//-----------------------------------------//
// Single link List
// by SuperXu 2006.11
// Version: Final
//-----------------------------------------//
#include <iostream>
using namespace std;
class SingleLinkListNode
{
private:
friend class SingleLinkList;
int m_data;
SingleLinkListNode* m_next;
public:
SingleLinkListNode(int data = 0) : m_data(data), m_next(0)
{ }
~SingleLinkListNode()
{ }
/*----------------------------------------
//get the data of node
int Data() const
{
return m_data;
}
//get the next node
SingleLinkListNode& Next() const
{
return *m_next;
}
/--------------------------------------*/
};
class SingleLinkList
{
private:
SingleLinkListNode* m_head;
public:
//constructor
SingleLinkList() : m_head(new SingleLinkListNode)
{ }
//destructor
~SingleLinkList()
{
SingleLinkListNode* p = m_head;
while (m_head != NULL)
{
p = m_head->m_next;
delete m_head;
m_head = p;
}
}
//copy constructor
SingleLinkList(const SingleLinkList& rhs)
{
m_head = new SingleLinkListNode;
SingleLinkListNode* temp = rhs.m_head->m_next;
SingleLinkListNode* p = m_head;
while (temp != NULL)
{
p->m_next = new SingleLinkListNode(temp->m_data);
p = p->m_next;
temp = temp->m_next;
}
}
//assign operator
SingleLinkList& operator=(const SingleLinkList& rhs)
{
if (this == &rhs)
return *this;
delete[] m_head;
m_head = new SingleLinkListNode;
SingleLinkListNode* temp = rhs.m_head->m_next;
SingleLinkListNode* p = m_head;
while (temp != NULL)
{
p->m_next = new SingleLinkListNode(temp->m_data);
p = p->m_next;
temp = temp->m_next;
}
return *this;
}
bool isEmpty() const
{
return (m_head->m_next == 0);
}
//get the length of the linklist
int Length() const
{
int len = 0;
SingleLinkListNode* temp = m_head;
while (temp->m_next != 0)
{
++len;
temp = temp->m_next;
}
return len;
}
//append a node into the link list at tail
void Append(SingleLinkListNode* node)
{
SingleLinkListNode* p = m_head;
while (p->m_next != NULL)
p = p->m_next;
p->m_next = node;
}
//append a data into the link list at tail
void Append(int data)
{
SingleLinkListNode* dataNode = new SingleLinkListNode(data);
SingleLinkListNode* p = m_head;
while (p->m_next != NULL)
p = p->m_next;
p->m_next = dataNode;
}
//insert a node into the link list
void Insert(SingleLinkListNode* node, int location)
{
int i = 0;
SingleLinkListNode* p = m_head;
while (i<location)
{
p = p->m_next;
++i;
}
node->m_next = p->m_next;
p->m_next = node;
}
//insert a data into the link list, need to create node itself
void Insert(int data, int location)
{
SingleLinkListNode* dataNode = new SingleLinkListNode(data);
SingleLinkListNode* p = m_head;
int i = 0;
while (i<location)
{
p = p->m_next;
++i;
}
dataNode->m_next = p->m_next;
p->m_next = dataNode;
}
/*---------------------------------------------
//delete a node from the link list
void Delete(SingleLinkListNode* node)
{
SingleLinkListNode* p = m_head;
while (p->m_next != node)
{
cout << p->m_data << endl;
p = p->m_next;
}
cout << p->m_next->m_data << endl;
if (p->m_next == NULL)
cout << "No node in the list: " << node->m_data << endl;
else
{
SingleLinkListNode* temp = p->m_next;
p->m_next = p->m_next->m_next;
delete temp;
}
}
-----------------------------------------------*/
//delete all element from the link list whose data equal to "data"
void Delete(int data)
{
SingleLinkListNode* p = m_head;
while (p->m_next != NULL)
{
while ((p->m_next != NULL) && (p->m_next->m_data != data))
p = p->m_next;
if (p->m_next == NULL)
break;
else
{
SingleLinkListNode* temp = p->m_next;
p->m_next = p->m_next->m_next;
delete temp;
}
p = p->m_next;
}
}
//locate the special node(first one) location(start with 1) whose data equal to "data"
//question: perhaps it should be get the Node, not the location
//problem: if no node in the list, error occurs
int Locate(const int& data) const
{
int i = 1;
SingleLinkListNode* p = m_head->m_next;
while (p->m_data != data)
{
p = p->m_next;
++i;
}
if (p == NULL)
return 0;
return i;
}
//display all the node of the link list
void DisplayList() const
{
SingleLinkListNode* temp = m_head->m_next;
while (temp != 0)
{
cout << temp->m_data << " ";
temp = temp->m_next;
}
cout << endl;
}
};