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

Assignment 5-1: Write a program that inserts 25 random integers from 0 to 100 in

ID: 665964 • Letter: A

Question

Assignment 5-1: Write a program that inserts 25 random integers from 0 to 100 in order into a LinkedList object. The program must: (1). sort the elements, (2). then calculate the sum of the elements, and (3). calculate the floating-point average of the elements. Assignment 5-2: Write a program that creates a LinkedList object of 10 characters, then creates a second LinkedList object containing a copy of the first list, but in reverse order. Print out both LinkedList's starting with the first list in list order.

Explanation / Answer

Assignment 5.1:

#include<stdio.h>
#include<stdlib.h>

/* structure for a node */
struct node
{
   int data;
   struct node *next;
};

/* Function to insert a node at the begining of a linked lsit */
void insertAtTheBegin(struct node **start_ref, int data);

/* Function to bubble sort the given linked lsit */
void bubbleSort(struct node *start);

/* Function to swap data of two nodes a and b*/
void swap(struct node *a, struct node *b);

/* Function to print nodes in a given linked list */
void printList(struct node *start);
/* Function to calculate sum of the elements in a given linked list */
int sum(struct node *start);
/* Function to calculate avg sum of elements in linked list */
double avg(struct node *start);

int main()
{
   int arr[] = {12, 56, 2, 11, 1, 90,42,23,67,26,12, 56, 2, 11, 1, 90,42,23,67,26,12, 56, 2, 11, 1, 90,42,23,67,26,12, 56, 2, 11, 1, 90,42,23,67,26,12, 56, 2, 11, 1, 90,42,23,67,26};
   int list_size, i;

   /* start with empty linked list */
   struct node *start = NULL;

   /* Create linked list from the array arr[]. */
   for (i = 0; i< 50; i++)
       insertAtTheBegin(&start, arr[i]);

   /* print list before sorting */
   printf(" Linked list before sorting ");
   printList(start);

   /* sort the linked list */
   bubbleSort(start);

   /* print list after sorting */
   printf(" Linked list after sorting ");
   printList(start);
  
   printf(" sum of elements: %d ",sum(start));
   printf("avg of elements: %f ",avg(start));

   getchar();
   return 0;
}


/* Function to insert a node at the begining of a linked lsit */
void insertAtTheBegin(struct node **start_ref, int data)
{
   struct node *ptr1 = (struct node*)malloc(sizeof(struct node));
   ptr1->data = data;
   ptr1->next = *start_ref;
   *start_ref = ptr1;
}

/* Function to print nodes in a given linked list */
void printList(struct node *start)
{
   struct node *temp = start;
   printf(" ");
   while (temp!=NULL)
   {
       printf("%d ", temp->data);
       temp = temp->next;
   }
}

/* Bubble sort the given linked lsit */
void bubbleSort(struct node *start)
{
   int swapped, i;
   struct node *ptr1;
   struct node *lptr = NULL;

   /* Checking for empty list */
   if (ptr1 == NULL)
       return;

   do
   {
       swapped = 0;
       ptr1 = start;

       while (ptr1->next != lptr)
       {
           if (ptr1->data > ptr1->next->data)
           {
               swap(ptr1, ptr1->next);
               swapped = 1;
           }
           ptr1 = ptr1->next;
       }
       lptr = ptr1;
   }
   while (swapped);
}

/* function to swap data of two nodes a and b*/
void swap(struct node *a, struct node *b)
{
   int temp = a->data;
   a->data = b->data;
   b->data = temp;
}
int sum(struct node *start)
{ int total=0;
    struct node *temp = start;
  
   while (temp!=NULL)
   {
       total=total+(temp->data);
       temp = temp->next;
   }
   return total;
}
double avg(struct node *start){
double average=sum(start)/50;
   
   return average;
}