c - 链表-插入函数

标签 c linked-list

大家好,

我尝试准备一个带有链接列表的学生信息系统。我创建了一个名为 course 的结构。用于插入过程;我将在函数 main 中调用 void insertCourse(CourseNodePtr* cr, char* code) 。当我编译时,我看到这些错误; “insertCourse”的 arg 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 */                    

最佳答案

insertCourse 的第二个参数需要类型 *char 并且您正在传递类型 Course

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

关于c - 链表-插入函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14344787/

相关文章:

c - 通过网络发送数字时是否必须颠倒字节顺序?

c - Valgrind 问题 - 显示在主下方而不是错误位置

c - C语言中if(a,b,c,d)是什么意思

c++ - 嗨,我正在尝试制作一个从 C++ 中的链表返回子列表的函数

c++ - 使用 Linux 时出现段错误,但在 Xcode 中没有

c++ - 删除链接列表上的更多特定节点?

进程可以等待不是子进程之一的 PID 吗?

c - 危险的指针转换导致失去 const 资格

c - 'manu' 不匹配重新声明中的 switch case

java - 迭代深复制链接列表