C语言建立顺序链表:输入n,建立n位数的简单顺序链表

建立简单顺序链表,初学者简单易懂。

#include<iostream>
#include<stdio.h>
#include<malloc.h>
using namespace std;
struct student
{
    int num;
    struct student *next;
};
void creat_List(struct student *&head,int n)
{
    int i;
    student *s,*L;
    head = (student *)malloc(sizeof(student));
    L = head;
    for(i=0;i<n;i++)
    {
        s = (student *)malloc(sizeof(student));
        scanf("%d",&s->num);
        L->next = s;
        L = s;
    }
    L->next=NULL;
}
void Print_List(struct student *&head)
{
    student *L;
    L=head->next;
    while(L!=NULL)
    {
        printf("%d ",L->num);
        L=L->next;
    }
}
int main()
{
    int i,n;
    student *head;
    scanf("%d",&n);
    creat_List(head,n);
    Print_List(head);
    free(head);
    return 0;
}

Sample:

5

1 4 2 6 9

Output:

5

1 4 2 6 9

1 4 2 6 9

猜你喜欢

转载自blog.csdn.net/lizizhenglzz/article/details/82813981