链表

#include <iostream>
typedef struct Node
{
    int             data;
    struct Node* pNext;
}NODE, *PNODE;

/**
 *@brief 创建链表
 */
PNODE create_list()
{
    int nLength;
    int nVal;
    std::cout << "链表长度 :" ;
    std::cin >> nLength;

    //pTail指向链表的结尾,方便数据插入
    //pHead指向链表的头结点,头结点不存储实际数据,头结点方便链表操作
    PNODE pHead, pTail;
    pHead = pTail = new NODE;
    pTail->pNext = nullptr;

    for (int i = 0; i < nLength; ++i)
    {
        std::cout << "输入数据:";
        std::cin >> nVal;
        PNODE pNew = new NODE;
        pNew->data = nVal;
        pTail->pNext = pNew;
        pNew->pNext = nullptr;
        pTail = pNew;
    }
    return pHead;
}

/**
 *@brief 遍历链表
 */
void trarver_list(PNODE pHead)
{
    PNODE pList = pHead->pNext;//pList指向实际的数据
    while (nullptr != pList)
    {
        std::cout << pList->data << " ";
        pList = pList->pNext;
    }

}

int main()
{
    PNODE pHead = nullptr;
    pHead = create_list();
    trarver_list(pHead);
    getchar();
    getchar();
    return 0;
}

现在看这些东西真想吐。。。

posted @ 2015-03-27 22:11  zzyoucan  阅读(160)  评论(0编辑  收藏  举报