zoukankan      html  css  js  c++  java
  • SDUT-2116_数据结构实验之链表一:顺序建立链表

    数据结构实验之链表一:顺序建立链表

    Time Limit: 1000 ms Memory Limit: 65536 KiB

    Problem Description

    输入N个整数,按照输入的顺序建立单链表存储,并遍历所建立的单链表,输出这些数据。

    Input

    第一行输入整数的个数N;
    第二行依次输入每个整数。

    Output

    输出这组整数。

    Sample Input

    8
    12 56 4 6 55 15 33 62

    Sample Output

    12 56 4 6 55 15 33 62

    Hint

    不得使用数组!

    链表的基本建立方式之一。

    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    typedef struct node
    {
        int data;
        struct node *next;
    }link;
    
    link *newlink()
    {
        link *t;
        t = (link*)malloc(sizeof(link));
        t->next = NULL;
        return t;
    }
    
    link *create(int n)
    {
        link *head,*p,*q;
        int i;
        head = newlink();
        p = head;
        for(i=0;i<n;i++)
        {
            q = newlink();
            scanf("%d",&q->data);
            q->next = NULL;
            p->next = q;
            p = q;
        }
        return head;
    }
    
    void show(link *head)
    {
        link *p;
        p = head->next;
        while(p)
        {
            if(p->next==NULL)
                printf("%d
    ",p->data);
            else
                printf("%d ",p->data);
            p = p->next;
        }
    }
    
    int main()
    {
        int n;
        link *head;
        scanf("%d",&n);
        head = create(n);
        show(head);
        return 0;
    }
    
  • 相关阅读:
    Java杂项
    JFrog Artifactory
    TestNG+Selenium
    Linux杂项
    Java
    Spring Boot
    学习ThinkPHP第一天
    linux下文件解压
    php中require_once与include_once的区别
    ubuntu下的wps office for linux
  • 原文地址:https://www.cnblogs.com/luoxiaoyi/p/9726655.html
Copyright © 2011-2022 走看看