这是一个用单链表写的大数阶乘,求各位帮忙看一下这个代码哪里不对,为什么
#include "stdafx.h"#includeusing namespace std;templateclass Link;//链表类 templateclass LinkNode{ friend Link;private: T data; LinkNode*link;};templateclass Link{public:Link() { first = 0; };~Link(); bool IsEmpty()const { return first = 0; } int Length()const; bool Find(int k, T&x); Link&Insert(int k, const T&x); Link&Change(int k, T x); Link&Delete(int k, T &x); Link&Search(const T&x)const; int OutPut();private: LinkNode*first;};//析构函数(删除链表的所有节点)templateLink::~Link(){ LinkNode*next; while (first) { next = first->link; delete first; first = next; }}//确定链表的长度templateint Link::Length()const{ LinkNode*current = first; int len = 0; while (current) { len++; current = current->link; } return len;}//在链表中查找第K个元素templatebool Link::Find(int k, T &x){ LinkNode*current = first; int index = 0; while (index < k&¤t) { current = current->link; index++; } if (current) { int x = current->data; return true; } return false;}//向链表中插入元素templateLink&Link::Insert(int k, const T&x){ LinkNode*p = first; for (int index = 1; index < k&&p; index++) p = p->link; LinkNode*y = new LinkNode; y->data = x; if (k) { y->link = p->link; p->link = y; } else { y->link = first; first = y; } return *this;} //改变链表第k个元素的值 template Link&Link::Change(int k, T x) { LinkNode*p = first; for (int index = 0; p&&index < k; index++) { p = p->link; } if (p) p->data = x; return *this; } //删除链表第k个元素 template Link&Link::Delete(int k, T&x) { if (k = 0) { first = first->link; } else LinkNode*p= first; LinkNode*q = first; for (int index = 1; index < k - 1 && q; index++) { q = q->link; p = q->link; q->link = p->link; x = p->data; delete p; return *this; } } //搜索第k个元素 template Link&Link::Search(const T&x)const{ LinkNode*current = first; int index = 1; while (current&¤t->data != x) { current = current->link; index++; } if (current) return index; return 0; } //倒序输出链表 template int Link::OutPut() { LinkNode*current = first; int index = 0; int len = 0; while (current) { len++; current = current->link; } int *arry = new int[len]; current = first; while (current) { arry[index] = current->data; current = current->link; index++;} index = index - 1; cout = 0; index--) { cout.fill('0'); cout.width(3); cout |
免责声明:本内容仅代表回答会员见解不代表天盟观点,请谨慎对待。
版权声明:作者保留权利,不代表天盟立场。
|
|
|
|