Question

Using the provided Linked List example code, linkedlist.c, as an example Make a node struct that...

  • Using the provided Linked List example code, linkedlist.c, as an example
  1. Make a node struct that holds ints instead of strings.
  2. In your main, insert 25 to 75 random integers with range (0 to 100) into a linked list of your nodes.
    • Use a random to determine how many to make.
  3. Write a function int sum(NodePointer current) that returns the sum of the integers in the list by looping through the linked list.
  4. Write a function int count(NodePointer current) that returns the number of nodes in the list by looping through the linked list.
  5. Use the two functions to calculate the floating point average of the elements
  6. Display the contents of the list, the sum, the count, and the average to the screen.

Example I/O

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

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

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

linkedlist.c file

/*
Example of a linked list of strings
*/

#include <stdio.h>
#include <stdlib.h> //has functions malloc() and free()
#include <string.h>
#include "stringinput.h"
#define MAX 30  //maximum characters in the word
#define QUIT "quit" //end the loop

/*
A self-referential structures contains a pointer 
that points to another structure of the same type
and link together to form dynamic data structures.
data: stores a word (string)
next: is a pointer to the next node
*/
struct node{
   char data[MAX];
   struct node *next;
};

//type definition for "new" data types
typedef struct node Node;
typedef struct node* NodePointer;

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

/*
main method - 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.
*/
int main(){
//stores each word
   char word[MAX]={'\0'};
//stores a pointer to the 1st node in the linked list
   NodePointer head = NULL;
   
//keep inserting until user enters "quit"
   while(0 != strcmp(word, QUIT)){
      printf("Enter a word to INSERT into linked list (enter \"%s\" to quit): ", QUIT);
      getline2(word, MAX);
      if(0 != strcmp(word, QUIT)){
      //&head: send the address of the pointer to the 1st node in list
         insertIntoLinkedList(word, &head);
         displayLinkedList(head);
      }
   }//end of while
   
//keep deleting until user enters "quit"
   strncpy(word, "don't quit", MAX);
   printf("\n");
   while(0 != strcmp(word, QUIT) && NULL != head){
      printf("Enter a word to DELETE from linked list (enter \"%s\" to quit): ", QUIT);
      getline2(word, MAX);
      if(0 != strcmp(word, QUIT)){
         deleteFromLinkedList(word, &head);
         displayLinkedList(head);
      }
   }
   
   return 0;
}//end of main

/*
Inserts a word in alphabetical order into a linked list
word2: is a string
head2: is the a pointer to the 1st node in a 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
      strcpy(newNode->data, word2);
   //figure out where to insert in linked list
      while(NULL != current && strcmp(word2, current->data)>0){
      //move previous to current
         previous = current;
      //move current to next node
         current = current->next;
      }//end of while
      //insert at beginning of linked list
      if(NULL == previous){
         newNode->next = current;
         //change the address stored in head
         *head2 = newNode; 
      }//end of if
      else{
      //insert between previous and current
         previous->next = newNode;
         newNode->next = current;
      }//end of else
   }//end of if
}//end of function


//displays the linked list
void displayLinkedList(NodePointer current){
   //for empty list
   if(NULL == current){
      printf("The linked list is empty!\n\n");
      return;
   }
   printf("linked list items: ");
   //loop through list
   while(NULL != current){
   //display each node
      printf("%s, ", current->data);
      //go to next node
      current = current->next;
   }
   printf("\n\n");
}//end of function


/*
Deletes a word from a linked list
word3: is a string
head3: is the a pointer to the 1st node in a 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 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;
   }

/*
check to see if 1st node can be deleted
*/
   if(0 == strcmp(word3, current->data)){
   //get address of 1st node
      tempNode = current;
   //change head to next node
      *head3 = current->next;
   //delete the node
      printf("deleting \"%s\" . . .\n", tempNode->data);
      free(tempNode);
   }
   else{
   //loop through linked list
      while(NULL != current && 0 != strcmp(word3, current->data)){
         //printf("current = %s\n", current->data);
      //move to next node in linked list
         previous = current;
         current = current->next;   
      }//end of while
      if(NULL != current){
      //get current node's address and store in tempNode
         tempNode = current;
         //printf("tempNode = %s\n", tempNode->data);
      //skip over current node
         previous->next = current->next;
      //delete node at current pointer
         printf("deleting \"%s\" . . .\n", tempNode->data);
         free(tempNode);
      }//end of if
   }//end of else    
}//end of function
   
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Output:

#include <stdio.h>

#include <stdlib.h>

#include <time.h>

typedef struct Node Node;

struct Node

{

int data;

Node *next;

};

void display(Node *temp)

{

while(temp!=NULL) {

printf("%d, ", temp->data);

temp = temp->next;

}

}

int sum(Node *current)

{

int sm = 0;

while(current!=NULL)

{

sm += current->data;

current = current->next;

}

return sm;

}

int count(Node *current)

{

int ct = 0;

while(current!=NULL)

{

ct++;

current = current->next;

}

return ct;

}




int main()

{

srand(time(0));

int howManyNodes = 25 + rand()%51;

int i;

int totalSum, totalCount;

Node *head = NULL;

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

{

if(head == NULL) {

head = (Node*)malloc(sizeof(int));

head->data = rand()%101;

head->next = NULL;

}

else {

Node *temp = head;

Node *newNode = (Node*)malloc(sizeof(int));

newNode->data = rand()%101;

if(newNode->data < head->data) {

newNode->next = head;

head = newNode;

} else {

while(temp->next!=NULL && temp->next->data < newNode->data) {

temp = temp->next;

}

newNode->next = temp->next;

temp->next = newNode;

}

}

}

printf("The list is: ");

display(head);

totalSum = sum(head);

totalCount = count(head);

printf("\nsum = %d\n", totalSum);

printf("count = %d\n", totalCount);

printf("average = %0.2f", totalSum/(float)totalCount);

return 0;

}


#include <stdio.h> #include <stdlib.h> #include <time.h> typedef struct Node Node; struct Node int data; Node *next; void display(Node *temp) while(temp!=NULL) { printf("%d, ", temp->data); temp = temp->next; int sum(Node *current) int sm = 0; while(current!=NULL)

while(current!=NULL) sm += current->data; current = current->next; return sm; int count(Node *current) { int ct = 0; while(current!=NULL) ct++; current = current->next; return ct; int main()

int main() srand(time()); int howManyNodes = 25 + rand()%51; int i; int totalsum, totalCount; Node *head = NULL; for(i=0; i<howManyNodes; i++) if(head == NULL) { head = (Node*)malloc(sizeof(int)); head->data = rand()%101; head ->next = NULL; berpand else { Node *temp = head; while(temp->next != NULL) { temp = temp->next; temp->next = (Node*)nalloc(sizeof(int)); temp = temp->next;

temp->next = (Node*) malloc(sizeof(int)); temp = temp->next; temp->data = rand()%101; temp->next = NULL; printf("The list is: "); display (head); totalsum = sum(head); totalCount = count (head); printf("\nSum = %d\n", totalsum); printf("Count = %d\n", totalCount); printf("Average = %f", totalsum/(float)totalCount); return 0;

The list is: 13, 26, 10, 11, 84, 27, 98, 26, 55, 33, 93, 26, 90, 21, 3, 98, 59, 55, 43, 45, 63, 13 Sum = 1728 Count = 39 Average = 44.307693

Add a comment
Know the answer?
Add Answer to:
Using the provided Linked List example code, linkedlist.c, as an example Make a node struct that...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Deleting multiples of a given integer from a linked list: #include <stdio.h> #include <stdlib.h> #include <assert.h>...

    Deleting multiples of a given integer from a linked list: #include <stdio.h> #include <stdlib.h> #include <assert.h> #define MAX 10000 typedef struct node_tag { int v; // data struct node_tag * next; // A pointer to this type of struct } node; // Define a type. Easier to use. node * create_node(int v) { node * p = malloc(sizeof(node)); // Allocate memory assert(p != NULL); // you can be nicer // Set the value in the node. p->v = v; p->next...

  • This is a code for linked list, it is about adding a node in the middle...

    This is a code for linked list, it is about adding a node in the middle of a list, I am really confused why int i = 2? can't it be 0? Add to the Middle • Allocate memory and store data for new node Traverse to node just before the required position of new node Change next pointers to include new node in between struct node *newNode; newNode = malloc(sizeof(struct node)); newNode->data = 4; struct node *temp head; for(int...

  • implement a doubly-linked list in C. Each node in the linked list should contain a string,...

    implement a doubly-linked list in C. Each node in the linked list should contain a string, a pointer to the previous node (or NULL), and a pointer to the next node (or NULL). The nodes should be sorted by their strings. struct node_t { char* str; struct node_t* prev; struct node_t* next; } To maintain the doubly-linked list, you should keep a pointer to the head node of the list (or NULL if the list is empty), and a pointer...

  • A linked list of integers is built using the following struct: struct node {   int data;...

    A linked list of integers is built using the following struct: struct node {   int data;   struct node *next; }; Define a function named max that returns the maximum integer in a list. The function takes one arguments, a pointer to the head of the list. The function returns an integer, which is the maximum value. If the list is empty, return zero. NOTE: You know nothing about the values in the list. They could all be negative!

  • struct Node * new_11( void ) { // return a new node to be the list...

    struct Node * new_11( void ) { // return a new node to be the list anchor struct Node * node = (struct Node *) malloc(sizeof(struct Node)); memset(node, o, sizeof(struct Node)); return node ; struct Node * find_l1(struct Node * anchor, char * word) { // given a pointer to the anchor of the list, and a word, search // the list for the word. return the pointer to the with the word, if found, 7/ or NULL if not...

  • () Given the following structure definition and typedef for a linked list of strings: typedef struct...

    () Given the following structure definition and typedef for a linked list of strings: typedef struct node st node; struct node st { char *word; /* a valid string pointer or NULL */ node *next; /* next node in the list or NULL */ }; Write a C function, free list(), that takes as an argument one of these lists, possibly NULL, and frees all the strings as well as the list itself. Write robust code. void free list(node *list){

  • Need some help creating a bubble sort for my linked list in C. Here are the...

    Need some help creating a bubble sort for my linked list in C. Here are the structs used in my code: struct simulation {    void *list;    int et; /* in seconds */ }; struct plane {    double x, y, altitude;    char callsign[15];    struct simulation *sim; }; Here is the struct used in my linked list: struct Node {    void *data;    struct Node *next; }; Here is my insert function: //ComparisonFunction and FILE not...

  • Programming in C: I am trying to modify this linked list to be doubly linked list....

    Programming in C: I am trying to modify this linked list to be doubly linked list. I’m also trying to add a print in reverse function. I’m really struggling with how to change the insert function to doubly link the nodes without effecting the alphabetical sorting mechanism. Example of desired output: Enter your choice: 1 to insert an element into the list. 2 to delete an element from the list. 3 to end. ? 1 Enter a character: a The...

  • Using the provided table interface table.h and the sample linked list code linkedList.c, complete an implementation...

    Using the provided table interface table.h and the sample linked list code linkedList.c, complete an implementation of the Table ADT. Make sure that you apply the concepts of design by contract (DbC) to your implementation. Once you have fully implemented the table, create a main.c file that implements a testing framework for your table. Your table implementation must ensure that values inserted are unique, and internally sorted within a linked list. table.h #ifndef _TABLE_H #define _TABLE_H //----------------------------------------------------------------------------- // CONSTANTS AND...

  • need this updated so it will delete the list and then recreate it again /***********************************************************/ /*...

    need this updated so it will delete the list and then recreate it again /***********************************************************/ /* Header files. */ /***********************************************************/ #include <stdio.h> #include <stdlib.h> #include <string.h> /***********************************************************/ /* Structure definitions. */ /***********************************************************/ struct node { int data; struct node *right; struct node *left; }; struct trash { struct node *node; struct trash *next; }; /****************************************/ /* BUILD_LIST. */ /****************************************/ void BUILD_LIST(int number2Add, struct node *(*head), struct node *(*tail)) { int i; struct node *previous, *current; *head = NULL; *tail =...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT