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





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
Using the provided Linked List example code, linkedlist.c, as an example Make a node struct that...
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 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, 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; 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 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 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 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. 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 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 /***********************************************************/ /* 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 =...