0

The final complete program is under below link. Thanks every one who respond me.

Completed program: http://pastebin.com/1Qrs11JE

Question:

I have an annoying issue with linked-list in C Language. I have an homework that I have to submit tomorrow. I have been trying to solve this problem since yesterday, but I couldn't. I think problem is appending new node to the linked-list, but I get the error while trying to view records in linked-list. I am facing meaningles result and thr program ends up with an error.

You can see the code in the link below which more readable or from below. Link: http://pastebin.com/Ugtue6JT

Here is my code:

#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
}
5
  • stackoverflow.com/help/mcve Commented Dec 4, 2016 at 14:36
  • Neither <conio.h> nor <malloc.h> are standard headers, btw. Commented Dec 4, 2016 at 14:36
  • @melpomene: use the [mcve] tag :) Commented Dec 4, 2016 at 14:39
  • conductor = getnode(); conductor = students; Do you understand that the first statement in the sequence is useless and only wastes memory? Or perhaps you can explain what freenode(p) is doing in a function that enters new data? There are many other problems, but for now try to remove all calls to freenode and also the call to getnode in display_all_data and see where it gets you. Commented Dec 4, 2016 at 14:59
  • Thanks for comments I had to complete another homework which has to be finished by 10:30 today, I will check when I get the pc after 8 hours class today. Commented Dec 5, 2016 at 6:28

1 Answer 1

2

After looking and checking the provided source code, there are multiple problems due to a misunderstanding of the malloc()/free() of a node in a linked-list.

  1. To add a node into a linked-list, it shall be allocated and initialize before,
  2. When using a pointer to an allocated node, neither allocate or free that pointer,
  3. A node added to a linked-list could be freed only after remove it or when the linked-list is delete.

Following those rules, here are the detected errors:

Error 1 : In the enter_new_data(), an unexpected freenode(p);.

The node p has been linked... Don't freed it.

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

Error 2 : In the display_all_data(), unexpected free/malloc of conductor.

The variable conductor is only a temporary pointer used to explored the linked-list (Not allocated).

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

Error 3 : minor error in display_all_data(), to explore all items the linked-list use node pointer instead of next pointer.

Test by ( conductor != NULL) to explore also the last item.

    while ( conductor != NULL) //->next != 0)
    {
        printf("\n%s%7d%18d%15s",conductor->name,conductor->age,
            conductor->student_no,conductor->nationality); // record
        conductor = conductor->next;
    }
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks for answer I had to complete another homework which has to be finished by 10:30 today, I will check when I get the pc after 8 hours class today. But I got that why I mustn't use free node and other points, I will feedback when I tried.
Hello again, tank you so much @J. Piquard for the perfect answer, I was thinking in wrong way with new node altough I know pointers. Anyway, it's working properly, thanks to you.
@AydınBulut, one function is missing in your source code if(opt == 2){ // removing record. Removing node is a very good practice with structure pointer and Linked-List.
yes, I am coding it write now. And I assigned code a library program most probably whish will contain linked-list and so many but I just allowed to use stdio.h :S. I hope will handle it.
I just completed and shared above in the question the final program.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.