Why is the Pop Function not working?
Giving me a SegFault error
Code:
#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(int removed_value); here's prototype
void instructions(void);
void printlist();
main()
{
int input, value, number;
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(number); here's call of function
printf("%d removed\n", 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 removed_value) here's function itself
{
if (removed_value == new_node-> value) {
first = new_node->next;
free(new_node);
}
else
printf("list is empty");
}
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");
}
void printlist()
{
if (new_node == NULL)
printf("List is empty.\n\n");
else {
printf("The list is:\n");
while (new_node != NULL) {
printf("%d --> ", new_node->value);
new_node = new_node->next;
}
printf("NULL\n\n");
}
}