0

i have an array, i want to create a doubly linked list from it,by transferring my elements to the nodes. and linking them through pointers(prev and next) what i did was

head = malloc(sizeof(struct)) head->prev=NULL head->next=NULL tail=head

for(i=0;i<num;i++){
    //copy data from arr[i] to tail
    temp=tail
    tail->next=malloc(sizeof(struct))
    tail=tail->next
    tail->prev=temp
}

now, how do i copy the data? temp, head and tail are pointers to structure

5
  • So, what is your question? Did you receive compiler errors because the code is not valid C? Or is that not what you did, but rather what you think to remember what you did (because, as said, that's not valid C)? Commented Dec 5, 2011 at 11:46
  • Ok but what are the contents of this struct? Commented Dec 5, 2011 at 11:47
  • i want to copy data of arr[i] to temp Commented Dec 5, 2011 at 11:47
  • struct a{ struct b b1[33]; struct c c1[128]; .. Commented Dec 5, 2011 at 11:48
  • You can of course use memcpy, but it's hard to tell what your problem is. You only say what you want to do, but not why you fail at doing so. And again, the code you posted is not valid C, so it's either not exactly what you did, or that may be your problem because you can't compile. Commented Dec 5, 2011 at 11:49

3 Answers 3

3

I guess you did not try writing and compiling. The elements of linked list should have at least some place to hold value from your array, let's say data of int type.

#include <stdio.h>
#include <malloc.h>
#define N 3

typedef struct a{
struct a* prev;
struct a* next;
int data;
}item;

int main(void)
{
item* head;
item* tail;
item* temp;
int num = N;
int data[N] = {1,2,3}; /*initialization*/
int i = 0;
head = (item*)malloc(sizeof(item));
head -> prev = NULL;
head -> next = NULL;
tail = head;
for(i = 0; i < num; i ++){
    temp = tail;
    tail -> next = (item*)malloc(sizeof(item));
    tail = tail -> next;
    tail -> next = NULL;
    tail -> data = data[i];
    tail -> prev = temp;
}
for(temp = head -> next; temp != NULL; temp = temp -> next) /*the following is for testing purpose*/
    printf("%d\t", temp -> data);
return 0;
}

Be aware that the head element does not contain what you want.

Sign up to request clarification or add additional context in comments.

Comments

1

create temp node and parse linklist from to last node assign these detail

counter=start_node;
     temp=(struct NODE *)malloc(sizeof(struct NODE));   /*create new node*/
                temp->node_data=data;                               /*set data in new node*/
                while(counter -> next_node!=NULL)
                {
                    counter=counter->next_node;             /*increment in counter*/
                }

          temp->prev_node=counter;          /*set next node of new node*/
          counter->next_node = temp;
          temp->next_node = null;

here temp is the current node.

1 Comment

I have a hard time at understanding what you say.
0

You need to save tail->prev, so you can do:

node * tailPrev = tail->prev;
memcpy(tail, &arr[i], sizeof(*tail));
tail->prev = tailPrev;
//continue with your code...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.