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

In the below file my delete function is not deleting the first node correctly. I

ID: 3712117 • Letter: I

Question

In the below file my delete function is not deleting the first node correctly. I just need help understanding and a fix to the problem. It deletes all other nodes correctly, but if I try to delete or fix the list to delete the first one things go crazy. Any help would greatly be appreciated. This code is in C

#include

#include

#include

#include

#define NAME_LEN 30

struct equipment{

char type[NAME_LEN+1];

char description[NAME_LEN+1];

int quantity;

struct equipment *next;

};

struct equipment *append_to_list(struct equipment *list);

void update(struct equipment *list);

void printList(struct equipment *list);

void clearList(struct equipment *list);

struct equipment *delete_from_list(struct equipment *list);

int read_line(char str[], int n);

int main(void)

{

char code;

struct equipment *e_list = NULL;  

printf("Operation Code: a for appending to the roster, f for finding a player"

", d for deleting, p for printing the roster; q for quit. ");

for (;;) {

printf("Enter operation code: ");

scanf(" %c", &code);

printf("%c ", code);

while (getchar() != ' ') /* skips to end of line */

;

switch (code)

{

case 'a': e_list = append_to_list(e_list);

break;

case 'u': update(e_list);

break;

case 'd': delete_from_list(e_list);

break;

case 'p': printList(e_list);

break;

case 'q': clearList(e_list);

return 0;

default: printf("Illegal code ");

}

printf(" ");

}

}

//This function adds struct items to the list

struct equipment *append_to_list(struct equipment *list)

{

//Checking for available memory and assining to pointer p

struct equipment *p = (struct equipment*)malloc(sizeof(struct equipment));

//struct equipment *q;

p->next = NULL;

//Starting location pointer at beggining of list

struct equipment *location = list;

//Recieving information to be processed

printf("Enter type: ");

read_line(p->type, NAME_LEN);

printf(" ");

//Recieving information to be processed

printf("Enter discription: ");

read_line(p->description, NAME_LEN);

printf(" ");

//Recieving information to be processed

printf("Enter quantity: ");

scanf("%d", &p->quantity);

printf("%d ", p->quantity);

if (list == NULL)

{

return p;

}

else

{

struct equipment *q = list;

while (q != NULL)

{

if ((strcmp(q->type, p->type) == 0) && (strcmp(q->description, p->description) == 0))

{

printf(" already exists in the list.");

return list;

}

if (strcmp(p->type, q->type) > 0) {

location = q;

q = q->next;

}

else if (strcmp(p->type, q->type) < 0) {

break;

}

else if (strcmp(p->type, q->type) == 0) {

if (strcmp(p->description, q->type) <= 0) {

break;

}

else {

location = q;

q = q->next;

}

}

}

if (q == list) {

p->next = list;

list = p;

return list;

}

else

{

p->next = q;

location->next = p;

return list;

}

}

return NULL;

}

//This function updates current items in the list

void update(struct equipment *list)

{

//Making sure list is created

if (!list)

{

printf("Equipment list not found");

return;

}

int add;

//Checking for memory space and assigning it to pointer p

struct equipment *p = (struct equipment*)malloc(sizeof(struct equipment));

//Creating pointer and beggining at the start of list

struct equipment *location = list;

//Recieving information to be processed

printf("Enter equipment type: ");

read_line(p->type, NAME_LEN);

printf(" ");

//Recieving information to be processed

printf("Enter equipment discription: ");

read_line(p->description, NAME_LEN);

printf(" ");

//Recieving information to be processed

printf("Enter equipment type: ");

scanf("%d", &add);

//While location is not null keep looking

while (location)

{

//Looking for the updated equipement the user requested

if ((strcmp(location->type, p->type) == 0) && (strcmp(location->description, p->description) == 0))

{

//Once found, do update

location->quantity = location->quantity + add;

printf("%s, new quantity: %d ", location->type, location->quantity);

free(p);

return;

}

//Go to next location

location = location->next;

}

}

//This function prints the list at the users request

void printList(struct equipment *list)

{

//Creating pointer p to point to list

struct equipment *p = list;

printf(" %s %s %s ", "Equipment", "Description", "Quantity");

//While p is not NULL print

while (p)

{

printf("%s %s %d ", p->type, p->description, p->quantity);

p = p -> next;

}

  

}

