rmtogether
asked on
array to linked list
hi,
How can I convert array to linked list.
for example, an array
int t [3][3]={ {3,3,4},
{8,2,1},
{3,3,4}
};
how to represent t[0][0] t[0][1]....t[2][2] with their value by linked list
single linked list or double linked list?
could someone give me a detail example or sample code..
thanks
How can I convert array to linked list.
for example, an array
int t [3][3]={ {3,3,4},
{8,2,1},
{3,3,4}
};
how to represent t[0][0] t[0][1]....t[2][2] with their value by linked list
single linked list or double linked list?
could someone give me a detail example or sample code..
thanks
Don't forget to check for failed mallocs. This code adds the list entrys in reverse order to avoid having to keep track of the last pointer. At the end, head will point to the value of t[0][0], next t[0][1], t[0][2], t[1][0], and so forth until t[2][2];
int t [3][3]={ {3,3,4},
{8,2,1},
{3,3,4}
};
struct list_entry {
struct list_entry *next;
int val;
};
struct list_entry *head = NULL;
for (i=3; i>=0; i--) {
for (j=3; j>=0; j--) {
struct list_entry *cur = malloc(sizeof(struct list_entry));
cur->val = t[i][j];
cur->next = head;
head = cur;
}
}
int t [3][3]={ {3,3,4},
{8,2,1},
{3,3,4}
};
struct list_entry {
struct list_entry *next;
int val;
};
struct list_entry *head = NULL;
for (i=3; i>=0; i--) {
for (j=3; j>=0; j--) {
struct list_entry *cur = malloc(sizeof(struct list_entry));
cur->val = t[i][j];
cur->next = head;
head = cur;
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
For an example of a linked list see:
http://vergil.chemistry.gatech.edu/resources/programming/c-tutorial/lists.html