deleting an integer from a list
Good morning, here's my problem. I have an assigment to create a list and do things to it like push, pop, count, printlist and delete item from list. Everything works well but I can't get the delete function to work. The code for it comes almost directly from my text but it doesn't work. When I run it and print the list afterwards, the list has not changed. However, if I put an incorrect value in the function, I do get the proper response from the function. Its the correct values that are the problem. I've tried everything. Originally I was using a void function with 1 argument, the input value. Now I have set it up with 2 arguments, the input value and the list, because this was more like the code in the text. I'm new to C and in every and all cases so far, I've always found that the problem was something very simple that I overlooked.
Here's the function:
void delete(struct node *list,int input_value)
{
struct node *cur, *prev, *q;
for (cur = list, prev = NULL;
cur != NULL && cur->value != input_value;
prev = cur, cur = cur->next)
;
if (cur == NULL)
printf("value not found\n");
return;
if (prev == NULL) {
list = list->next;
printf(" First value removed"); }
else
prev->next = cur->next;
free (cur);
return;
}
Its kind of long b#include <stdio.h>
#include <stdlib.h>
struct node {
int value;
struct node *next;
};
struct node *first = NULL;
struct node *new_node;
void push( int input_value);
void pop();
/* struct node *delete(struct node *first, int input_value);
*/
void delete(struct node *list, int input_value);
void instructions(void);
void printlist();
main()
{
int input, value, number, lv;
instructions();
printf("?");
scanf("%d", &input);
while (input != 0) {
switch (input) {
case 1:
printf("Enter an integer: ");
scanf("\n%d", &number);
push(number);
printlist();
break;
case 2:
pop();
printlist();
break;
case 3:
printf("Enter value to be removed: ");
scanf("\n%d", &number);
delete(first, number);
printlist();
break;
default:
printf("Invalid choice.\n\n");
break;
}
printf("?");
scanf("%d", &input);
}
printf("End of run.\n");
return 0;
}
void push( int input_value)
{
new_node = malloc(sizeof(struct node));
new_node->value = input_value;
new_node->next = first;
first = new_node;
}
void pop()
{
int lv;
if (first == NULL)
return;
lv = first->value;
printf(" %d returned\n", lv);
first = first->next;
return;
}
void delete(struct node *list,int input_value)
{
struct node *cur, *prev, *q;
for (cur = list, prev = NULL;
cur != NULL && cur->value != input_value;
prev = cur, cur = cur->next)
;
if (cur == NULL)
printf("value not found\n");
return;
if (prev == NULL) {
list = list->next;
printf(" First value removed"); }
else
prev->next = cur->next;
free (cur);
return;
}
void instructions (void)
{
printf("Enter your choice:\n"
" 0 to end program.\n"
" 1 to add a value to the list.\n"
" 2 to remove a value from the list.\n"
" 3 to delete a value from the list.\n");
}
void printlist()
{
struct node *p;
if (first == NULL)
printf("List is empty.\n\n");
else {
printf("The list is:\n");
p = first;
while (p != NULL) {
printf("%d --> ", p->value);
p = p->next;
}
printf("NULL\n\n");
}
}
ut here is the complete program: