zoukankan      html  css  js  c++  java
  • a linked list

    /***

    a linked list demo

    by atoman

    ***/

    #include <iostream>

    using namespace std;

    #if 1
    class nodelist{
    public:
    unsigned int uiNum;
    nodelist* pNext;
    //nodelist(unsigned int x = 0) {this->uiNum = x; this->pNext = NULL;}     //init type 1: "this->" is necessary.
    nodelist(unsigned int x = 0) : uiNum(x), pNext(NULL) {}                         //init type 2: prefered initialization way.
    };
    #else

    // struct is equal to class in most case 
    struct nodelist{ 
    unsigned int uiNum;
    nodelist* pNext;
    nodelist(unsigned int x = 0) : uiNum(x), pNext(NULL) {}
    };
    #endif

    nodelist* addlist(nodelist* pList1, nodelist* pList2)
    {
    nodelist* pHeadList = new nodelist(0);
    cout << "step 1: " << pHeadList->uiNum << endl;
    nodelist* pResultList = pHeadList;
    cout << "step 2: " << pResultList->uiNum << endl;

    unsigned int uiCarry = 0;
    while (NULL != pList1 || NULL != pList2 || 0 != uiCarry)
    {
    cout << "step 3" << endl;
    unsigned int uiSum = ((NULL != pList1) ? pList1->uiNum : 0) + ((NULL != pList2) ? pList2->uiNum : 0) + uiCarry;
    uiCarry = uiSum / 10;

    pResultList->pNext = new nodelist(uiSum % 10);
    pResultList = pResultList->pNext;

    if (NULL != pList1) pList1 = pList1->pNext;
    if (NULL != pList2) pList2 = pList2->pNext;
    }

    nodelist *pt = pHeadList->pNext;
    delete pHeadList;

    return pt;
    }

    int main()
    {
    nodelist list1(7), list2;
    list1.pNext = new nodelist(9);
    list2.pNext = new nodelist(9);

    nodelist *pResultList = addlist(&list1, &list2);

    cout << endl;
    cout << "list1.uiNum = " << list1.uiNum << endl;
    cout << "list2.uiNum = " << list2.uiNum << endl;
    cout << "Sum = ";
    while (NULL != pResultList)
    {
    cout << pResultList->uiNum;
    pResultList = pResultList->pNext;
    }
    cout << endl;
    }

    /*

    Narrow input, deep learning. We can learn more from research a single and classical model deeply.
    This is a linked list model. we can learn something from it.
    1. C++ standard. C++ is not only a language, but also a standard.
    2. C++ include C, in MOST case, "class" is equal to "struct". We can use class or struct to create a linked list.
    3. initialization type: initialize them in construct function or initialize them by initialization list. The latter is preferred. For the first way, you should not discard to use "this->" which it's very important. If not, when "x" is named "uiNum" it will happen mistake without any compile warning/error.

    g++ -std=c++11 -o print.o linkedlist.cpp
    ./print.o

    g++ -std=c++11 -o print.o linkedlist.cpp -g
    gdb print.o
    l
    b ***
    r
    n
    s
    q

    */

  • 相关阅读:
    ASP.NET Core2利用MassTransit集成RabbitMQ
    ASP.NET Core2集成Office Online Server(OWAS)实现办公文档的在线预览与编辑(支持wordexcelpptpdf等格式)
    ASP.NET Core2利用Jwt技术在服务端实现对客户端的身份认证
    net core System.Drawing is not supported on this platform.
    小程序开发之维护access_token
    net core 100个案例
    msgSystem.Drawing is not supported on this platform【netcore】
    wpf,前端动画demo,鱼眼效果
    自定义控件,重写 TextBox 实例
    TextBox输入法控制,进入输入框则启用或禁用输入法(ime),禁用后只能输入英文
  • 原文地址:https://www.cnblogs.com/atoman/p/8126943.html
Copyright © 2011-2022 走看看