| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1698 人关注过本帖
标题:单向链表哪里有问题?谢谢
只看楼主 加入收藏
captain2050
Rank: 2
等 级:论坛游民
帖 子:57
专家分:43
注 册:2016-7-15
结帖率:92.86%
收藏
 问题点数:0 回复次数:7 
单向链表哪里有问题?谢谢
程序代码:
#include<stdio.h>
#include<stdlib.h>

int main()
{
  struct new
  {
    int num;
    struct new *next;
  };
  struct new *first=NULL;
  struct new *current=NULL;
  struct new *last=NULL;

 

  first=malloc(sizeof(struct new));
  current=malloc(sizeof(struct new));
  last=malloc(sizeof(struct new));
  printf("please input the first number:");
  setbuf(stdin,NULL);
  scanf("%d",&first->num);
  current=first->next;

 

  while(1)
    {
      scanf("%d",&current->num);
      if(current->num==-1)  //输入-1表示结束
    {last->next==NULL;break;}
      last=current;
      current=malloc(sizeof(struct new));
      last->next=current;
      current->next=NULL;
    }
  current=first;
  do
    {
      printf("%d\n",current->num);
      current=current->next;
    }
  while(current);
   

}
搜索更多相关主题的帖子: struct new next NULL current 
2017-07-31 12:41
yangfrancis
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:贵宾
威 望:141
帖 子:1510
专家分:7661
注 册:2014-5-19
收藏
得分:0 
看编译信息。
2017-08-01 18:05
wp231957
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
来 自:神界
等 级:贵宾
威 望:423
帖 子:13688
专家分:53332
注 册:2012-10-18
收藏
得分:0 
有啥问题

DO IT YOURSELF !
2017-08-02 08:32
GBH1
Rank: 7Rank: 7Rank: 7
等 级:黑侠
威 望:5
帖 子:112
专家分:510
注 册:2017-6-13
收藏
得分:0 
你的代码问题很多,较为严重的为
1.new 是保留关键字,不能作为结构体名
2.分配内存必须指定地址变量类型
给你一份我自己写的,比较普通,这是一般做法:
#include<stdio.h>
#include<stdlib.h>
struct list{
    int num;
    struct list *next;
 };
 /*建议这样定义结构体,后面用起来方便
 typedef struct list{
    int num;
    struct list *next;
 }list;
 */
int main()
{
    struct list *first=NULL;
    struct list *current=NULL;
    struct list *last=NULL;
    first=(struct list*)malloc(sizeof(struct list));
    last = first; //初始化,第一个节点也是最后一个节点
    int x;  // 设元素类型为整型
    current=(struct list*)malloc(sizeof(struct list));
    printf("please input the first number:");
    setbuf(stdin,NULL);//这一句可以不要
    scanf ("%d", &x);  //输入结点的值
    while (x!=-1) {  //输入 -1 表示结束
        current=(struct list *)malloc(sizeof(struct list));
        current->num=x;
        last->next=current;        
        last=current;  //current指向新的表尾结点
        printf("please input the first number:");
        scanf ("%d", &x);
    }
    last->next = NULL;  //尾结点指针置空
    current = first;
    do
    {
      printf("%d\n",current->num);
      current=current->next;
    }while(current);
    return 0;
}
2017-08-04 11:47
mqh462692
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2017-8-10
收藏
得分:0 
回复 4楼 GBH1
你的输入不是从首个节点开始的,首节点是没有初始化的
2017-08-10 11:14
Racheall
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2017-8-10
收藏
得分:0 
2017-08-10 18:15
renkejun1942
Rank: 14Rank: 14Rank: 14Rank: 14
来 自:不是这样
等 级:贵宾
威 望:33
帖 子:1645
专家分:5297
注 册:2016-12-1
收藏
得分:0 
回复 4楼 GBH1
new是保留关键字?你在逗我,难道我学了假的C语言。

09:30 05/21 种下琵琶种子,能种活么?等待中……
21:50 05/27 没有发芽。
20:51 05/28 没有发芽。
23:03 05/29 没有发芽。
23:30 06/09 我有预感,要发芽了。
2017-08-10 20:57
alxwang2020
Rank: 2
等 级:论坛游民
帖 子:5
专家分:20
注 册:2017-7-12
收藏
得分:0 
不留空白行,看着累,这段代码写的相当混乱
2017-08-11 13:58
快速回复:单向链表哪里有问题?谢谢
数据加载中...
 
   



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

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