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

来源:互联网 发布:淘宝节日 编辑:程序博客网 时间:2024/05/08 22:15

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

Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^

题目描述

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

输入

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

输出

输出这组整数。

示例输入

812 56 4 6 55 15 33 62

示例输出

12 56 4 6 55 15 33 62


#include<stdio.h>#include<malloc.h>struct node{    int number;    struct node* next;};struct node* Create(int num){    struct node* head,*tail,*p;    head=(struct node*)malloc(sizeof(struct node));    tail=head;    for(int i=0;i<num;i++)    {        p=(struct node*)malloc(sizeof(struct node));        scanf("%d",&p->number);        p->next=NULL;        tail->next=p;        tail=p;    }    return head;};void shuchu(struct node*head){    struct node *p;    p=head->next;    while(p)    {        if(p==head->next)            printf("%d",p->number);        else            printf(" %d",p->number);        p=p->next;    }    printf("\n");}int main(){    int n;    struct node *head;    scanf("%d",&n);    head=Create(n);    shuchu(head);}


0 0
原创粉丝点击