zoukankan      html  css  js  c++  java
  • 从尾到头打印链表

    #include <stack>

    #include <stdio.h>

    typedef struct ListNode

    {

     int m_nValue;

     ListNode *m_pNext;

    }ListNode;

    ListNode *createListNode(int value)

    {

     ListNode *node = new ListNode();

     node->m_nValue = value;

     node->m_pNext = NULL;

     return node;

    }

    void connectListNode(ListNode *currentNode, ListNode *nextNode)

    {

       if(currentNode == NULL)  

      {   

       printf("error");

       return;  

      }

     currentNode->m_pNext = nextNode;

    }

    void destroyList(ListNode *head) {  

      ListNode *node = head;

         if(node)

          {   

            head = head->m_pNext;

            delete node;

            node = head;

         }

    }

    void printList(ListNode *head) {

         printf("原先列表: ");

         ListNode *node = head;

         while(node)

        {

           printf("%d ", node->m_nValue);

            node = node->m_pNext;

         }

        printf(" ");

      }

    void printListReversingly(ListNode *head)

       {

          std::stack<ListNode *> nodes;

          ListNode *node = head;

         while(node != NULL)

        {   

           nodes.push(node);  

           node = node->m_pNext;

         }

     while(!nodes.empty())  

      {   

           node = nodes.top();

           printf("%d ", node->m_nValue);

          nodes.pop();

       }

    }

    void main()

    {  

    ListNode *node1 = createListNode(1);

     ListNode *node2 = createListNode(2);  

    ListNode *node3 = createListNode(3);

     ListNode *node4 = createListNode(4);

     ListNode *node5 = createListNode(5);

     connectListNode(node1, node2);

     connectListNode(node2, node3);

     connectListNode(node3, node4);

     connectListNode(node4, node5);

     printList(node1);

     printf("从尾到头打印列表: ");

     printListReversingly(node1);

     printf(" ");

     destroyList(node1);

    }

  • 相关阅读:
    Redis持久化之RDB
    linux中查看进程中的线程
    Redis客户端
    Redis之GEO
    Redis之发布订阅
    Redis之HyperLogLog
    CSP-S2020游记
    根据表名 查询 表的列,备注,类型等 SQL
    mybatis-plus的使用 ------ 入门
    IntelliJ IDEA 版本控制(svn、git) 修改文件后,所属目录的颜色也变化
  • 原文地址:https://www.cnblogs.com/hainanlinyu/p/3284582.html
Copyright © 2011-2022 走看看