Print linked list from end to beginning

  Question : Enter the head node of a linked list, and print out the value of each node from the end to the beginning.

  Ideas : There are two ways. One is to use the last-in-first-out feature of the stack to achieve; the other is to use recursion, which is also a stack structure in nature.

  Test case :
  1. The input linked list has only one node and multiple nodes
  2. The input linked list header pointer is NULL

  Code:

stack:

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<stack>
using namespace std;

struct ListNode
{
    int         m_nValue;
    ListNode*   m_pNext;
};

//创建链表结点
ListNode* CreateListNode(int value)
{
    ListNode* pNode = new ListNode();
    pNode->m_nValue = value;
    pNode->m_pNext = NULL;

    return pNode;
}

//链接链表结点
void ConnectListNodes(ListNode* pCurrent, ListNode* pNext)
{
    if (pCurrent == NULL)
    {
        cout << "链接出错" << endl;
        exit(1);
    }

    pCurrent->m_pNext = pNext;
}

//打印链表
void PrintListNode(ListNode* pHead)
{
    cout << "打印链表:" << endl;
    ListNode* pNode = pHead;
    while (pNode != NULL)
    {
        cout << pNode->m_nValue << " ";
        pNode = pNode->m_pNext;
    }

    cout << endl;
}

//销毁链表
void DestoryList(ListNode* pHead)
{
    ListNode* pNode = pHead;
    while (pNode != NULL)
    {
        pHead = pHead->m_pNext;
        delete pNode;
        pNode = pHead;
    }
}

void PrintListReversingly_Iteratively(ListNode* pHead)
{
    stack<ListNode*> nodes; //创建栈

    ListNode* pNode = pHead;
    while (pNode != NULL)    //把所有链表结点打入栈
    {
        nodes.push(pNode);
        pNode = pNode->m_pNext;
    }

    while (!nodes.empty())  //从栈中弹出
    {
        pNode = nodes.top();
        cout << pNode->m_nValue << " " ;
        nodes.pop();
    }
    cout << endl;
}

void test(ListNode* pHead)
{
    PrintListNode(pHead);
    PrintListReversingly_Iteratively(pHead);
}

void test1()
{
    ListNode* pNode1 = CreateListNode(1);
    ListNode* pNode2 = CreateListNode(2);
    ListNode* pNode3 = CreateListNode(3);
    ListNode* pNode4 = CreateListNode(4);
    ListNode* pNode5 = CreateListNode(5);

    ConnectListNodes(pNode1, pNode2);
    ConnectListNodes(pNode2, pNode3);
    ConnectListNodes(pNode3, pNode4);
    ConnectListNodes(pNode4, pNode5);

    test(pNode1);

    DestoryList(pNode1);
}

void test2()
{
    ListNode* pNode1 = CreateListNode(1);

    test(pNode1);
    DestoryList(pNode1);
}

void test3()
{
    test(NULL);
}

int main()
{
    test1();
    test2();
    test3();

    return 0;
}

Recursive:

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<stack>
using namespace std;

struct ListNode
{
    int         m_nValue;
    ListNode*   m_pNext;
};

//创建链表结点
ListNode* CreateListNode(int value)
{
    ListNode* pNode = new ListNode();
    pNode->m_nValue = value;
    pNode->m_pNext = NULL;

    return pNode;
}

//链接链表结点
void ConnectListNodes(ListNode* pCurrent, ListNode* pNext)
{
    if (pCurrent == NULL)
    {
        cout << "链接出错" << endl;
        exit(1);
    }

    pCurrent->m_pNext = pNext;
}

//打印链表
void PrintListNode(ListNode* pHead)
{
    cout << "打印链表:" << endl;
    ListNode* pNode = pHead;
    while (pNode != NULL)
    {
        cout << pNode->m_nValue << " ";
        pNode = pNode->m_pNext;
    }

    cout << endl;
}

//销毁链表
void DestoryList(ListNode* pHead)
{
    ListNode* pNode = pHead;
    while (pNode != NULL)
    {
        pHead = pHead->m_pNext;
        delete pNode;
        pNode = pHead;
    }
}

void PrintList_Recursion(ListNode* pHead)  
{
    if (pHead != NULL)
    {
        if (pHead->m_pNext != NULL)
        {
            PrintList_Recursion(pHead->m_pNext);  //遍历打印链表
        }

        cout << pHead->m_nValue << " ";
    }

}

void test(ListNode* pHead)
{
    PrintListNode(pHead);
    PrintList_Recursion(pHead);
    cout << endl;
}

void test1()
{
    ListNode* pNode1 = CreateListNode(1);
    ListNode* pNode2 = CreateListNode(2);
    ListNode* pNode3 = CreateListNode(3);
    ListNode* pNode4 = CreateListNode(4);
    ListNode* pNode5 = CreateListNode(5);

    ConnectListNodes(pNode1, pNode2);
    ConnectListNodes(pNode2, pNode3);
    ConnectListNodes(pNode3, pNode4);
    ConnectListNodes(pNode4, pNode5);

    test(pNode1);

    DestoryList(pNode1);
}

void test2()
{
    ListNode* pNode1 = CreateListNode(1);

    test(pNode1);
    DestoryList(pNode1);
}

void test3()
{
    test(NULL);
}

int main()
{
    test1();
    test2();
    test3();

    return 0;
}

  Although the recursive code is concise, when the linked list is very long, the function call level will be very deep, which may cause the function call stack to overflow. Code based on loops that are implemented explicitly with stacks is slightly more robust.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325161665&siteId=291194637