2016-12-04 80 views
0

最终的完整程序位于下面的链接下。感谢每一位回应我的人。C链接列表添加新节点问题

完成的程序:http://pastebin.com/1Qrs11JE

问:

我有一个链表在C语言中一个恼人的问题。我有明天必须提交的作业。自昨天以来我一直在试图解决这个问题,但我不能。我认为问题是将新节点附加到链接列表,但在尝试查看链接列表中的记录时出现错误。我面临着结果和thr程序结束了一个错误。

您可以在下面的链接中看到更易读或更低的代码。 链接:http://pastebin.com/Ugtue6JT

这里是我的代码:

#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 
} 
+0

http:// stackoverflow。com/help/mcve – melpomene

+0

”和“”都不是标准标题,而是btw。 – melpomene

+0

@melpomene:使用'[mcve]'标签:) –

回答

2

寻找和检查提供的源代码后,存在多个问题,由于节点的malloc()/free()的链表的误解。

  1. 添加节点到一个链表,它应分配和初始化之前,
  2. 当使用一个指针分配节点,无论分配或释放该指针,
  3. 的节点添加到只有在删除链接列表或链接列表被删除后才能释放链接列表。

按照这些规则,这里检测到的错误:

错误1:在enter_new_data(),一个意想不到的freenode(p);

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

// 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 

错误2:在display_all_data(),的conductor意想不到免费/ malloc的。

可变conductor是仅用于探索 链接表一暂时指针(未分配)。

// 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 */ 

错误3:在display_all_data()小错误,探索所有项目的链接列表使用节点指针,而不是下一个指针。

测试通过(conductor != NULL)也探索的最后一个项目。

while (conductor != NULL) //->next != 0) 
    { 
     printf("\n%s%7d%18d%15s",conductor->name,conductor->age, 
      conductor->student_no,conductor->nationality); // record 
     conductor = conductor->next; 
    } 
+0

感谢您的回答我不得不完成今天10:30之前完成的另一项作业,我会在今天上课8小时后检查我的电脑。但是我明白了为什么我不能使用空闲节点和其他点,我会在尝试时反馈。 –

+0

你好,坦克你这么多@J。 Piquard为完美的答案,我正在用错误的方式思考新节点,尽管我知道指针。无论如何,感谢你,它工作正常。 –

+0

@AydınBulut,你的源代码中缺少一个函数'if(opt == 2){// remove record'。删除节点是结构指针和链接列表的一个非常好的做法。 –