首页上一页 1 下一页尾页 1 条记录 1/1页
如何显示链表包含多少个节点
发表在C语言答疑区
2017-03-21
是否精华
是
否
版块置顶:
是
否
#include <iostream> enum { kIsSmaller, kIsLarger, kIsSame }; class Data { public: Data(int newVal):value(newVal) {} ~Data() {} int compare(const Data&); void show() { std::cout << value << "\n"; } private: int value; }; int Data::compare(const Data& otherData) { if (value < otherData.value) return kIsSmaller; if (value > otherData.value) return kIsLarger; else return kIsSame; } class Node; class HeadNode; class TailNode; class InternalNode; class Node { public: Node() {} virtual ~Node() {} virtual Node* insert(Data* data) = 0; virtual void show() = 0; private: }; class InternalNode : public Node { public: InternalNode(Data* data, Node* next); virtual ~InternalNode() { delete next; delete data; } virtual Node* insert(Data* data); virtual void show() { data->show(); next->show(); } // delegate! private: Data* data; // the data itself Node* next; // points to next node in the linked list }; InternalNode::InternalNode(Data* newData, Node* newNext): data(newData), next(newNext) { } Node* InternalNode::insert(Data* otherData) { // is the new guy bigger or smaller than me? int result = data->compare(*otherData); switch(result) { // by convention if it is the same as me it comes first case kIsSame: // fall through case kIsLarger: // new data comes before me { InternalNode* dataNode = new InternalNode(otherData, this); return dataNode; } case kIsSmaller: next = next->insert(otherData); return this; } return this; // appease the compiler } class TailNode : public Node { public: TailNode() {} virtual ~TailNode() {} virtual Node* insert(Data* data); virtual void show() {} private: }; Node* TailNode::insert(Data* data) { InternalNode* dataNode = new InternalNode(data, this); return dataNode; } class HeadNode : public Node { public: HeadNode(); virtual ~HeadNode() { delete next; } virtual Node* insert(Data* data); virtual void show() { next->show(); } private: Node* next; }; HeadNode::HeadNode() { next = new TailNode; } Node* HeadNode::insert(Data* data) { next = next->insert(data); return this; } class LinkedList { public: LinkedList(); ~LinkedList() { delete head; } void insert(Data* data); void showAll() { head->show(); } private: HeadNode* head; }; LinkedList::LinkedList() { head = new HeadNode; } void LinkedList::insert(Data* pData) { head->insert(pData); } int main() { Data* pData; int val; LinkedList ll; while (true) { std::cout << "What value (0 to stop)? "; std::cin >> val; if (!val) break; pData = new Data(val); ll.insert(pData); } ll.showAll(); return 0; // ll falls out of scope and is destroyed! }
在这段程序中添加一段程序显示链表包含多少个节点?