2013-01-15 65 views
0

好日子全部,链接列表 - 插入功能

我试着准备一个带有链表的学生信息系统。我创建了一个名为course的scturct。用于插入过程;我将在main函数中调用void insertCourse(CourseNodePtr * cr,char * code)。当我编译时,我看到了这些错误; “insertCourse”参数2的不兼容类型。功能“insertCourse”的参数太少。有没有人评论这个问题?非常感谢..

/* Function Prototypes */ 
void insertCourse(CourseNodePtr* cr, char* code); 

/* Main func starts here */ 

int main(void) 
Course course_code; 


switch (choice) { 
      case 1: 
        insertCourse(&startPtr, course_code); 
        break; 



/* Insert course function */ 
void insertCourse(CourseNodePtr* cr, char* code) 
{ 
CourseNodePtr newPtr; /* New node pointer */ 
CourseNodePtr previousPtr; /* previous node pointer in list */ 
CourseNodePtr currentPtr; /* current node pointer in list */ 

newPtr = malloc(sizeof(Course)); /* memory allocation for new node */ 

if (newPtr != NULL) { 
      printf("Pls enter the code number of the course.\n"); 
      scanf("%s", &(newPtr->code)); 
      printf("Pls enter the course name.\n"); 
      scanf("%s", &(newPtr->name)); 
      printf("Pls enter the instructor name.\n"); 
      scanf("%s", &(newPtr->instructor)); 
      printf("Pls enter the term; Spring or Fall.\n"); 
      scanf("%s", &(newPtr->term)); 
      printf("Pls enter the year.\n"); 
      scanf("%s", &(newPtr->year)); 
      newPtr->coursePtr = NULL; 

      previousPtr = NULL; 
      currentPtr = *cr; 

      while ((currentPtr != NULL) && (code > currentPtr->code)) { 
       previousPtr = currentPtr; 
       currentPtr = currentPtr->coursePtr; 
      } /* End While */ 

      if (previousPtr == NULL) { 
       newPtr->coursePtr = *cr; 
       *cr = newPtr; 
      } /* End if */ 
      else { 
       previousPtr->coursePtr = newPtr; 
       newPtr->coursePtr = currentPtr; 
      } /* End else */ 
    } /* End if */ 

    else { 
     printf(" %c could not be inserted. Memory not enough...\n", code); 
    } /* End else */ 
} /* End function insert */      

回答

0

您定义course_codeCourse

Course course_code; 

,并调用insertCourse()

insertCourse(&startPtr, course_code); 

但它的参数必须是CourseNodePtr*char*

void insertCourse(CourseNodePtr* cr, char* code) 

您传递第二个参数的类型不正确。它必须是Course

void insertCourse(CourseNodePtr* cr, Course code) 
+0

insertCourse(startPtr,字符*代码);它现在在“char”之前给出语法错误。 – Behzat

+0

'insertCourse(&startPtr,course_code);' - 这个一定不能改变 – Alex

+0

@Behzat,替换函数原型:'void insertCourse(CourseNodePtr * cr,Course code);'和函数定义与原型相同。 – Alex

0

insertCourse二PARAM预计键入*char和你逝去的类型Course

insertCourse(&startPtr, course_code); 
------------------------^ here 
+0

我尝试了insertCourse(&startPtr,char * code)in main;但它说现在“char”之前的语法错误.. – Behzat

+0

更改为'insertCourse(&startPtr,code)' –

+0

不允许更改函数,对不起大卫。我需要创建一个链接列表的课程结构。然后,我将创建一个学生结构,它将通过* coursePtr达到课程结构。我相信链表是好的,但我不知道如何从主要调用插入函数。 – Behzat