//This function depoints to items on the list and frees memory

void clearList(struct equipment *list)

{

struct equipment *p = list;

struct equipment *temp;

//Goes through list and frees items

while (!p)

{

temp = p->next;

free(p);

p = temp;

}

}

struct equipment *delete_from_list(struct equipment *list)

{

//Checking for available memory and assining to pointer p

struct equipment *p = (struct equipment*)malloc(sizeof(struct equipment));

struct equipment *q;

struct equipment *location;

p->next = NULL;

printf("Enter type: ");

read_line(p->type, NAME_LEN);

printf(" ");

printf("Enter discription: ");

read_line(p->description, NAME_LEN);

printf(" ");

//prev = curr = list;

q = list;

if (q == NULL)

{

printf("List is empty ");

}

else

{

location = q;

if (strcmp(location->type, p->type) == 0 && strcmp(location->description, p->description) == 0)

{

//This is where my problem is

//list = list->next;

}

else

{

while (q->next != NULL)

{

location = q->next;

if (strcmp(location->type, p->type) == 0 && strcmp(location->description, p->description) == 0)

{

break;

}

q = q->next;

}

if (q->next != NULL)

{

q->next = location->next;

printf("Deleted ");

}

else

{

printf("Not Found ");

}

}

}

return list;

}

//This was part of the original file given, I only edited it slightly to show more output

int read_line(char str[], int n)

{

int ch, i = 0;

while (isspace(ch = getchar()))

;

printf("%c", ch);

str[i++] = ch;

while ((ch = getchar()) != ' ')

{

printf("%c", ch);

if (i < n)

str[i++] = ch;

  

}

str[i] = '';

return i;

}

Explanation / Answer

// Check the bold mark for change

// File Name: LinkedListEquipment.c

#include<stdio.h>
#include<stdlib.h>
#define NAME_LEN 30

// Structure definition for equipment
struct equipment
{
char type[NAME_LEN+1];
char description[NAME_LEN+1];
int quantity;
struct equipment *next;
};// End of structure

// Prototype of the functions
struct equipment *append_to_list(struct equipment *list);
void update(struct equipment *list);
void printList(struct equipment *list);
void clearList(struct equipment *list);
struct equipment *delete_from_list(struct equipment *list);
int read_line(char str[], int n);

// main function definition
int main(void)
{

// To store the character code entered by the user
char code;
// Declares head node and initializes to null
struct equipment *e_list = NULL;
// Loops till user choice is not q
for (;;)
{
// Displays the menu
printf("Operation Code: (a) for appending to the roster, (u) for updating a player, (d) for deleting, (p) for printing the roster; (q) for quit. ");
// Accepts the user choice
printf("Enter operation code: ");
scanf(" %c", &code);
printf("%c ", code);
while (getchar() != ' ') /* skips to end of line */
;
// Checks the code entered by the user and calls appropriate function
switch (code)
{
case 'a':
e_list = append_to_list(e_list);
break;
case 'u':
update(e_list);
break;
case 'd':
e_list = delete_from_list(e_list);
break;
case 'p':
printList(e_list);
break;
case 'q':
clearList(e_list);
return 0;
default:
printf("Illegal code ");
}// End of switch case
printf(" ");
}// End of for loop
}// End of main function

