A03/sorted_snackbar.c
// Insert a new node to a list (implemented as a linked list). 
// The new node should store the given properties
// Param snacks: the first item in the list (NULL if empty)
// Param name: the snack name (max length is 32 characters)
// Param quantity: the snack quantity
// Param cost: the snack cost
// Returns the first item in the list
Insert a new node to a list (implemented as a linked list).
The new node should store the given properties
Param snacks: the first item in the list (NULL if empty)
Param name: the snack name (max length is 32 characters)
Param quantity: the snack quantity
Param cost: the snack cost
Returns the first item in the list
Insert a new node to a list ( implemented as a linked list ) . The new node should store the given properties Param snacks : the first item in the list ( NULL if empty ) Param name : the snack name ( max length is 32 characters ) Param quantity : the snack quantity Param cost : the snack cost Returns the first item in the list
struct snack* insert_sorted(struct snack* snacks,
                const char* name, int quantity, float cost) {
	struct snack *insert;
	struct snack *current;
        insert = malloc(sizeof(*insert));
        if (insert == NULL) {
                printf("Error with malloc");
                return NULL;
        }
	strcpy(insert->name, name);
	insert->quantity = quantity;
	insert->cost = cost;
	insert->next = NULL;
	if (snacks == NULL || strcmp(snacks->name, name) <= 0) {
		insert->next = snacks;
		snacks = insert;
	} else {
		current = snacks;
		while (current->next != NULL && strcmp(current->next->name, insert->name) < 0) {
			current = current->next;
		}
		insert->next = current->next;
		current = insert;
	}
	print(snacks);
	return snacks;
}
struct snack * insert_sorted ( struct snack * snacks , const char * name , int quantity , float cost ) { struct snack * insert ; struct snack * current ; insert = malloc ( sizeof ( * insert ) ) ; if ( insert == NULL ) { printf ( " " ) ; return NULL ; } strcpy ( insert -> name , name ) ; insert -> quantity = quantity ; insert -> cost = cost ; insert -> next = NULL ; if ( snacks == NULL || strcmp ( snacks -> name , name ) <= 0 ) { insert -> next = snacks ; snacks = insert ; } else { current = snacks ; while ( current -> next != NULL && strcmp ( current -> next -> name , insert -> name ) < 0 ) { current = current -> next ; } insert -> next = current -> next ; current = insert ; } print ( snacks ) ; return snacks ; }
Insert a new node to a list (implemented as a linked list).
Insert a new node to a list ( implemented as a linked list ) .
/*strcpy(insert->name, name);
        insert->quantity = quantity;
        insert->cost = cost;
        insert->next = NULL;
	struct snack* current;
	if (snacks == NULL || strcmp(snacks->name, name) > 0) {
		insert->next = snacks;
		snacks = insert;
	} else if (snacks->next == NULL) {
		if (strcmp(current->name, name) < 1) {
			insert->next = snacks;
			snacks = insert;
		} 
	} else {
		current = snacks;
		while (current->next != NULL && strcmp(current->next->name, name) < 1) {
			current = current->next;
		}
		insert->next = current->next;
		current->next = insert;
	}*/