Today C + + teacher Let write a list of the class, which requires to get a node class, expressing deep inadequacy. At night, when the boredom of the time to think of this problem, feel still can, knocked down.
First the sub-block introduction:
Node class
class node//节点类{public: double data; node *next;};
Linked List classes:
class Link//;链表类{public: Link()//构造函数 { pfront=NULL; pend=NULL; } double Getx(int n);//找出第n个数字 void add(double x);//添加函数 ~Link();//析构函数private: node *pfront,*pend;};
The following is the total code:
/ * Roughly implemented two functions, adding numbers to the list, finding the nth number in the list, and slowly updating it without considering the cross-border access.#include <iostream>using namespace STD;classNode//Node class{ Public:DoubleData node *next;};classLink//; Chain List class{ Public: Link ()//Constructors{pfront=null; Pend=null; }DoubleGetx (intn);//Find nth number voidAddDoublex);//Add function~link ();//destructorPrivate: node *pfront,*pend;};voidLink::add (Doublex) {node *p; p=NewNodeif(Pfront==null&&pend==null) {pfront=pend=p; pfront->data=x; pend->data=x; pend->next=null; }Else{pend->next=p; Pend=p; pend->data=x; pend->next=null; }}DoubleLink::getx (intN) {node *p; p=NewNode P=pfront; for(intI=1; i<n;i++) {if(p->next!=null) p=p->next; }returnP->data;} Link::~link () {node *p; while(Pfront->next!=null) {p=pfront->next;Delete(Pfront); }}intMain () {Link L; L.add (1.1); L.add (20.5);cout<<l.getx (1) <<endl;cout<<l.getx (2) <<endl;return 0;}
The implementation of chain List class and its simple function