//This function adds struct items to the list
struct equipment *append_to_list(struct equipment *list)
{
//Checking for available memory and assigning to pointer p
struct equipment *p = (struct equipment*)malloc(sizeof(struct equipment));
//struct equipment *q;
p->next = NULL;
//Starting location pointer at beginning of list
struct equipment *location = list;
//Receiving information to be processed
printf("Enter type: ");
read_line(p->type, NAME_LEN);
printf(" ");
//Receiving information to be processed
printf("Enter discription: ");
read_line(p->description, NAME_LEN);
printf(" ");
//Receiving information to be processed
printf("Enter quantity: ");
scanf("%d", &p->quantity);
printf("%d ", p->quantity);
// Checks if the list is null
if (list == NULL)
return p;
// Otherwise list is not null
else
{
// Creates a temporary pointer pointing to list
struct equipment *q = list;
// Checks if q is not null
while (q != NULL)
{
// To check duplicate record
// Checks if the current equipment type and description with the equipment type and description entered by the user
if ((strcmp(q->type, p->type) == 0) && (strcmp(q->description, p->description) == 0))
{
// Displays equipment already exists
printf(" Item already exists in the list.");
return list;
}// End of if condition
// Checks if the current equipment type is greater than the equipment type entered by the user
if (strcmp(p->type, q->type) > 0)
{
// Assign the q address to location
location = q;
// Now q points to its next equipment
q = q->next;
}// End of if condition
// Otherwise checks if the current equipment type is less than the equipment type entered by the user
else if (strcmp(p->type, q->type) < 0)
{
// Come out of the loop
break;
}// End of else if condition
// Otherwise checks if the current equipment type is equals to the equipment type entered by the user
else if (strcmp(p->type, q->type) == 0)
{
// Checks if the current equipment description is less than or equals to the equipment description entered by the user
if (strcmp(p->description, q->type) <= 0)
{
// Come out of the loop
break;
}// End of if condition
// Otherwise
else
{
// Assign the q address to location
location = q;
// Now q points to its next equipment
q = q->next;
}// End of else
}// End of else if condition
}// End of while loop
// Checks if q is equals to list which is head
if (q == list)
{
// p next points to list
p->next = list;
// P is assigned to list as the head
list = p;
// Return the list
return list;
}// End of if condition
// Otherwise
else
{
// p next points to q
p->next = q;
// location next points to p
location->next = p;
// Return the list
return list;
}// End of else
}// End of outer else
// Returns NULL
return NULL;
}// End of main function

//This function updates current items in the list
void update(struct equipment *list)
{
//Making sure list is created
if (!list)
{
printf("Equipment list not found");
return;
}// End of if condition
// To store the quantity entered by the user
int add;
//Checking for memory space and assigning it to pointer p
struct equipment *p = (struct equipment*)malloc(sizeof(struct equipment));
//Creating pointer and beginning at the start of list
struct equipment *location = list;
//Receiving information to be processed
printf("Enter equipment type: ");
read_line(p->type, NAME_LEN);
printf(" ");
//Receiving information to be processed
printf("Enter equipment description: ");
read_line(p->description, NAME_LEN);
printf(" ");
//Receiving information to be processed
printf("Enter equipment quantity: ");
scanf("%d", &add);
//While location is not null keep looking
while (location)
{
//Looking for the updated equipment the user requested
if ((strcmp(location->type, p->type) == 0) && (strcmp(location->description, p->description) == 0))
{
//Once found, do update by adding previous quantity with the currently entered quantity
location->quantity = location->quantity + add;
printf("%s, new quantity: %d ", location->type, location->quantity);
free(p);
return;
}// End of if condition
//Go to next location
location = location->next;
}// End of while loop
}// End of function

//This function prints the list at the users request
void printList(struct equipment *list)
{
//Creating pointer p to point to list
struct equipment *p = list;
printf(" %s %s %s ", "Equipment", "Description", "Quantity");
//While p is not NULL print
while (p)
{
printf("%s %s %d ", p->type, p->description, p->quantity);
// Move next
p = p -> next;
}// End of while loop
}// End of function

//This function depoints to items on the list and frees memory
void clearList(struct equipment *list)
{
struct equipment *p = list;
struct equipment *temp;
//Goes through list and frees items
while (!p)
{
temp = p->next;
free(p);
p = temp;
}// End of while loop
}// End of function

// Function to delete a node
struct equipment *delete_from_list(struct equipment *list)
{
//Checking for available memory and assigning to pointer p
struct equipment *p = (struct equipment*)malloc(sizeof(struct equipment));
// Temporary pointers
struct equipment *q;
struct equipment *location;
p->next = NULL;
// Accepts data to find an equipment to delete
printf(" Enter equipment information to delete ");
printf("Enter type: ");
read_line(p->type, NAME_LEN);
printf(" ");
printf("Enter description: ");
read_line(p->description, NAME_LEN);
printf(" ");
// q is pointing to list
q = list;

// Checks if list is empty
if (q == NULL)
printf("List is empty ");
// Otherwise nodes are available
else
{
// location points to list
location = list;
// For first equipment node
// Checks if the location equipment type and description is equals to the equipment type and description entered by the user
if (strcmp(location->type, p->type) == 0 && strcmp(location->description, p->description) == 0)
{
// q points to its next
q = q -> next;
// Returns q
return q;
}// End of if condition

// Otherwise it is not the first equipment node
else
{
// Loops till q next is not null
while (q->next != NULL)
{
// location points to q next
location = q->next;
// Checks if the location equipment type and description is equals to the equipment type and description entered by the user
if (strcmp(location->type, p->type) == 0 && strcmp(location->description, p->description) == 0)
// Come out of the loop
break;
// Otherwise move next
q = q->next;
}// End of while loop
// Checks if q next node is null
if (q->next != NULL)
{
// q next points to location next
q->next = location->next;
printf(" Equipment deleted ");
}// End of if condition
else
printf("Not Found ");
}// End of inner else
}// End of outer else
return list;
}// End of function

