标题:创建单链表运行错误
只看楼主
mhchen2010
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2012-12-25
结帖率:0
已结贴  问题点数:20 回复次数:1 
创建单链表运行错误
#include<stdlib.h>
#include<stdio.h>
struct list
{
   int data;
 struct list *next;
};
typedef struct list node;
typedef node *link;
int main()
{
 link ptr,head;
 head=NULL;
     int num,i;
     ptr=(link)malloc(sizeof(node));
     ptr=head;
     printf("please input 5 numbers==>\n");
     for(i=0;i<=4;i++)
         {
           scanf("%d",&num);
           ptr->data=num;
           ptr->next=(link)malloc(sizeof(node));
           if(i==4)ptr->next=NULL;
           else ptr=ptr->next;
         }
         ptr=head;
         while(ptr!=NULL)
               {
                 printf("The value is ==>%d\n",ptr->data);
                 ptr=ptr->next;
               }
}
搜索更多相关主题的帖子: next head include numbers please 
2012-12-25 20:32
azzbcc
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
来 自:江西财经大学
等 级:贵宾
威 望:81
帖 子:3293
专家分:12919
注 册:2012-11-4
得分:20 
程序代码:
#include<stdlib.h>
#include<stdio.h>
struct list
{
    int data;
    struct list *next;
};
typedef struct list node;
typedef node *link;
int main()
{
    link ptr,head;
    head = NULL;
    int num, i;
    ptr = (link)malloc(sizeof(node));
    head = ptr;    //写反了。。。
    printf("please input 5 numbers==>\n");
    for (i = 0;i <= 4;i++)
    {
        scanf("%d", &num);
        ptr->data = num;
        ptr->next = (link)malloc(sizeof(node));
        if(i==4)ptr->next = NULL;
        else ptr = ptr->next;
    }
    ptr = head;
    while(ptr!=NULL)
    {
        printf("The value is ==>%d\n",ptr->data);
        ptr = ptr->next;
    }
    return 0;
} 


[fly]存在即是合理[/fly]
2012-12-25 20:43



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-393452-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.115562 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved