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

Given the following code Please sort the five struct birthday elements in the li

ID: 3673594 • Letter: G

Question

Given the following code Please sort the five struct birthday elements in the list by this sequence: from head to tail, the people are from old to young. You may need to come up with new functions if you need. Traverse the linked list again and output its contents to the kernel log buffer. Invoke the dmesg command to ensure the list is properly rearranged.

Hint:

======================================================================

#include <linux/init.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/list.h>
#include <linux/slab.h>

struct birthday {
   char *name;
   int day;
   int month;
   int year;
   struct list_head list;
}birthday;

LIST_HEAD(birthday_list);

int birthdayList_init(void)
{
   printk(KERN_INFO "Loading Module... ");

   struct birthday *person;

   person = kmalloc(sizeof(*person), GFP_KERNEL);
   person->name = "Alice";
   person->day = 9;
   person->month = 1;
   person->year = 1999;

   INIT_LIST_HEAD(&person->list);
   list_add_tail(&person->list, &birthday_list);

   person = kmalloc(sizeof(*person), GFP_KERNEL);
   person->name = "Bob";
   person->day = 8;
   person->month = 3;
   person->year = 1978;

   INIT_LIST_HEAD(&person->list);
   list_add_tail(&person->list, &birthday_list);

   person = kmalloc(sizeof(*person), GFP_KERNEL);
   person->name = "Mallory";
   person->day = 0;
   person->month = 12;
   person->year = 1958;

   INIT_LIST_HEAD(&person->list);
   list_add_tail(&person->list, &birthday_list);

   person = kmalloc(sizeof(*person), GFP_KERNEL);
   person->name = "Nancy";
   person->day = 9;
   person->month = 6;
   person->year = 2003;

   INIT_LIST_HEAD(&person->list);
   list_add_tail(&person->list, &birthday_list);

   person = kmalloc(sizeof(*person), GFP_KERNEL);
   person->name = "Katie";
   person->day = 8;
   person->month = 3;
   person->year = 1978;

   INIT_LIST_HEAD(&person->list);
   list_add_tail(&person->list, &birthday_list);

   printk(KERN_INFO "List Out of Order... ");

   struct birthday *ptr;
   list_for_each_entry(ptr, &birthday_list, list)
   {
       printk(KERN_INFO "Birthday : Name: %s, Day: %d, Month: %d, Year: %d ",
           ptr->name, ptr->day, ptr->month, ptr->year);
   }

   printk(KERN_INFO "Done... ");

   return 0;
}


void birthdayList_exit(void)
{
   printk(KERN_INFO "Removing Module... ");

   struct birthday *ptr, *nxt;
   list_for_each_entry_safe(ptr, nxt, &birthday_list, list)
   {
       printk(KERN_INFO "Deleting : Name: %s, Day: %d, Month: %d, Year: %d ",
           ptr->name, ptr->day, ptr->month, ptr->year);
       list_del(&ptr->list);
       kfree(ptr);
   }

   printk(KERN_INFO "Memory free... ");
}

module_init(birthdayList_init);
module_exit(birthdayList_exit);

MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("Birthday List - CSC 212");
MODULE_AUTHOR("Horris McDavey");

Explanation / Answer

void print_sorted ( struct test a[], int n );

int main ( void )

{

  struct test a[] = { // assign struct element in an array

    {"alice", 9, 1, 1999},

    {"Bob", 9, 3, 1978},

    {"Malory", 10, 12, 1958},

    {"Nancy", 9, 6, 2003}

{'Katie", 9, 8, 1993}

  };

  print_sorted ( a,5 );

  return 0;

}

void print_sorted ( struct test a[], int n )

{

  int i, j, save;

  int *index = malloc ( n * sizeof *index );

  if ( index == NULL )

    return;

  for ( i = 0; i < n; i++ )

    index[i] = i;

  for ( i = n - 1; i > 0; i-- ) {

    int swapped = 0;

    for ( j = 0; j < i; j++ ) {

      if ( strcmp ( a[index[j]].year, a[index[j + 1]].year ) > 0 ) {

        save = index[j];

        index[j] = index[j + 1];

        index[j + 1] = save;

        swapped = 1;

      }

    }

    if ( !swapped )

      break;

  }

  for ( i = 0; i < n; i++ )

    printf ( "%s : %d %d %d ", a[index[i]].name a[index[i]].date a[index[i]].month  a[index[i]].year );

  free ( index );

}

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