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

    */

  • 相关阅读:
    【重磅】FineUIPro基础版免费,是时候和ExtJS说再见了!
    【续】抓个Firefox的小辫子,jQuery表示不背这黑锅,Chrome,Edge,IE8-11继续围观中
    FineUICore已发布,跨平台速度快(现在可申请试用)!
    【原创】抓个Firefox的小辫子,围观群众有:Chrome、Edge、IE8-11
    快了快了,你的 MacBook Pro 和 FineUICore!
    [ASP.NET Core 2.0 前方速报]Core 2.0.3 已经支持引用第三方程序集了
    [译]ASP.NET Core 2.0 区域
    [译]ASP.NET Core 2.0 视图组件
    [译]ASP.NET Core 2.0 部分视图
    [译]ASP.NET Core 2.0 布局页面
  • 原文地址:https://www.cnblogs.com/atoman/p/8126943.html
Copyright © 2011-2022 走看看