/*
LinearList.h是基类
Node.h是结点类
SingleList.h是链表类
SingleList.cpp是SingleList.h的具体实现
main.cpp是测试
*/
//LinearList.h
using namespace std;
template <typename T>
class LinearList
{
public:
LinearList()
{
;
}
~LinearList()
{
;
}
virtual bool IsEmpty() const = 0;
virtual int Length() const = 0;
virtual bool Find(int, T&)const = 0;
virtual int Search(const T&) const = 0;
virtual bool Insert(int, const T&) = 0;
virtual bool Delete(int) = 0;
virtual bool Update(int, const T&) = 0;
virtual void Output(ostream&) const = 0;
};
//Node.h
template <typename T>
class Node
{
public:
Node()
{
;
}
Node(const T &x)
{
data = x;
}
~Node()
{
;
}
T data;
Node<T> *next;
};
//SingleList.h
#include "LinearList.h"
#include "Node.h"
template <typename T>
class SingleList:public LinearList<T>
{
public:
SingleList()
{
head = new Node<T>();
head->next = NULL;
}
~SingleList()
{
if (head)
{
delete head;
}
}
bool IsEmpty() const;
int Length() const;
bool Find(int, T&)const;
int Search(const T&) const;
bool Insert(int, const T&);
bool Delete(int);
bool Update(int, const T&);
void Output(ostream&) const;
bool Append(const T&);
private:
Node<T> *head;
int length;
};
#include "SingleList.cpp"
//SingleList.cpp
#include <conio.h>
template <typename T>
bool SingleList<T>::IsEmpty() const
{
return (length == 0);
}
template <typename T>
int SingleList<T>::Length() const
{
return length;
}
template <typename T>
bool SingleList<T>::Find(int k, T &x)const
{
if (k < 0 || k > length - 1)
{
return false;
}
Node<T> *p = head->next;
int i = 0;
while ( p && (i < k) )
{
p = p->next;
++i;
}
x = p->data;
return true;
}
template <typename T>
int SingleList<T>::Search(const T &x) const
{
Node<T> *p = head->next;
int index = 0;
while (p)
{
if (p->data == x)
{
return index;
}
p = p->next;
++index;
}
return false;
}
template <typename T>
bool SingleList<T>::Insert(int k, const T &x)
{
if (k < 0 || k > length - 1)
{
return false;
}
Node<T> *p = head;
Node<T> *temp = new Node<T>();
temp->data = x;
int i = 0;
while ( (i < k) && p )
{
p = p->next;
++i;
}
temp->next = p->next;
p->next=temp;
return true;
}
template <typename T>
bool SingleList<T>::Delete(int k)
{
if (k < 0 || k > length - 1)
{
return false;
}
Node<T> *p = head;
Node<T> *temp;
int i = 0;
while (i < k)
{
p = p->next;
++i;
}
temp = p->next;
p->next = temp->next;
delete temp;
return true;
}
template <typename T>
bool SingleList<T>::Update(int k, const T &x)
{
if (k < 0 || k > length - 1)
{
return false;
}
Node<T> *p = head;
int i = 0;
while (i < k)
{
p = p->next;
++i;
}
p->next->data = x;
return true;
}
template <typename T>
void SingleList<T>::Output(ostream &os) const
{
Node<T> *p = head->next;
while (p != NULL)
{
os << p->data << " ";
p = p->next;
}
}
template <typename T>
bool SingleList<T>::Append(const T &x)
{
Node<T> *p = head;
while (p->next)
{
p = p->next;
}
Node<T> *temp = new Node<T>(x);
temp->next = NULL;
p->next = temp;
++length;
return true;
}
//main.cpp
#include <iostream>
#include "SingleList.h"
using namespace std;
int main(void)
{
SingleList<int> *sl = new SingleList<int>();
for (int i = 0; i < 10 ; ++i)
{
sl->Append(i);
}
sl->Output(cout);
cout << endl;
cout << sl->Length() << endl;
cout << sl->IsEmpty() << endl;
int temp;
cout << sl->Find(9, temp) << endl;
cout << temp << endl;
cout << sl->Search(15) << endl;
cout << sl->Insert(9, -1) << endl;
sl->Output(cout);
cout << endl;
sl->Delete(9);
sl->Output(cout);
cout << endl;
sl->Update(10 ,666);
sl->Output(cout);
cout << endl;
return 0;
}