To me, it is not obvious how to set up the POP and PUSH functions.
Now I changed some stuff around in my program and I get a new error I aint ever seen called a BUS error. What the heck is that?
The menu of choices comes up. I choose 1 to add a number to my list; then it cycles again and asks for another choice then I choose 1 again to add a number; and then I get bumped with the BUS error.
Please explain my problems with my key functions POP and PUSH and what am I missing/ doing wrong???
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(int take_away_value);
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(int take_away_value)
{
if (take_away_value == new_node->data) {
first = new_node->next;
free(new_node);
}
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 : ");
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(" ?");
scanf("%d",choice);
}
printf("End of run.\n");
return 0;
}