zoukankan      html  css  js  c++  java
  • 5--链表输出

    /*
    题目:
    (1)首先写出单链表
    (2)对链表进行方向输出
    
    解题思路:
    (a)使用stack,后进先出的策略。
    (b)递归
    
    
    */
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <stack>
    #include <iostream>
    using namespace std;
    
    typedef struct ListNode
    {
        int m_nValue;
        struct ListNode *m_pNext;
    } lNode;
    
    void listAddNode(lNode *head)
    {
        lNode *p = head, *p_Inter = NULL;
    
        if (!(p_Inter = ((lNode *)malloc(sizeof(lNode)))))
        {
            printf("the memery is don't create it
    ");
            return;
        }
        p_Inter->m_pNext = NULL;
    
        int data;
        printf("请输入数字:
    ");
        scanf_s("%d", &data);
        p_Inter->m_nValue = data;
    
        while (p->m_pNext != NULL)
        {
            p = p->m_pNext;
        }
    
        p->m_pNext = p_Inter;
    
    }
    
    lNode* createList(lNode *head)
    {
        if (!(head = ((lNode *)malloc(sizeof(lNode)))))
        {
            printf("the memery is don't create it
    ");
            return NULL;
        }
        head->m_pNext = NULL;
        int data;
        printf("请输入数字:
    ");
        scanf_s("%d", &data);
        head->m_nValue = data;
    
    
        lNode *p = head, *p_Inter = NULL;
        char X_cin = 'Y';
        while (true)
        {
            printf("是否继续添加:N/n 
    ");
            cin >> X_cin;
    
            if (X_cin == 'y' || X_cin == 'Y')
            {
                ;
            }
            else if (X_cin == 'N' || X_cin == 'n')
            {
                return head;
            }
            else
            {
                ;
            }
    
            listAddNode(p);
    
        }
    
    }
    
    void showList(lNode *head)
    {
        if (NULL == head)
        {
            cout << "list is empty 
    " << endl;
            return;
        }
    
        lNode *p = head;
    
        while (p != NULL)
        {
            printf("%d
    ", p->m_nValue);
            p = p->m_pNext;
        }
    
    }
    
    void reversePut(lNode *point)
    {
        stack <int> stack_rev;
        
        lNode *p = point;
    
        while (p != NULL)
        {
            stack_rev.push(p->m_nValue);
            p = p->m_pNext;
        }
        while (!stack_rev.empty())
        {
            cout << stack_rev.top() << endl;
            stack_rev.pop();
        }
    }
    
    
    
    int main()
    {
        lNode *head = NULL;
    
        head = createList(head);
        showList(head);
        reversePut(head);
        
        return 0;
    }
  • 相关阅读:
    Spark中文指南(入门篇)-Spark编程模型(一)
    Scala入门学习笔记三--数组使用
    沃老师学生的成绩
    Codeforces Round #461 (Div. 2) DRobot Vacuum Cleaner
    Codeforces Round #461 (Div. 2) ABC
    Educational Codeforces Round 37 (Rated for Div. 2) ABC
    Codeforces Round #460 (Div. 2) D Substring
    Codeforces Round #460 (Div. 2) ABC
    中缀式转后缀式求表达式结果
    计算器——python正则表达式
  • 原文地址:https://www.cnblogs.com/hgonlywj/p/4842545.html
Copyright © 2011-2022 走看看