I am having problems with my doubly linked list. Can anyone help, I get a bus error after the first word is added. Thanks.
Code:
#include <stdio.h>
#include <ctype.h>
#define Z 21
struct node {
char strg[Z];
struct node *next, *prev;
};
void print_list(struct node *,struct node *);
void add_word(int *,struct node *, struct node *,struct node *);
/*********************************************************main()******/
int main() {
char user_input='1';
int run=1;
struct node *first,*current,*previous,*next,*newnode;
first=(struct node *)malloc (sizeof(struct node));
first->next=NULL;
first->prev=NULL;
while((user_input!='q')&&(user_input!='Q')) {
if (run!=1)
newnode=(struct node *)malloc(sizeof(struct node));
add_word(&run,first,current,newnode);
user_input='1';
print_list(first,current);
}
}
void add_word(int *run,struct node *first,struct node *current,struct node *newnode) {
if (*run==1) {
printf("Enter word to add.\n");
scanf("%s",first->strg);
current=first;
*run=0;
}
else {
printf("Enter word to add.\n");
scanf("%s",newnode->strg);
newnode->prev=current;
printf("%s",current->next);
newnode->next=current->next;
current->next=newnode;
current=newnode;
}
}
void print_list(struct node *first,struct node *current) {
struct node *print_current;
print_current=first;
printf("\n");
while (print_current!=NULL) {
if (print_current==current)
printf(">");
printf("%s ", print_current->strg);
print_current=print_current->next;
}
printf("\n\n");
}