in my current lab we are working with linked list. my professor wants us to crea
ID: 3815583 • Letter: I
Question
in my current lab we are working with linked list. my professor wants us to create a function to append a new node to the end of the linked list, but i dont completely understand what im doing in the function and im getting a segmentation fault. after we append we are supposed to print the queue,but earlier in the program he wants us the print the content of the struct student_record_node, so im a little confused on what the difference is. so could you please help me understand how to add a new node to the end of my linked list, and the difference between printing the content of the struct and printing the queue. P.S all the functions my professor provided so i dont have the flexiblity to add or remove a function :(
THE FILE:
5 24 Dion Wills
2 17 Ashley Jackson
4 28 Ciara Brown
1 19 Zack Rench
3 21 Robert Morrison
MY CODE:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct student_record
{
int student_id_;
int student_age_;
char first_name_[21];
char last_name_[21];
};
struct student_record_node
{
struct student_record* record_;
struct student_record_node* next_;
};
void parseFile(char* filename, struct student_record_node** head);
void printNode(struct student_record_node* node);
struct student_record_node* student_record_allocate();
void student_record_node_deallocate(struct student_record_node* node);
void sortByAge(struct student_record_node** recordsHead);
void sortById(struct student_record_node** recordsHead);
void swap(struct student_record_node** node1, struct student_record_node** node2);
void freeNodeList(struct student_record_node* head);
void appendNode(struct student_record_node* head, struct student_record_node* newNode);
void printNodeList(struct student_record_node* head);
int main (int argc, char *argv[])
{
struct student_record_node* head = NULL;
char line[25];
if(argv[1] == NULL)
{
printf("ERROR: Missing File! ");
exit(0);
}
else
{
strcpy(line, argv[1]);
parseFile(line, &head);
}
return 0;
}
void parseFile(char* filename, struct student_record_node** head)
{
struct student_record_node* temp;
int i, id, age;
char firstName[25], lastName[25], line[25];
FILE *file = fopen(filename, "r");
*head = NULL;
while(fscanf(file, "%d%d%s%s", &id, &age, firstName, lastName) != EOF)
{
temp = student_record_allocate();
temp -> record_ = calloc(1, sizeof(struct student_record));
if(temp == NULL || temp -> record_ == NULL)
{
printf("Unable to allocate memory for new node!!! ");
exit(0);
}
temp -> record_ -> student_id_ = id;
temp -> record_ -> student_age_ = age;
strcpy(temp -> record_ -> first_name_, firstName);
strcpy(temp -> record_ -> last_name_, lastName);
temp -> next_ = *head;
*head = temp;
}
printf("Before Sorting... ");
printNode(temp);
sortByAge(&temp);
printf(" After Sorting By Age... ");
printNode(temp);
sortById(&temp);
printf(" After Sorting By ID... ");
printNode(temp);
freeNodeList(temp);
appendNode(*head, temp);
fclose(file);
}
void printNode(struct student_record_node* node)
{
struct student_record_node* temp;
for(temp = node; temp != NULL; temp = temp -> next_)
{
printf("struct student_record_node: ");
printf(" Student First Name: %s ", temp -> record_ -> first_name_);
printf(" Student Last Name: %s ", temp -> record_ -> last_name_);
printf(" Student ID: %d ",temp -> record_ -> student_id_);
printf(" Student Age: %d ", temp -> record_ -> student_age_);
}
}
struct student_record_node* student_record_allocate()
{
struct student_record_node* mem = calloc(1, sizeof(struct student_record_node));
mem -> record_ = 0;
mem -> next_ = NULL;
return mem;
}
void student_record_node_deallocate(struct student_record_node* node)
{
free(node -> record_);
}
void sortByAge(struct student_record_node** recordsHead)
{
struct student_record_node* sort;
struct student_record_node* temp = NULL;
int swapped, i;
do
{
swapped = 0;
sort = *recordsHead;
while (sort -> next_ != NULL)
{
if (sort -> record_ -> student_age_ > sort -> next_ -> record_ -> student_age_)
{
swap(&sort, &sort -> next_);
swapped = 1;
}
sort = sort -> next_;
}
temp = sort;
}while(swapped);
}
void sortById(struct student_record_node** recordsHead)
{
struct student_record_node* sort;
struct student_record_node* temp = NULL;
int swapped, i;
do
{
swapped = 0;
sort = *recordsHead;
while (sort -> next_ != NULL)
{
if (sort -> record_ -> student_id_ > sort -> next_ -> record_ -> student_id_)
{
swap(&sort, &sort -> next_);
swapped = 1;
}
sort = sort -> next_;
}
temp = sort;
}while(swapped);
}
void swap(struct student_record_node** node1, struct student_record_node** node2)
{
struct student_record* temp;
struct student_record_node* n1;
struct student_record_node* n2;
n1 = *node1;
n2 = *node2;
temp = n1 -> record_ ;
n1 -> record_ = n2 -> record_ ;
n2 -> record_ = temp;
}
void freeNodeList(struct student_record_node* head)
{
struct student_record_node* freeList;
while (head != NULL)
{
freeList = head;
head = head -> next_;
student_record_node_deallocate(freeList);
free(freeList);
}
}
void appendNode(struct student_record_node* head, struct student_record_node* newNode)// FUNCTION GIVING ME TROUBLE!!!
{
struct student_record_node* temp;
newNode = calloc(1, sizeof(head));
if(newNode == NULL)
{
printf("Unable to allocate memory for new node!!! ");
exit(0);
}
newNode -> record_ = head -> record_;
newNode -> next_ = NULL;
if(head -> next_ == NULL)
{
head -> next_ = newNode;
printf("added at beginning ");
}
else
{
while(head -> next_ != NULL)
{
if(head -> next_ == NULL)
{
head -> next_ = newNode;
printf("added later ");
}
head = head -> next_;
}
}
}
void printNodeList(struct student_record_node* head)
{
}
Explanation / Answer
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct student_record
{
int student_id_;
int student_age_;
char first_name_[21];
char last_name_[21];
};
struct student_record_node
{
struct student_record* record_;
struct student_record_node* next_;
};
void parseFile(char* filename, struct student_record_node** head);
void printNode(struct student_record_node* node);
struct student_record_node* student_record_allocate();
void student_record_node_deallocate(struct student_record_node* node);
void sortByAge(struct student_record_node** recordsHead);
void sortById(struct student_record_node** recordsHead);
void swap(struct student_record_node** node1, struct student_record_node** node2);
void freeNodeList(struct student_record_node* head);
void appendNode(struct student_record_node* head, struct student_record_node* newNode);
void printNodeList(struct student_record_node* head);
int main (int argc, char *argv[])
{
struct student_record_node* head = NULL;
char line[25];
if(argv[1] == NULL)
{
printf("ERROR: Missing File! ");
exit(0);
}
else
{
strcpy(line, argv[1]);
parseFile(line, &head);
}
return 0;
}
void parseFile(char* filename, struct student_record_node** head)
{
struct student_record_node* temp;
int i, id, age;
char firstName[25], lastName[25], line[25];
FILE *file = fopen(filename, "r");
*head = NULL;
while(fscanf(file, "%d%d%s%s", &id, &age, firstName, lastName) != EOF)
{
temp = student_record_allocate();
temp -> record_ = calloc(1, sizeof(struct student_record));
if(temp == NULL || temp -> record_ == NULL)
{
printf("Unable to allocate memory for new node!!! ");
exit(0);
}
temp -> record_ -> student_id_ = id;
temp -> record_ -> student_age_ = age;
strcpy(temp -> record_ -> first_name_, firstName);
strcpy(temp -> record_ -> last_name_, lastName);
temp -> next_ = *head;
*head = temp;
}
printf("Before Sorting... ");
printNode(temp);
sortByAge(&temp);
printf(" After Sorting By Age... ");
printNode(temp);
sortById(&temp);
printf(" After Sorting By ID... ");
printNode(temp);
freeNodeList(temp);
appendNode(*head, temp);
fclose(file);
}
void printNode(struct student_record_node* node)
{
struct student_record_node* temp;
for(temp = node; temp != NULL; temp = temp -> next_)
{
printf("struct student_record_node: ");
printf(" Student First Name: %s ", temp -> record_ -> first_name_);
printf(" Student Last Name: %s ", temp -> record_ -> last_name_);
printf(" Student ID: %d ",temp -> record_ -> student_id_);
printf(" Student Age: %d ", temp -> record_ -> student_age_);
}
}
struct student_record_node* student_record_allocate()
{
struct student_record_node* mem = calloc(1, sizeof(struct student_record_node));
mem -> record_ = 0;
mem -> next_ = NULL;
return mem;
}
void student_record_node_deallocate(struct student_record_node* node)
{
free(node -> record_);
}
void sortByAge(struct student_record_node** recordsHead)
{
struct student_record_node* sort;
struct student_record_node* temp = NULL;
int swapped, i;
do
{
swapped = 0;
sort = *recordsHead;
while (sort -> next_ != NULL)
{
if (sort -> record_ -> student_age_ > sort -> next_ -> record_ -> student_age_)
{
swap(&sort, &sort -> next_);
swapped = 1;
}
sort = sort -> next_;
}
temp = sort;
}while(swapped);
}
void sortById(struct student_record_node** recordsHead)
{
struct student_record_node* sort;
struct student_record_node* temp = NULL;
int swapped, i;
do
{
swapped = 0;
sort = *recordsHead;
while (sort -> next_ != NULL)
{
if (sort -> record_ -> student_id_ > sort -> next_ -> record_ -> student_id_)
{
swap(&sort, &sort -> next_);
swapped = 1;
}
sort = sort -> next_;
}
temp = sort;
}while(swapped);
}
void swap(struct student_record_node** node1, struct student_record_node** node2)
{
struct student_record* temp;
struct student_record_node* n1;
struct student_record_node* n2;
n1 = *node1;
n2 = *node2;
temp = n1 -> record_ ;
n1 -> record_ = n2 -> record_ ;
n2 -> record_ = temp;
}
void freeNodeList(struct student_record_node* head)
{
struct student_record_node* freeList;
while (head != NULL)
{
freeList = head;
head = head -> next_;
student_record_node_deallocate(freeList);
free(freeList);
}
}
/*
void appendNode(struct student_record_node* head, struct student_record_node* newNode)// FUNCTION GIVING ME TROUBLE!!!
{
struct student_record_node* temp;
newNode = calloc(1, sizeof(head));
if(newNode == NULL)
{
printf("Unable to allocate memory for new node!!! ");
exit(0);
}
newNode -> record_ = head -> record_;
newNode -> next_ = NULL;
if(head -> next_ == NULL) //this is adding in the second position, not first
{
head -> next_ = newNode;
printf("added at beginning ");
}
else
{
while(head -> next_ != NULL)
{
if(head -> next_ == NULL) //how can this be ever true inside a while loop with a complement condition??
{
head -> next_ = newNode;
printf("added later ");
}
head = head -> next_;
}
}
}
*/
void printNodeList(struct student_record_node* head)
{
struct student_record* temp;
while (head.next_ != NULL){
temp = head.record_; //get the pointer to the record and store it for future use.
//once we have the pointer, we can use the pointer to extract student datails and print them
printf("%s %s -->", temp->first_name_, temp->last_name_);
head = head->next_;
}
printf("%s %s ", head->record_->first_name_, head->record_->last_name_);//print the last data
}
void appendNode(struct student_record_node* head, struct student_record_node* newNode)// FUNCTION GIVING ME TROUBLE!!!
{
struct student_record_node* temp;
newNode = calloc(1, sizeof(head));
if(newNode == NULL)
{
printf("Unable to allocate memory for new node!!! ");
exit(0);
}
newNode -> record_ = head -> record_;
newNode -> next_ = NULL;
// NOTE that we cannot add in the begining if head pointer is NULL
while(head -> next_ != NULL)
head = head -> next_;
head -> next_ = newNode;
}
I have change the append method and is working fine. I have commented the old apend method to mark the wrong you were doing and rectified them in a simpler way.
and regarding the peint queue method, the question is ambiguous. The probable solution your prof is expecting is list of all names in a single line.
Please let me know if you have any confusion regarding methods is updated. I shall be glad to help you.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.