//This was part of the original file given, I only edited it slightly to show more output
int read_line(char str[], int n)
{
int ch, i = 0;
while (isspace(ch = getchar()))
;
printf("%c", ch);
str[i++] = ch;
while ((ch = getchar()) != ' ')
{
printf("%c", ch);
if (i < n)
str[i++] = ch;
}// End of while loop
str[i] = '';
return i;
}// End of function

Sample Output:

Operation Code: (a) for appending to the roster, (u) for updating a player, (d) for deleting, (p) for printing the roster; (q) for quit.
Enter operation code: a
a
Enter type: Lux
Lux
Enter discription: soap
soap
Enter quantity: 10
10

Operation Code: (a) for appending to the roster, (u) for updating a player, (d) for deleting, (p) for printing the roster; (q) for quit.
Enter operation code: a
a
Enter type: Cinthol
Cinthol
Enter discription: Soap
Soap
Enter quantity: 22
22

Operation Code: (a) for appending to the roster, (u) for updating a player, (d) for deleting, (p) for printing the roster; (q) for quit.
Enter operation code: a
a
Enter type: Fox
Fox
Enter discription: Perfume
Perfume
Enter quantity: 5
5

Operation Code: (a) for appending to the roster, (u) for updating a player, (d) for deleting, (p) for printing the roster; (q) for quit.
Enter operation code: a
a
Enter type: Kenley
Kenley
Enter discription: Water
Water
Enter quantity: 50
50

Operation Code: (a) for appending to the roster, (u) for updating a player, (d) for deleting, (p) for printing the roster; (q) for quit.
Enter operation code: p
p

Equipment Description Quantity
Cinthol Soap 22
Fox Perfume 5
Kenley Water 50
Lux soap 10

Operation Code: (a) for appending to the roster, (u) for updating a player, (d) for deleting, (p) for printing the roster; (q) for quit.
Enter operation code: u
u
Enter equipment type: Fox
Fox
Enter equipment description: Perfume
Perfume
Enter equipment quantity: 5
Fox, new quantity: 10

Operation Code: (a) for appending to the roster, (u) for updating a player, (d) for deleting, (p) for printing the roster; (q) for quit.
Enter operation code: p
p

Equipment Description Quantity
Cinthol Soap 22
Fox Perfume 10
Kenley Water 50
Lux soap 10

Operation Code: (a) for appending to the roster, (u) for updating a player, (d) for deleting, (p) for printing the roster; (q) for quit.
Enter operation code: d
d

Enter equipment information to delete
Enter type: Cinthol
Cinthol
Enter description: Soap
Soap

Operation Code: (a) for appending to the roster, (u) for updating a player, (d) for deleting, (p) for printing the roster; (q) for quit.
Enter operation code: p
p

Equipment Description Quantity
Fox Perfume 10
Kenley Water 50
Lux soap 10

Operation Code: (a) for appending to the roster, (u) for updating a player, (d) for deleting, (p) for printing the roster; (q) for quit.
Enter operation code: d
d

Enter equipment information to delete
Enter type: Lux
Lux
Enter description: soap
soap
Equipment deleted

Operation Code: (a) for appending to the roster, (u) for updating a player, (d) for deleting, (p) for printing the roster; (q) for quit.
Enter operation code: p
p

Equipment Description Quantity
Fox Perfume 10
Kenley Water 50

Operation Code: (a) for appending to the roster, (u) for updating a player, (d) for deleting, (p) for printing the roster; (q) for quit.
Enter operation code: q
q

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote