QuestionQuestion

• Objective
o Create a linked list of randomly generated integers. Insert 25 to 75 random integers from 0 to 100 into the linked list.
o Display the contents, the sum, the count, and average of the integers.
o How to use structures.
o How to use structures to create a linked list.
• Related SLO
o Develop properly structured multifile programs with automatic compilation.
o Implement pointers, structures and linked data structures in C.
• Instructions
o Note: There is no user input in this assignment.
o Create a makefile named makefile that will automicatlly compile, link, and create an executable for this assignment.
- Do NOT submit a makefile with any other name.
- There are no extra .h or .c needed for this assignment.
o Create a program named LastnameFirstname19.c, that does the following:
- Create a structure named node that stores an integer and a pointer to another node.
- You may use the struct definition in linkedlist.c as a template.
- You may also want to use typedef to create alias' for the node structure and node pointer for convenience.
- Setup your program to generate random numbers.
- Make the necessary includes and seed the random function.
- Generate a random integer between 25 and 75, this number will be the size of the linked list.
- For example: int size = 25 + rand() % 51;
- Build the linked list of integers in ascending order, from smallest to largest.
- Generate an integer between 0 and 100, this will be used as the data in the node.
- For example: int randomData = rand() % 101;
- Generate an integer for each node to be inserted.
- Create a function that takes in an integer and a node pointer, and inserts a node containing that integer into the linked list in ascending order.
- Create a function to display the linked list and use it to display the contents.
- Create a function that returns the sum of all the integers in the list and use it to get the sum.
- Create a function that returns the count of all the nodes in the list by looping through the linked list.
- Do NOT use the number that was generated at the beginning of the program.
- You must create a function that loops through the linked list and counts how many nodes.
- Use the sum and count to calculate the floating point average of the elements.
- Be aware of integer division!
- Display the average to 2 decimal places.
o Be sure to have a program description at the top and in-line comments.
- Be clear with your comments and output to the user, so I can understand what you're program is doing.
Example Output
• % make
• % ./program
• The list is: 0, 0, 1, 4, 5, 6, 8, 9, 12, 13, 14, 17, 19, 20, 20, 23, 24, 25, 25, 26, 28, 30, 30, 32, 34, 36, 37, 38, 43, 44, 46, 50, 53, 54, 54, 62, 62, 65, 66, 66, 68, 70, 70, 74, 79, 79, 80, 82, 83, 85, 85, 89, 96, 97,
• sum = 2338
• count = 54
• average = 43.30

• % ./program
• The list is: 1, 2, 7, 9, 15, 17, 17, 18, 18, 19, 19, 19, 20, 22, 22, 22, 22, 25, 26, 27, 27, 27, 30, 31, 35, 36, 37, 39, 40, 41, 43, 44, 47, 48, 49, 49, 52, 54, 55, 58, 58, 58, 59, 61, 61, 62, 65, 72, 74, 75, 84, 85, 85, 85, 86, 86, 86, 88, 89, 89, 90, 91, 92, 93, 95, 96, 97,
• sum = 3351
• count = 67
• average = 50.01

• % ./program
• The list is: 1, 2, 4, 10, 12, 22, 22, 23, 23, 23, 28, 38, 42, 47, 47, 48, 49, 60, 65, 65, 66, 67, 78, 79, 80, 80, 82, 86, 90, 94, 97,
• sum = 1530
• count = 31
average = 49.35
• Notes
o For testing and verifying if your display, sum, and count functions work, you may want to set the linked list to a fixed size of 5 or 10.
o As always, program in small pieces, do not code all functions. Code one function at a time and ensure they work before moving on to the next function.

Lindkedlist.c Template

#include <stdio.h>
#include <stdlib.h> // For malloc() and free().
#include <string.h>
#include "stringinput.h"

/**
* An example program of implementing a linked list in C.
* Asks the user to enter a word, and inserts the word into a linked list
* in alphabetical order, and deletes words at the user's request.
*
* Each node will store a string of length MAX.
* Default is 30 characters.
*
* The following files are needed to compile:
*   stringinput.c
*   stringinput.h
*   makefile-list
*
* To compile:
*   % make -f makefile-list
* To run:
*   % ./program
*
* @author Ed Meyer
* @original_author William Albritton
*/

#define MAX 30 // Maximum characters in the word.
#define QUIT "quit" // End the loop

/**
* A node that stores a string and points to another node.
* Stores the following attributes:
*   data: The string.
*   *next: Pointer to the next node.
*
* A self-referential structure contains a pointer to
* another structure of the same type to form a linked list.
*/
struct node {
char data[MAX];
struct node *next;
};

// Declare "new" data types.
typedef struct node Node;
typedef struct node* NodePointer;

/** Function prototypes */
void insertIntoLinkedList(char [], NodePointer *);
void displayLinkedList(NodePointer);
void deleteFromLinkedList(char [], NodePointer *);

int main() {

// Used to store each word from the user.
char word[MAX] = {'\0'};
// Pointer to the 1st node in the linked list.
NodePointer head = NULL;

// Keep inserting strings into the linked list until user enters "quit"
while (0 != strcmp(word, QUIT)) {

    // Ask for and get a word from the user.
    printf("Enter a word to INSERT into linked list (enter \"%s\" to quit): ", QUIT);
    getline(word, MAX);

    // If the user entered a string other than "quit", add it to the linked list.
    if (0 != strcmp(word, QUIT)) {
      // &head: send the address of the pointer to the 1st node in list
      insertIntoLinkedList(word, &head);
      // Show that the string was added to the list.
      displayLinkedList(head);
    }
}

// Reset word[] so the program doesn't automatically quit without
// asking the user to delete a string from the list.
strncpy(word, "don't quit", MAX);
printf("\n");

// User entered "quit" to stop inserting words move on to deleting.
// Ask the user to delete a word from the list.
// Keep deleting until user enters "quit" again
while (0 != strcmp(word, QUIT) && NULL != head) {

    // Ask for and get a word from the user.
    printf("Enter a word to DELETE from linked list (enter \"%s\" to quit): ", QUIT);
    getline(word, MAX);

    // If the user entered a string other than "quit", attempt to delete it from the list.
    if (0 != strcmp(word, QUIT)) {
      deleteFromLinkedList(word, &head);
      displayLinkedList(head);
    }
}

return 0;
}

/**
* Inserts a word in alphabetical order into a linked list.
*
* @param word2[] A string to be added to the linked list.
* @param *head2 Pointer to the 1st node in the linked list.
*               This parameter is a pointer to a pointer because
*               we are passing the address of the linked list to
*               the function, and the linked list itself is a pointer
*               to the 1st node in the list.
*/
void insertIntoLinkedList(char word2[], NodePointer *head2) {
// Pointer to a new node to be inserted into linked list
NodePointer newNode = NULL;
// Pointer to the previous node in the linked list
NodePointer previous = NULL;
// Pointer to the current node in the linked list
NodePointer current = *head2;

// Create a node on the heap
newNode = malloc(sizeof(Node));

// Check to see if space is available.
// If no space on heap, malloc() will return NULL.
if (NULL != newNode) {

    // Copy over word to the node being added to the list.
    strcpy(newNode->data, word2);
    // Figure out the insertion point of the new node to be added in
    // alphabetical order.
    while (NULL != current && strcmp(word2, current->data) > 0) {
      // Move previous to current
      previous = current;
      // Move current to next node
      current = current->next;
    }

    // Special case if the list is empty or the new node will be at the beginning,
    // set the newNode as the head of the linked list.
    if (NULL == previous) {
      *head2 = newNode;
      newNode->next = current;
    } else {
      // Otherwise, insert between previous and current.
      previous->next = newNode;
      newNode->next = current;
    }
}
}


/**
* Displays the linked list starting from a NodePointer and on.
*
* @param current A pointer to the starting node.
*/
void displayLinkedList(NodePointer current) {

// Special case, if the list is empty.
if (NULL == current) {
    printf("The linked list is empty!\n\n");
} else {

    // Otherwise, go through all nodes in the list and print them.
    printf("linked list items: ");
    while (NULL != current) {
      // Display each node.
      printf("%s, ", current->data);
      // Go to next node
      current = current->next;
    }
    printf("\n\n");
}
}

/**
* Deletes a string from a linked list.
* Does nothing if the string does not exist in the linked list.
*
* @param word3[] The string to be deleted from the list.
* @param *head3 Pointer to the 1st node in the linked list.
*/
void deleteFromLinkedList(char word3[], NodePointer *head3) {

// Pointer to a temp node to be deleted from linked list
NodePointer tempNode = NULL;
// Pointer to the previous node in the linked list
NodePointer previous = NULL;
// Pointer to the current node in the linked list
NodePointer current = *head3;

// Check for empty list.
if (NULL == current) {
    printf("Cannot delete from empty list!\n");
    return;
}

// Special case, if the node to be deleted is the first node in the list.
if (0 == strcmp(word3, current->data)) {
    // Get address of 1st node.
    tempNode = current;
    // Change head of the linked list to next node
    *head3 = current->next;
    // Delete the node that was the "old" head
    printf("deleting \"%s\" . . .\n", tempNode->data);
    free(tempNode);
} else {
    // Otherwise, go through the linked list and search for the string.
    while (NULL != current && 0 != strcmp(word3, current->data)) {
      //printf("current = %s\n", current->data); // Debug print.
      // Move to next node in linked list
      previous = current;
      current = current->next;
    }

    // Match was found is current != NULL.
    // If current did == NULL, that means the string was not found in the linked list.
    // Do nothing if the string was not found in the linked list.
    if (NULL != current) {
      // Get current node's address and store in tempNode.
      tempNode = current;
      //printf("tempNode = %s\n", tempNode->data); // Debug print.
      // Skip over current node.
      // Set the previous next to the node after the current one.
      previous->next = current->next;
      // Delete node at current pointer.
      printf("deleting \"%s\" . . .\n", tempNode->data);
      free(tempNode);
    }
}
}

Solution PreviewSolution Preview

These solutions may offer step-by-step problem-solving explanations or good writing examples that include modern styles of formatting and construction of bibliographies out of text citations and references. Students may use these solutions for personal skill-building and practice. Unethical use is strictly forbidden.

/**
* A node that stores an integer and points to another node.
* Stores the following attributes:
*   data: The integer.
*   *next: Pointer to the next node.
*
* A self-referential structure contains a pointer to
* another structure of the same type to form a linked list.
*/
struct node {
int data;
struct node *next;
};

// Declare "new" data types.
typedef struct node Node;
typedef struct node* NodePointer;

/** Function prototypes */
void InsertNode(NodePointer *np, int data);
void DisplayLinkedList(NodePointer np);
int SumOfLinkedList(NodePointer np);
int CountLinkedList(NodePointer np);

int main(int argc, char** argv)
{

    // Pointer to the 1st node in the linked list.
    NodePointer head = NULL;
   
    // Time variable for seed
    time_t t;
   
    // Seed
    srand(time(&t));
   
    // Get random size of the list
    int size = 25 + rand() % 51;
   
    // Fill the list with random integers
    for (int i = 0; i < size; i++)
    {
       int randomData = rand() % 101;
       InsertNode(&head, randomData);
    }
   
    // Display list
    DisplayLinkedList(head);
   
    // Get sum of list elements and print it
    int sum = SumOfLinkedList(head);
    printf("sum = %i\n", sum);...

By purchasing this solution you'll be able to access the following files:
Solution.c.

$13.50
for this solution

or FREE if you
register a new account!

PayPal, G Pay, ApplePay, Amazon Pay, and all major credit cards accepted.

Find A Tutor

View available C-Family Programming Tutors

Get College Homework Help.

Are you sure you don't want to upload any files?

Fast tutor response requires as much info as possible.

Decision:
Upload a file
Continue without uploading

SUBMIT YOUR HOMEWORK
We couldn't find that subject.
Please select the best match from the list below.

We'll send you an email right away. If it's not in your inbox, check your spam folder.

  • 1
  • 2
  • 3
Live Chats