#include #include #include void Exception(int Condition, char *ErrorMsg){ if(Condition){cerr << ErrorMsg << endl; abort();} } template class Chain; template class ChainNode{ friend class Chain; private: // public: T data; ChainNode *link; }; template class Chain { public: Chain(); ~Chain(); void Clear(); //清空链表 bool IsEmpty() const {return first->link == 0;} int Length() const; T Get(int k) const; //返回第i个结点的值 int Find(const T e) const; //返回值等于e的结点的序号,无则返回0 //int Search(const T& x) const; bool Delete(int k, T &x); bool Insert(int k, const T x); void Output(ostream& out) const; private: ChainNode *first; // pointer to first node }; template Chain::Chain(){ //构造函数 first = new ChainNode; //申请连续空间,返回空间首地址 Exception(!first, "there is no space in memory"); //若申请空间失败,则程序中断 first->link = 0; } template Chain::~Chain() {// Chain destructor. Delete all nodes in chain. ChainNode *next; // next node while (first) { next = first->link; delete first; first = next; } } template void Chain::Clear() //清空链表 { ChainNode *next1, *next2; // next node next1 = first->link; while (next1) { next2 = next1->link; delete next1; next1 = next2; } first->link = 0; } template int Chain::Length() const {// Return the number of elements in the chain. ChainNode *current = first->link; int len = 0; while (current) { len++; current = current->link; } return len; } template T Chain::Get(int k) const {// Set x to the k'th element in the chain. // Return false if no k'th; return true otherwise. Exception(k<1, "Out of bounds access."); //越界、非法 ChainNode *current = first->link; int index = 1; // index of current while (index < k && current) { current = current->link; index++; } Exception(!current, "Out of bounds access."); //越界、非法 return current->data; } template int Chain::Find(const T x) const {// Locate x. Return position of x if found. // Return 0 if x not in the chain. ChainNode *current = first->link; int index = 1; // index of current while (current && current->data != x) { current = current->link; index++; } if (current) return index; return 0; } template bool Chain::Delete(int k, T &x) {// Set x to the k'th element and delete it. // Throw OutOfBounds exception if no k'th element. Exception((k < 1 || !first->link), "Out of bounds access."); // move q to k-1'th ChainNode *q = first; for (int index = 0; index < k - 1 && q->link; index++) q = q->link; Exception(!(q->link), "Out of bounds access."); // no k'th // p will eventually point to k'th node ChainNode *p = q->link; // k'th q->link = p->link; // remove from chain // save k'th element and free node p x = p->data; delete p; return true; } template bool Chain::Insert(int k, const T x) {// Insert x at the k'th element. // Throw OutOfBounds exception if no k'th element. // Pass NoMem exception if inadequate space. Exception((k <= 0), "Out of bounds access."); // p will eventually point to k-1'th node ChainNode *p = first; for (int index = 0; (index < k-1) && p; index++) // move p to k'th p = p->link; Exception((!p), "Out of bounds access."); // no k'th // insert ChainNode *y = new ChainNode; y->data = x; y->link = p->link; p->link = y; return true; } template void Chain::Output(ostream& out) const {// Insert the chain elements into the stream out. ChainNode *current; for (current = first->link; current; current = current->link) out << current->data << " "; } // overload << template ostream& operator<<(ostream& out, const Chain& x) {x.Output(out); return out;} void main(void) { try { Chain L; cout << "Length = " << L.Length() << endl; cout << "IsEmpty = " << L.IsEmpty() << endl; L.Insert(1,2);L.Insert(1,6); cout << "List is " << L << endl; cout << "IsEmpty = " << L.IsEmpty() << endl; int z; cout << "First element is " << L.Get(1) << endl; cout << "Length = " << L.Length() << endl; L.Delete(1,z); cout << "Deleted element is " << z << endl; cout << "List is " << L << endl; } catch (...) { cerr << "An exception has occurred" << endl; } }