首页 > 其他 > 详细

带头结点的单链表的创建

时间:2019-02-01 22:51:16      阅读:225      评论:0      收藏:0      [点我收藏+]
#include <stdio.h>
#include <stdlib.h>
typedef struct Node
{
    int data;
    struct Node *next;
}Node;
Node* CreatList(int n)
{
    Node *head,*p,*q;
    head=(Node*)malloc(sizeof(Node));
    q=head;
    for(int i=0;i<n;i++)
    {
        p=(Node*)malloc(sizeof(Node));
        scanf("%d",&p->data);
        p->next=NULL; 
        q->next=p;
        q=p;
    }
    return head;
}
void Print(Node *head)
{
    Node *p=head->next;
    while(p!=NULL)
    {
        printf("%d\n",p->data);
        p=p->next;
    }
}
int main()
{
    int n;
    scanf("%d",&n);
    Node *p=CreatList(n);
    Print(p);
} 

 

带头结点的单链表的创建

原文:https://www.cnblogs.com/xyfs99/p/10347219.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!