Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Ok so in my current lab we are working with function pointers and doubly linked

ID: 3824679 • Letter: O

Question

Ok so in my current lab we are working with function pointers and doubly linked list. we must read in a file then put that information into a linked list and also printed the previous and next memory addresses of the nodes. then we must sort the information by changing the position of the nodes using a funcition pointer. Next print the queue, then free all the memory allocated. Everything seems to be working fine, but i dont know if im actually changing the position of the nodes when im sorting or how to print the previous and next memory addresses of the nodes. can you please help me understand how to sort by changing the positon of the nodes and how to print the previous and next memory addresses of the nodes. Thank you!!! P.S my professor wanted us to define most of the function that he gave us in header files so I will provide that as well as my code.

File that I am reading in:

Dion Wills 5 24
Ashley Jackson 2 17
Ciara Brown 4 28
Zack Rench 1 19
Robert Morrison 3 21

My Code:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "StudentRecord.h"
#include "StudentRecordNode.h"

void parseFile(char* filename, 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], *sort_age = "sort_age.txt", *sort_id = "sort_id.txt";

    FILE *file = fopen(filename, "r");
    if (file == NULL)
   {
   printf("Error opening file! ");
   exit(0);
   }
   *head = NULL;
  
   while(fscanf(file, "%s%s%d%d", firstName, lastName, &id, &age) != 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);
       }
       strcpy(temp -> record_ -> first_name_, firstName);
        strcpy(temp -> record_ -> last_name_, lastName);
        temp -> record_ -> student_id_ = id;
        temp -> record_ -> student_age_ = age;
       
        if(*head == NULL)
        {
            *head = temp;
        }
        else
        {  
            temp -> next_ = *head;
            *head = temp;
            //appendNode(head, temp);
        }
    }
   
    printf("Before Sorting... ");
    printNodeList(*head);
    sort(head, ageComparator);
    printf(" After Sorting by Age... ");
    printNodeList(*head);
    streamNodeList(sort_age, *head);
    sort(head, idComparator);
    printf(" After Sorting by ID... ");
    printNodeList(*head);
    streamNodeList(sort_id, *head);
    freeNodeList(*head);
}

Header File 1: StudentRecord.h

#pragma once

struct student_record
{
   int student_id_;
   int student_age_;
   char first_name_[21];
   char last_name_[21];
};

Header File 2: StudentRecordNode.h

#pragma once
#include <stdio.h>

struct student_record_node
{
   struct student_record* record_;
   struct student_record_node* next_;
struct student_record_node* prev_;
};

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;
}

int ageComparator(struct student_record_node* node1, struct student_record_node* node2)
{
   return node1 -> record_ -> student_age_ - node2 -> record_ -> student_age_;
}

int idComparator(struct student_record_node* node1, struct student_record_node* node2)
{
   return node1 -> record_ -> student_id_ - node2 -> record_ -> student_id_;
}

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 sort(struct student_record_node** recordsHead, int (*compare_fcn)(struct student_record_node*, struct student_record_node*))
{
    struct student_record_node* sort;
   struct student_record_node* temp = NULL;
   int swapped, i;

   do
   {
       swapped = 0;
       sort = *recordsHead;
       while (sort -> next_ != NULL)
{
if(compare_fcn(sort, sort -> next_) > 0)
{
   swap(&sort, &sort -> next_);
swapped = 1;
}
sort = sort -> next_;
}
temp = sort;
   }while(swapped);
}

void printNode(struct student_record_node* node)
{  
    printf("struct student_record_node: ");
printf("   Student First Name: %s ", node -> record_ -> first_name_);
printf("   Student Last Name: %s ", node -> record_ -> last_name_);
printf("   Student ID: %d ", node -> record_ -> student_id_);
printf("   Student Age: %d ", node -> record_ -> student_age_);     
}

void printNodeList(struct student_record_node* head)
{
while (head != NULL)
{
printNode(head);
head = head->next_;
    }
}

void streamNodeList(char* fileName, struct student_record_node* head)
{
   FILE* writefile = fopen(fileName, "w+");
   if (writefile == NULL)
   {
   printf("Error opening file! ");
   exit(0);
   }
   while(head != NULL)
   {
        fprintf(writefile, "struct student_record_node: ");
    fprintf(writefile, "   Student First Name: %s ", head -> record_ -> first_name_);
    fprintf(writefile, "   Student Last Name: %s ", head -> record_ -> last_name_);
    fprintf(writefile, "   Student ID: %d ", head -> record_ -> student_id_);
    fprintf(writefile, "   Student Age: %d ", head -> record_ -> student_age_);
    head = head -> next_;      
}
}

void student_record_node_deallocate(struct student_record_node* node)
{
   free(node -> record_);
}

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);
}
}

Explanation / Answer

Printing of any address id easy. You can follow something like this:

printf("Address of n1 %d ",n1);
printf("Address of n2 %d ",n2);

Let us first look at the swap function which is the main crux of the sort function:

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;

}

As you can see from the last three statements, you are not just swapping the data, but you are actually swapping the nodes itself. If you were swapping the data, then the code would look something like:

temp->record_->student_id_ = n1 -> record_->student_id_ ;
n1 -> record_->student_id_ = n2 -> record_->student_id_ ;
n2 -> record_->student_id_ = temp->student_id_;

Which is not true in this case. Hence the method you are following is correct.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote