• YTU 2620: B 链表操作


    2620: B 链表操作

    时间限制: 1 Sec  内存限制: 128 MB
    提交: 418  解决: 261

    题目描述

    (1)编写一个函数createlink,用来建立一个动态链表(链表中的节点个数由参数count来控制)。

    节点结构如下:

    struct Node
    {
    int data;
    Node * next; 
    };

    函数createlink的声明如下:

    Node * createlink(int count);


    (2)编写一个函数printlink,用来遍历输出一个链表。

    函数printlink的声明如下:

    void printlink(Node * head);

    (3) 在主程序中调用函数createlink来动态创建链表,然后调用printlink函数遍历输出链表节点数据。

    主程序如下:(提交时不用提交主程序)

    int main()
    {
    Node * head=NULL; 
    int n;
    cin>>n;
    head=createlink(n);
    printlink(head);
    return 0;
    }

    输入

    输入要建立链表的节点个数
    输入各个节点的数据

    输出

    输出链表中各个节点的数据

    样例输入

    4
    1 2 3 4

    样例输出

    1 2 3 4

    提示

    提交时不用提交主程序,其他都要提交


    迷失在幽谷中的鸟儿,独自飞翔在这偌大的天地间,却不知自己该飞往何方……

    #include<iostream>
    using namespace std;
    struct Node
    {
        int data;
        Node * next;
    };
    Node * createlink(int count)
    {
        Node *p=new Node;
        if(count==0)
        {
            p->next=NULL;
            return p;
        }
        cin>>p->data;
        p->next=createlink(count-1);
        return p;
    }
    void printlink(Node * head)
    {
        if(head!=NULL)cout<<head->data;
        while(head->next->next!=NULL)
        {
            cout<<" "<<head->next->data;
            head=head->next;
        }
    }
    int main()
    {
        Node * head=NULL;
        int n;
        cin>>n;
        head=createlink(n);
        printlink(head);
        return 0;
    }
    

  • 相关阅读:
    React简明学习
    react-router简明学习
    react组件生命周期
    在vue中使用css modules替代scroped
    深入理解javascript中的事件循环event-loop
    javascript中的内存管理和垃圾回收
    移动端中的陀螺仪
    基于create-react-app的再配置
    vscode常用设置
    更高效地使用搜索引擎
  • 原文地址:https://www.cnblogs.com/im0qianqian/p/5989521.html
Copyright © 2020-2023  润新知