首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

;c链表里的一个例子 帮看看错哪了

2013-08-09 
求助;c链表里的一个例子 帮看看哪里错了程序代码:# includestdio.h# includestdlib.h# includemalloc

求助;c链表里的一个例子 帮看看哪里错了
程序代码:
# include<stdio.h>
# include<stdlib.h>
# include<malloc.h>
/* # define NULL 0*/
# define LEN sizeof(struct student)
struct student
{
  long num;
  float score;
  struct student * next;
};
int n;

struct student * creat(void)
{
  struct student * head, * p1, * p2;
  p1 = p2 = (struct student *)malloc(LEN);
  p1 = head;
  n = 0;
  printf("please inputs the recodes:\n");
  scanf("%ld,%f",p1->num, p1->score);
  while(p1->num != 0)
  {
    n = n+1;
    if(n==1)
    {
      head = p1;
    }
    else
    {
      p2->next = p1;
      p2 = p1;
      p1 = (struct student *)malloc(LEN);
      scanf("%ld,%f",p1->num, p1->score);
    }
  p2->next = NULL;
  return(head);
}

void print(struct student * head)
{
  struct student * p;
  printf("\nThese %d records are:\n",n);
  p = head;
  if(head == NULL)
    printf("There is no record.\n");
  else
  {
    while(p != NULL) 
    {
      printf("%ld %5.1f\n",p->num, p->score);
      p = p->next;
    }
  }
  /*return(0);*/
}
?
int main()

  struct student * creat(void);
  void print(struct student * head);
  struct student * head;
  head = creat();
  print(head);
  return(0);
}

--------------------Configuration: SingleLinkedList - Win32 Debug--------------------
Compiling...
SingleLinkedList.c
E:\code\Ctest\SingleLinkedList.c(42) : error C2143: syntax error : missing ';' before 'type'


E:\code\Ctest\SingleLinkedList.c(46) : error C2065: 'p' : undeclared identifier
E:\code\Ctest\SingleLinkedList.c(46) : warning C4047: '=' : 'int ' differs in levels of indirection from 'struct student *'
E:\code\Ctest\SingleLinkedList.c(51) : warning C4047: '!=' : 'int ' differs in levels of indirection from 'void *'
E:\code\Ctest\SingleLinkedList.c(53) : error C2223: left of '->num' must point to struct/union
E:\code\Ctest\SingleLinkedList.c(53) : error C2223: left of '->score' must point to struct/union
E:\code\Ctest\SingleLinkedList.c(54) : error C2223: left of '->next' must point to struct/union
执行 cl.exe 时出错.

SingleLinkedList.obj - 1 error(s), 0 warning(s)


程序第46行就是定义print函数里面的p=head;这一语句,提示貌似是说p没用定义,但是前面第44行struct student * p;已经定义了啊 不知道什么原因呢?求解答 C 链表 struct p
[解决办法]
creat里的while循环少了右大括号
[解决办法]
struct student * creat(void)
{}
缺少右大括号

热点排行