I manipulated my code to be able to use pred_p but have run into problems since. My code stops at line "pred_p->next_p = temp_p;" and gives me the message "Thread 1: EXC_BAD_ACCESS (code=1, address=0x8).Not sure where to go from here.
struct list_node_s {
int data;
struct list_node_s* next_p;
};
struct list_node_s* Insert(struct list_node_s* head_p, int val);
void Print(struct list_node_s* head_p);
char Get_command(void);
int Get_value(void);
/*-----------------------------------------------------------------*/
int main(void) {
char command;
int value;
struct list_node_s* head_p = NULL;
/* start with empty list */
command = Get_command();
while (command != 'q' && command != 'Q') {
switch (command) {
case 'i':
case 'I':
value = Get_value();
head_p = Insert(head_p, value);
break;
case 'p':
case 'P':
Print(head_p);
break;
default:
printf("There is no %c command\n", command);
printf("Please try again\n");
}
command = Get_command();
}
return 0;
} /* main */
/*-----------------------------------------------------------------*/
struct list_node_s* Insert(struct list_node_s* head_p, int val) {
struct list_node_s* curr_p = head_p;
struct list_node_s* pred_p = NULL;
struct list_node_s* temp_p;
while (curr_p != NULL) {
if (curr_p->data >= val)
break;
pred_p = curr_p;
curr_p = curr_p->next_p;
}
// Create new node
temp_p = malloc(sizeof(struct list_node_s));
temp_p->data = val;
temp_p->next_p = curr_p;
if (pred_p = NULL) {
head_p = temp_p;
}
else {
pred_p->next_p = temp_p;
}
return head_p;
} /* Insert */