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);

    }

  • 相关阅读:
    Mysql性能优化
    PHP IF判断简写
    PHP与MYSQL事务处理
    js获取select标签选中的值
    oralce 的安装以及plsql的配置的html连接
    mysql 中启动服务的命令 、登录命令、退出命令 mysql 的常用命令
    oracle 中 某个字段的长度不够的sql 语句
    随机获得id的方法
    java中解析excel 批量插入数据库
    java 调用存储过程
  • 原文地址:https://www.cnblogs.com/hainanlinyu/p/3284582.html
Copyright © 2011-2022 走看看