It doesn't really matter why one works and another doesn't. You're not setting your pointers up right, so if it "works", it's just you getting lucky.
Code:
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
struct node {
char name[30];
char address[65];
float amount;
struct node * next;
struct node * prev;
};
typedef struct node item;
item * curr, * head, * tail, * new;
void insert(char *name_, char *address_, float amount_){
if ( (new = malloc( sizeof *new )) == NULL )
return;
strcpy( new->name, name_ );
strcpy( new->address, address_ );
new->amount = amount_;
new->next = new->prev = NULL;
if( head == NULL )
{
new-> prev = new->next = new; /* I assume you want this circular. */
head = tail = new;
}
else
if( strcmp( new->name, head->name ) < 0 )
{
new->prev = head->prev;
head->prev = new;
new->next = head;
}
else
if( strcmp( new->name, head->name ) > 0 )
{
new->next = head->next;
new->prev = head;
head->next = new;
}
else
{
free( new );
}
}
int main() {
insert("John", "12 main" , 112.2);
insert("Paul", "14 oak", 200.5);
curr = head;
while(curr) {
printf("%s, ", curr->name);
printf("%s, ", curr->address);
printf("%f\n", curr->amount);
curr = curr->next;
}
return 0;
}
Quzah.