赞
踩
来自:happcock论坛
节点类
#ifndef Node_H #define Node_H template class Node //单链节点类 { public: Type data; Node *link; Node() : data(Type()), link(NULL) {} Node(const Type &item) : data(item), link(NULL) {} Node(const Type &item, Node *p) : data(item), link(p) {} }; #endif |
#ifndef List_H #define List_H #ifndef TURE #define TURE 1 #endif #ifndef FALSE #define FALSE 0 #endif typedef int BOOL; #include "Node.h" template class List //单链表定义 { //基本上无参数的成员函数操作的都是当前节点,即current指的节点 //认为表中“第1个节点”是第0个节点,请注意,即表长为1时,最后一个节点是第0个节点 public: List() { first = current = last = new Node ; prior = NULL; } ~List() { MakeEmpty(); delete first; } void MakeEmpty() //置空表 { Node *q; while (first->link != NULL) { q = first->link; first->link = q->link; delete q; } Initialize(); } BOOL IsEmpty() { if (first->link == NULL) { Initialize(); return TURE; } else return FALSE; } int Length() const //计算带表头节点的单链表长度 { Node *p = first->link; int count = 0; while (p != NULL) { p = p->link; count++; } return count; } Type *Get()//返回当前节点的数据域的地址 { if (current != NULL) return ¤t->data; else return NULL; } BOOL Put(Type const &value)//改变当前节点的data,使其为value { if (current != NULL) { current->data = value; return TURE; } else return FALSE; } Type *GetNext()//返回当前节点的下一个节点的数据域的地址,不改变current { if (current->link != NULL) return ¤t->link->data; else return NULL; } Type *Next()//移动current到下一个节点,返回节点数据域的地址 { if (current != NULL && current->link != NULL) { prior = current; current = current->link; return ¤t->data; } else { return NULL; } } void Insert(const Type &value)//在当前节点的后面插入节点,不改变current { Node *p = new Node (value, current->link); current->link = p; } BOOL InsertBefore(const Type &value)//在当前节点的前面插入一节点,不改变current,改变prior { Node *p = new Node (value); if (prior != NULL) { p->link = current; prior->link = p; prior = p; return TURE; } else return FALSE; } BOOL Locate(int i)//移动current到第i个节点 { if (i <= -1) return FALSE; current = first->link; for (int j = 0; current != NULL && j < i; j++, current = current->link) prior = current; if (current != NULL) return TURE; else return FALSE; } void First()//移动current到表头 { current = first; prior = NULL; } void End()//移动current到表尾 { if (last->link != NULL) { for ( ;current->link != NULL; current = current->link) prior = current; last = current; } current = last; } BOOL Find(const Type &value)//移动current到数据等于value的节点 { if (IsEmpty()) return FALSE; for (current = first->link, prior = first; current != NULL && current->data != value; current = current->link) prior = current; if (current != NULL) return TURE; else return FALSE; } BOOL Remove()//删除当前节点,current指向下一个节点,如果current在表尾,执行后current = NULL { if (current != NULL && prior != NULL) { Node *p = current; prior->link = p->link; current = p->link; delete p; return TURE; } else return FALSE; } BOOL RemoveAfter()//删除当前节点的下一个节点,不改变current { if (current->link != NULL && current != NULL) { Node *p = current->link; current->link = p->link; delete p; return TURE; } else return FALSE; } friend ostream & operator << (ostream & strm, List &l) { l.First(); while (l.current->link != NULL) strm << *l.Next() << " " ; strm << endl; l.First(); return strm; } protected: /*主要是为了高效的入队算法所添加的。因为Insert(),Remove(),RemoveAfter()有可能改变last但没有改变last所以这个算法如果在public里除非不使用这些,否则不正确。但是last除了在队列中非常有用外,其他的时候很少用到,没有必要为了这个用途而降低Insert(),Remove()的效率所以把这部分放到protected,实际上主要是为了给队列继承*/ void LastInsert(const Type &value) { Node *p = new Node (value, last->link); last->link = p; last = p; } void Initialize()//当表为空表时使指针复位 { current = last = first; prior = NULL; } //这部分函数返回类型为Node 指针,是扩展List功能的接口 Node *pGet() { return current; } Node *pNext() { prior = current; current = current->link; return current; } Node *pGetNext() { return current->link; } Node *pGetFirst() { return first; } Node *pGetLast() { return last; } Node *pGetPrior() { return prior; } void PutLast(Node *p) { last = p; } //这部分插入删除函数不建立或删除节点,是原位操作的接口 void Insert(Node *p) { p->link = current->link; current->link = p; } void InsertBefore(Node *p) { p->link = current; prior->link = p; prior = p; } void LastInsert(Node *p) { p->link = NULL; last->link = p; last = p; } Node *pRemove() { if (current != NULL && prior != NULL) { Node *p = current; prior->link = current->link; current = current->link; return p; } else return NULL; } Node *pRemoveAfter() { if (current->link != NULL && current != NULL) { Node *p = current->link; current->link = current->link->link; return p; } else return NULL; } private: List(const List &l); Node *first, *current, *prior, *last; //尽量不要使用last,如果非要使用先用End()使指针last正确 }; #endif |
单链表应用
有人曾经建议最好把链表和链表位置这两个分开,C++标准库是这么做的;但对于初学者来说,一个类总比两个类好操作。我不清楚在书中这部分的程序究竟调没调试,但这种语句我是绝对看不懂的:
ListNode
*pa, *pb, *pc, *p;
ListIterator Aiter(ah.poly); ListIterator Biter(ah.poly); pa = pc = Aiter.First(); pb = p = Biter.First(); ……………………….. pa->coef = pa->coef + pb->coef; p = pb; pb = Biter.Next(); delete p; |
#ifndef Polynomial_H #define Polynomial_H #include "List.h" class Term { public: int coef; int exp; Term() : coef(0), exp(0) {} Term(int c, int e) : coef(c), exp(e) {} Term(int c) : coef(c), exp(0) {} }; class Polynomial : List { public: void Input() { cout << endl << "输入多项式的各项系数和指数"; cout << endl << "注意:请按降序输入各项,输入系数0表示结束" << endl; int coef, exp; for(int i = 1; ; i++) { cout << "第" << i << "项的系数:"; cin >> coef; if (coef) { cout << "指数:"; cin >> exp; Term term(coef, exp); Insert(term); } else break; } } void Print() { cout << endl; First(); if (!IsEmpty()) { Term *p = Next(); cout << p->coef; if (p->exp) { cout << "x"; if (p->exp != 1) cout << "^" << p->exp; } while (Next() != NULL) { p = Get(); if (p->coef > 0) cout << "+"; cout << p->coef; if (p->exp) { cout << "x"; if (p->exp != 1) cout << "^" << p->exp; } } } cout << endl; } friend void PolyAdd (Polynomial &polyA, Polynomial &polyB) { Node *pA, *pB; polyA.First();polyB.First(); pA = polyA.pNext();pB = polyB.pNext(); while (pA != NULL && pB !=NULL) { if (pA->data.exp == pB->data.exp) { pA->data.coef = pA->data.coef + pB->data.coef; polyB.Remove(); if (!pA->data.coef) polyA.Remove(); else polyA.pNext(); } else { if (pA->data.exp > pB->data.exp) { polyB.pRemove(); polyA.InsertBefore(pB); } else if (pA->data.exp < pB->data.exp) polyA.pNext(); } pA = polyA.pGet();pB = polyB.pGet(); } if (pA == NULL) { polyA.pGetPrior()->link = pB; polyB.pGetPrior()->link = NULL; } } }; #endif |
下面将完成单链表的赋值运算的重载,请把这部分加到List类的public部分。的确,这部分也可以放在多项式类里实现;但是,复制一个多项式实际上就是复制一个单链表,与其单单做一个多项式赋值,还不如完成单链表的赋值,让派生类都能共享。
operator = (const List
&l)
{ MakeEmpty(); for (Node *p = l.first->link; p != NULL; p = p->link) LastInsert(p->data); } |
List(const List
&l)
{ first = current = last = new Node ; prior = NULL; for (Node *p = l.first->link; p != NULL; p = p->link) LastInsert(p->data); } |
friend Polynomial operator + (Polynomial &polyA, Polynomial &polyB) { Polynomial tempA = polyA;Polynomial tempB = polyB; PolyAdd(tempA, tempB); return tempA; } friend Polynomial operator * (Polynomial &polyA, Polynomial &polyB) { Node *pA = polyA.pGetFirst()->link; Node *pB = polyB.pGetFirst()->link; Polynomial polyTempA, polyTempB; int coef, exp; if (pA == NULL || pB == NULL) return polyTempA; for (pA = polyA.pGetFirst()->link; pA != NULL; pA = pA->link) { for(pB = polyB.pGetFirst()->link; pB != NULL; pB = pB->link) { coef = pA->data.coef * pB->data.coef; exp = pA->data.exp + pB->data.exp; Term term(coef, exp); polyTempB.LastInsert(term); } PolyAdd(polyTempA, polyTempB); polyTempB.Initialize(); } return polyTempA; } |
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。