The implementation of chain List class and its simple function

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.