Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/57.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C链表添加新节点问题_C_Linked List - Fatal编程技术网

C链表添加新节点问题

C链表添加新节点问题,c,linked-list,C,Linked List,最后的完整程序在下面的链接下。谢谢每一个回复我的人 已完成课程: 问题: 我对C语言中的链表有一个恼人的问题。我明天要交作业。从昨天起我就一直试图解决这个问题,但我做不到。我认为问题在于将新节点追加到链表中,但我在尝试查看链表中的记录时出错。我面对的是meaningles结果,而thr程序最终出现了错误 您可以在下面的链接中看到代码,该链接更易于阅读或从下面查看。 链接: 这是我的密码: #include <stdio.h> #include <stdlib.h> #in

最后的完整程序在下面的链接下。谢谢每一个回复我的人

已完成课程:

问题:

我对C语言中的链表有一个恼人的问题。我明天要交作业。从昨天起我就一直试图解决这个问题,但我做不到。我认为问题在于将新节点追加到链表中,但我在尝试查看链表中的记录时出错。我面对的是meaningles结果,而thr程序最终出现了错误

您可以在下面的链接中看到代码,该链接更易于阅读或从下面查看。 链接:

这是我的密码:

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <malloc.h>

// structure definition
 struct node {
char name[50];
int age;
int student_no;
char nationality[50];
struct node *next;
};
typedef struct node *NODE_T;

// function declarations
void enter_new_data();
void remove_existing_data();
void display_all_data();
void show_options();
NODE_T getnode();
void freenode(NODE_T p);
void free_buffer();
void continue_f(char message[100]);

// global variables
char ch;

// create linked-list's main holder variable
NODE_T students,conductor;

int main(){

// allocate memory for the node link-list holder
students = NULL;
//students->next = 0; // initialize next to 0 for first node of linked-list

// show the options that user has
show_options();

return 1;
}

// this function will list options that user can apply
void show_options(){

system("cls"); // clear screen

int opt,opt_bool=0;

printf("%s\n\n%s\n%s\n%s\n%s\n\n","The Options You Have","1. Add New Student's Record","2. Delete An Existing Student's Record","3. Display The List Of Students","4. Exit");


while(opt_bool != 1){
    printf("Operation:  ");
    scanf("%d",&opt);

    if(opt == 1 || opt == 2 || opt == 3 || opt == 4){
        opt_bool = 1;
    }

}

// check the operation and go to the operation
if(opt == 1){ // adding record
    enter_new_data();
} else if(opt == 2){ // removing record

} else if(opt == 3){ // displaying records
    display_all_data();
} else if(opt == 4){ // exit the program
    exit(0);
}
}

// enter new student data into linked-list
void enter_new_data(){

system("cls"); // clear screen

// get a new node
NODE_T p = getnode();

printf("You are entering a new student's record\n");

// take student's name
printf("Student's Name: ");
scanf("%s",p->name);
free_buffer();

// take student's age
printf("Student's Age: ");
scanf("%d",&p->age);
free_buffer();

// take student's number
printf("Student's Number: ");
scanf("%d",&p->student_no);
free_buffer();

// take student's nationality
printf("Student's Nationality: ");
scanf("%s",p->nationality);
free_buffer();

// set p->next next value of last node of linked-list, which is equal to 0
p->next = 0;

printf("%s, %d, %d, %s",p->name,p->age,p->student_no,p->nationality);


// if there is no any node yet, add node p as first node
if(students == NULL) {
    students = p;
} else {

    conductor = students; // assign linked-list to the conductor to traverse

    // reach the last node
    while (conductor->next != 0)
    {
        conductor = conductor->next;
    }

    conductor->next = p; // append the node p to the linked list
}

freenode(p); // set free node p

continue_f("Adding new record is done."); // ask press any key to continue

show_options(); // show options

}

// to display all data of linked list
void display_all_data(){

system("cls"); // clear screen

printf("The Student Records\n\n");

printf("%s%7s%18s%15s","STUDENT'S NAME","AGE","STUDENT NUMBER","NATIONALITY"); // captions

freenode(conductor);
conductor = getnode();
conductor = students; // assign linked-list to the conductor to traverse

if (conductor != NULL ) { /* Makes sure there is a place to start */
    // traverse untill last node
    while ( conductor->next != 0)
    {

        printf("\n%s%7d%18d%15s",conductor->name,conductor->age,conductor->student_no,conductor->nationality); // record

        conductor = conductor->next;
    }
  } else {
    printf("\n\n There is not any record yet.");
  }

  continue_f("Listing records is done."); // ask press any key to continue

  show_options(); // show options
}

// create new node
NODE_T getnode(void){
   NODE_T p;
   p = (struct node *) malloc(sizeof(struct node));
   return p;
}

// set free a node
void freenode(NODE_T p){
    free(p);
}

// clear the buffer if there are any extra data in it
void free_buffer(){
    while (getchar() != '\n') { }   
}

