| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 657 人关注过本帖
标题:求教
只看楼主 加入收藏
蓝调
Rank: 1
来 自:火星
等 级:新手上路
帖 子:22
专家分:0
注 册:2007-12-19
收藏
 问题点数:0 回复次数:2 
求教
以下程序首先建立一个链表,  函数fmax( )的功能是: 求出链表所有结点中, 数据域值最大的结点的位置,  并由参数s返回给主函数. 该函数的第一参数是链表的首指针.
#include “stdio.h”
#include “alloc.h”
struct node
{ int data;
 struct node *next;
}
void fmax(struct node *head, struct node     1    )
   { struct node *p;
     p=head; *s=p;
    if( p= = NULL) return;
    while (p)
      { if(p->data >   2    ) *s=p;
       p=      3    ;
      }
    }
  void print(struct node *p)
    { while(p)
      { printf(“%5d”M p->data);
         p=p->next;
      }
     printf(“\n”);
    }
 main( )
   { struct node *h=0, *p, *p1;
     int a;
    printf(“Input data:”); scanf(“%d”,&a);
    while (a!=-1)
     { p=( struct node *)malloc(sizeof(struct node));
      p->data=a;
      if (h= =0) { h=p; p1=p;}
     }
  }
搜索更多相关主题的帖子: node struct 链表 函数 node struct 链表 函数 
2008-04-25 13:24
闪闪4521
Rank: 1
等 级:新手上路
帖 子:196
专家分:0
注 册:2007-11-30
收藏
得分:0 
你再仔细看看书吧,建立链表哪里就错了
2008-04-25 15:45
Sun_DN
Rank: 1
来 自:NEU
等 级:新手上路
帖 子:48
专家分:0
注 册:2006-4-5
收藏
得分:0 
帮你修改了一下,变动比较大
你的程序有许多问题,有时间我再仔细给你指出,先把我给你修改的附上,用Dev-C++调试通过。


//修改
//以下程序首先建立一个链表,  函数fmax( )的功能是: 求出链表所有结点中, 数据域值最大的结点的位置,  并由参数s返回给主函数. 该函数的第一参数是链表的首指针.

#include "stdio.h"
#include "alloc.h"
#include "stdlib.h"

struct node
{
  int data;
  int ad;
  struct node *next;
};

void fmax(struct node *head)   //传递链表头指针
{
  struct node *p;
  struct node *res;
  int max;            
  
  p=head;        
  max=p->data;
  res=p;                        
  while(p->next!=NULL)         //这个条件很重要,判断p是否最后一个节点,用其next,而不是其本身
  {
    if((p->data) > max)
    {
     max=p->data;
     res=p;
    }
    p=p->next;                  
  }
  printf("The max node is:%d\n",res->ad);
  printf("The value of the node is:%d\n",res->data);
}
   
int main( )
{
  struct node *h, *p, *p1;
  int length;
  int a;
  
  h=p=p1=(struct node *)malloc(sizeof(struct node));
  p->ad=0;
  printf("Enter the length:");
  scanf("%d",&length);
  printf("Input data:");
  scanf("%d",&a);
  for(int i=1;i<length;i++)   //输入数据创建链表,用while出现死循环,改用指定长度      
  {     
    p->data=a;
    p->ad=i;
    p1=new struct node;      
    p->next=p1;
    p1->next=NULL;
    p=p1;                     
    printf("Input data:");
    scanf("%d",&a);
  }
  
  fmax(h);
  
  system("pause");
  return 0;
}
2008-04-25 16:50
快速回复:求教
数据加载中...
 
   



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

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