t=malloc(sizeof(NODE));
		t->lname[i]=ln[i];
		t->fname[i]=fn[i];
		t->add[i]=ad[i];
		t->telno[i]=tn[i];
		t->next=NULL;

		i++;


	if(*h==NULL){

		printf("\nCreating record...");
		*h=t;

	}
	else{
		p=*h;

		if(p->next!=NULL)
		{
			while(p->next!=NULL){
				if(strcmp(p->lname[i],ln[i])>=0){
					p->prev->next=t;
					t->next=p;
				}
				p->prev=p;
			}
		}
		else
		{
			if(strcmp(p->lname[i],ln[i])>=0)
			{
				p->next=t;
				t->prev=p;
				t->next=NULL;
			}
			else
			{
				p->prev=t;
				t->next=p;
			}
			*h=p;
		}
	}

could help me with my code? i can't make the 3rd node and when i try to make the 2nd node and print it the 2nd node will replace the data inside my first node.

Hmm lets try adding code tags, so it becomes a bit more readable. Also couldyou give us a little clue as to what problems you are actually having cause thats alot to look over when we don't know what were looking for

Chris

I presume h is a pointer to the first node? Like:
NODE* h;

In that case, this code:

if(*h==NULL)
//should be:
if (h == NULL)

Because (*h) is actualy the node itself, and not a pointer.

Hi there,

Please try to be more specific about your queries... Looks like this is a doubly linked list... a queue?...

Like Sci@phy said, if h is a pointer to first node, you should use

if(h == NULL)

or if it is an argument you have passed to this function of type NODE**, then its correct...

Further more, do a type cast while memory allocating, like

t=(NODE *)malloc(sizeof(NODE));

How many nodes are you trying to add at one go?... And are you trying to add the node in a reverse manner?

t=(NODE *)malloc(sizeof(NODE));

Why the hell would you cast the malloc. You shouldn't do that! Read this.

@OP for me most of of the things seems to be fine, except the head node pointer deferenceing like it has been mentioned by others already. Unless you've got your head node like this

void Insert( NODE **h, data );

???

ssharish

I think what you are trying to do is insert values in a doubly linked list in alphabetical order by the last name ?

if so, you aren't going about it correctly .. lets assume you have a set of 5 elements you want to insert in the list; We'll try to insert them in descending order by the last name..

/* lets insert 5 elements in the list */
  for(j = 0; j < 5; j++){

    /* there are no elements in the list. add the first one */
    if(h==NULL){
      h=&t[j];
    }
    else{
      p      = h; 
      done = 0;
      /* lets try to figure out the node, around which we want to 
         insert this new element */ 
      while(p->next != NULL && !done){
        if(strcmp(p->lname,t[j].lname) >= 0){
          p = p->next;
        }
        else{
          done = 1;
        }
      }
      /* do we want to insert the element before the current node
         or after the current node ? */ 
      if(strcmp(p->lname,t[j].lname)>=0){
        /* after -- manipulate the prev and next pointers 
           for the new node. the prev pointer for the
           node after the current node and the next pointer 
           for the current node */ 
        t[j].next       = p->next; 
        t[j].prev       = p;
        if(p->next)
          p->next->prev = &t[j];
        p->next         = &t[j];
      }
      else{
        /* before -- manipulate the prev and next pointers 
           for the new node. the next pointer for the
           node before the current node and the prev pointer 
           for the current node */ 
        t[j].prev       = p->prev;
        t[j].next       = p;
        if(p->prev)
          p->prev->next = &t[j];
        p->prev         = &t[j];         
      }
    }

  }

Why the hell would you cast the malloc. You shouldn't do that! Read this.

@OP for me most of of the things seems to be fine, except the head node pointer deferenceing like it has been mentioned by others already. Unless you've got your head node like this

void Insert( NODE **h, data );

???

ssharish

Thanks for that info... i wasn't aware of that...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.