void continue_f(char message[100]){
     printf("\n\n%s\nPress any key to continue...",message);
     getch(); // wait for pushing any key from user
}
#包括
#包括
#包括
#包括
//结构定义
结构节点{
字符名[50];
智力年龄;
国际学生会编号;
查尔族[50];
结构节点*下一步;
};
typedef结构节点*node\T;
//函数声明
无效输入新数据();
作废删除现有的数据();
无效显示所有数据();
void show_options();
NODE_T getnode();
void freenode(NODE_T p);
void free_buffer();
void continue_f(字符消息[100]);
//全局变量
char ch;
//创建链表的主holder变量
节点学生、指挥;
int main(){
//为节点链接列表持有者分配内存
学生=空;
//students->next=0;//为链表的第一个节点在0旁边初始化
//显示用户拥有的选项
显示_选项();
返回1;
}
//此函数将列出用户可以应用的选项
void show_选项(){
系统(“cls”);//清除屏幕
int opt,opt_bool=0;
printf(“%s\n\n%s\n%s\n%s\n%s\n\n”、“您的选项”、“1.添加新学生记录”、“2.删除现有学生记录”、“3.显示学生列表”、“4.退出”);
while(opt_bool!=1){
printf(“操作:”);
scanf(“%d”和&opt);
如果(opt==1 | | opt==2 | | opt==3 | | opt==4){
opt_bool=1;
}
}
//检查操作并转到操作
如果(opt==1){//添加记录
输入新数据();
}如果(opt==2){//删除记录,则为else
}如果(opt==3){//显示记录,则为else
显示所有数据();
}否则,如果(opt==4){//退出程序
出口(0);
}
}
//将新学生数据输入到链接列表中
无效输入新数据(){
系统(“cls”);//清除屏幕
//获取新节点
NODE_T p=getnode();
printf(“您正在输入新学生的记录”);
//取学生姓名
printf(“学生姓名:”);
scanf(“%s”,p->name);
空闲缓冲区();
//以学生年龄为例
printf(“学生年龄:”);
scanf(“%d”,&p->age);
空闲缓冲区();
//记下学生的电话号码
printf(“学生编号:”);
scanf(“%d”,&p->学生编号);
空闲缓冲区();
//以学生国籍为例
printf(“学生国籍:”);
scanf(“%s”,p->国籍);
空闲缓冲区();
//设置链表最后一个节点的p->next next值,该值等于0
p->next=0;
printf(“%s,%d,%d,%s”,p->name,p->age,p->student\u no,p->national);
//如果还没有任何节点,则添加节点p作为第一个节点
如果(学生==NULL){
学生=p;
}否则{
导体=学生;//将链表分配给要遍历的导体
//到达最后一个节点
while(导体->下一步!=0)
{
导体=导体->下一步;
}
conductor->next=p;//将节点p追加到链表中
}
freenode(p);//设置自由节点p
continue_f(“添加新记录完成”);//要求按任意键继续
show_options();//显示选项
}
//显示链表的所有数据
无效显示所有数据(){
系统(“cls”);//清除屏幕
printf(“学生记录”\n\n);
printf(“%s%7s%18s%15s”、“学生姓名”、“年龄”、“学生编号”、“国籍”);//说明
自由节点(导体);
conductor=getnode();
导体=学生;//将链表分配给要遍历的导体
如果(conductor!=NULL){/*确保有一个开始的地方*/
//遍历到最后一个节点
while(导体->下一步!=0)
{
printf(“\n%s%7d%18d%15s”,指挥->姓名,指挥->年龄,指挥->学生号,指挥->国籍);//记录
导体=导体->下一步;
}
}否则{
printf(“\n\n还没有任何记录。”);
}
continue_f(“列出记录已完成”);//请按任意键继续
show_options();//显示选项
}
//创建新节点
节点\u T获取节点(无效){
节点T p;
p=(结构节点*)malloc(sizeof(结构节点));
返回p;
}
//释放一个节点
无效自由节点(节点p){
自由基(p);
}
//如果缓冲区中有任何额外数据,请清除缓冲区
无效空闲缓冲区(){
而(getchar()!='\n'{}
}
无效继续(字符消息[100]){
printf(“\n\n%s\n按任意键继续…”,消息);
getch();//等待从用户处按下任何键
}

查看并检查提供的源代码后,由于对链接列表中节点的
malloc()
/
free()
的误解,出现了多个问题

  • 要将节点添加到链表中,应在
  • 使用指向已分配节点的指针时,既不分配也不释放该指针
  • 添加到链接列表中的节点只有在删除它或删除链接列表后才能释放
  • 根据这些规则,以下是检测到的错误:

    错误1:在
    中输入新数据()
    ,出现意外的
    自由节点(p)

    节点
    p
    已链接。。。不要释放它

    错误2:在
    显示所有数据()
    中,
    导线的意外空闲/malloc

    变量
    conductor
    只是一个临时指针,用于 链接列表(未分配)

    错误3:在
    显示所有数据()
    中出现小错误,要浏览链接列表中的所有项目,请使用节点指针而不是下一个指针

    通过
    (导体!=NULL)
    进行测试,以探索最后一项


    查看并检查提供的源代码后,由于对中节点的
    malloc()
    /
    free()
    的误解,出现了多个问题
    // if there is no any node yet, add node p as first node
    if(students == NULL) {
        students = p;
    }
    else {
        conductor = students; // assign linked-list to the conductor to traverse
        // reach the last node
        while (conductor->next != NULL)
        {
            conductor = conductor->next;
        }
        conductor->next = p; // append the node p to the linked list
    }
    // NO !!!!
    freenode(p); // set free node p
    
    // NO !!!!
    freenode(conductor);
    // NO !!!!
    conductor = getnode();
    
    conductor = students; // assign linked-list to the conductor to traverse
    
    if (conductor != NULL ) { /* Makes sure there is a place to start */
    
        while ( conductor != NULL) //->next != 0)
        {
            printf("\n%s%7d%18d%15s",conductor->name,conductor->age,
                conductor->student_no,conductor->nationality); // record
            conductor = conductor->next;
        }