SDUT 2117 数据结构实验之链表二:逆序建立链表

在这里插入图片描述

#include <stdio.h>
#include <stdlib.h>
typedef struct Node
{
    int data;
    struct Node *next;
}node;
node *create(int n)
{
    node *head,*p;
    int i;
    head=(node *)malloc(sizeof(node));
    head->next=NULL;
    for (i=0;i<n;i++)
    {
        p=(node *)malloc(sizeof(node));
        scanf("%d",&p->data);
        p->next=head->next;
        head->next=p;
    }
    return (head);
}
void output(node *head)
{
    node *p;
    p=head->next;
    while (p!=NULL)
    {
        printf("%d ",p->data);
        p=p->next;
    }
}
int main()
{
    int n;
    node *head;
    scanf("%d",&n);
    head=create(n);
    output(head);
    return 0;
}

猜你喜欢

转载自blog.csdn.net/Here_SDUT/article/details/102940115