| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 329 人关注过本帖
标题:帮忙看下是哪的问题!
只看楼主 加入收藏
梦_启航
Rank: 1
等 级:新手上路
帖 子:9
专家分:0
注 册:2009-6-3
结帖率:0
收藏
已结贴  问题点数:20 回复次数:4 
帮忙看下是哪的问题!
#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
typedef struct NODE
{
    int value;
    struct NODE *link;
}Node;
int increse(Node **linkp,int new_value)
{
    register Node *current;
    register Node *new1;
    while((current=*linkp)!=NULL&&current->value<new_value)
        linkp=&current->link;
    new1=(Node *)malloc(sizeof(Node));
      if(new1==NULL)
      {
          printf("Get memory error!");
          return 0;
      }
      new1->value=new_value;
      new1->link=current;
      *linkp=new1;
      return 1;
}
 void main()
 {
     int n,i,temp;
     Node **linkp;
     Node *p;
     printf("Please input the number of the link:\n");
     scanf("%d",&n);
     p=(Node *)malloc(sizeof(Node));
     linkp=&p;
     for(i=0;i<n;i++)
     {
         printf("Please input the %d number\n",i+1);
         scanf("%d",&temp);
         increse(linkp,temp);
     }
     for(i=0;i<n;i++)
     {
         printf("%5d",p->value);
         p=p->link;
     }
}
搜索更多相关主题的帖子: register current include memory return 
2009-07-26 00:10
asmdaydream
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:中原
等 级:版主
威 望:13
帖 子:257
专家分:840
注 册:2009-5-10
收藏
得分:20 
通过对你的程序调试跟踪,发现你的程序应该是没有问题的
但是
你的输出却有点问题,因为你的链表是有头节点的,而且你并没有给头节点赋值
因此你输出的是有总是第一个输出头节点 等于0,
只要你将 最后输出语句 p->value 修改成  p->link->value;就没有问题了

常走夜路不怕黑 长沙PHP高薪招聘群6K+ 95926136
2009-07-26 03:13
梦_启航
Rank: 1
等 级:新手上路
帖 子:9
专家分:0
注 册:2009-6-3
收藏
得分:0 
还是不行呀。。。。老是出现“0x******”指令引用的“0x*******”内存。该内存不能为“read”。
2009-07-26 09:49
asmdaydream
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:中原
等 级:版主
威 望:13
帖 子:257
专家分:840
注 册:2009-5-10
收藏
得分:0 
Please input the number of the link:
3
Please input the 1 number
1
Please input the 2 number
2
Please input the 3 number
3
    1    2    3Press [Enter] to close the terminal ...
我已经编译过了,上面是执行的结果,如果想简单一点建议换个编译工具试一下,
如果想真正找出原因就调试吧

常走夜路不怕黑 长沙PHP高薪招聘群6K+ 95926136
2009-07-26 10:04
梦_启航
Rank: 1
等 级:新手上路
帖 子:9
专家分:0
注 册:2009-6-3
收藏
得分:0 
谢谢啦  我已经找到答案了 !
#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
typedef struct NODE
{
    int value;
    struct NODE *link;
    struct NODE *before;
}Node;
int increse(Node **linkp,int new_value)
{
    register Node *current;
    register Node *new1;
    register Node *linkla;
    while((current=*linkp)!=NULL&&current->value<new_value)
    {
        linkp=&current->link;
        linkla=current;
    }
    new1=(Node *)malloc(sizeof(Node));
      if(new1==NULL)
      {
          printf("Get memory error!");
          return 0;
      }
      new1->value=new_value;
      new1->link=current;
      *linkp=new1;
      new1->before=linkla;
      if(current!=NULL)
      current->before=new1;
      return 1;
}
 void main()
 {
     int n,i,temp;
     Node **linkp=NULL;
     Node *p=NULL;
     Node *q=NULL;
     printf("Please input the number of the link:\n");
     scanf("%d",&n);
     linkp=&p;
     for(i=0;i<n;i++)
     {
         printf("Please input the %d number:\n",i+1);
         scanf("%d",&temp);
         increse(linkp,temp);
     }
     for(i=0;i<n;i++)
     {   
         printf("%5d",p->value);
        q=p;
        p=p->link;
     }
     for(i=0;i<n;i++)
     {
         printf("%5d",q->value);
         q=q->before;
     }

}
2009-07-26 10:24
快速回复:帮忙看下是哪的问题!
数据加载中...
 
   



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

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