changing POP function to remove nodes
I removed the scanf line QuestionC mentioned.
Crashes stopped.
I changed the Pop function to having no arguments.
However it is still not working properly.
Choosing 1 to add nodes works okay.
Choosing 2 to delete nodes not working okay. and still in loop
Choosing 0 to stop ; okay but leaves me in the loop; doesn't end program
Choosing any other number defaults in switch; works okay.
What is wrong here???
Code:
#include <stdio.h>
#include <stdlib.h>
struct node {
int data; /* 1st item of node */
struct node *next; /* 2nd item of node */
};
struct node *first = NULL; /* global variable */
struct node *new_node; /* need pointers for
dynamic mem alloc */
void push(int insert_value);
void pop();
void print_list();
void choice_list(void);
void push(int insert_value)
{
new_node=malloc(sizeof(struct node));
new_node->data = insert_value;
new_node->next = first;
first = new_node;
}
void pop()
{
if (first = new_node) {
new_node->next = first;
free(new_node->next) ;
}
else
printf("Your list is now empty.");
}
void print_list()
{
if (new_node == NULL)
printf("Your list is empty.\n\n");
else {
printf("Your list is: \n");
while (new_node != NULL) {
printf("%d -> ",new_node->data);
new_node = new_node->next;
}
printf("NULL\n\n");
}
}
void choice_list(void)
{
printf("Choose one of the following.\n"
"0 To end program\n"
"1 To add a value to list\n"
"2 To remove a value from list\n");
}
main()
{
int number,choice;
for( ; ; ) {
choice_list();
printf("Enter your choice :");
scanf("%d",&choice);
printf("\n");
switch(choice) {
case 0:
printf("You have ended the program"
"Good-bye !!");
break;
case 1:
printf("Enter a number integer : \n");
scanf("%d",&number);
push(number);
print_list();
break;
case 2:
printf("Enter number to remove from your list :");
scanf("%d",&number);
pop(number);
print_list();
break;
default:
printf("That is an invalid choice\n\n");
break;
}
}
printf("End of run.\n");
return 0;
}