Solved

array to linked list

Posted on 2006-11-16
3
859 Views
Last Modified: 2013-12-14
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

0
Comment
Question by:rmtogether
  • 2
3 Comments
 
LVL 19

Expert Comment

by:BrianGEFF719
Comment Utility
You could create a 'standard' linked list whose nodes are integer arrays of size 3.

For an example of a linked list see:
http://vergil.chemistry.gatech.edu/resources/programming/c-tutorial/lists.html

0
 
LVL 1

Expert Comment

by:pattjin
Comment Utility
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;
   }
}
0
 
LVL 1

Accepted Solution

by:
pattjin earned 500 total points
Comment Utility
oops, that should be i = 2 and j = 2... I just noticed it since I wrote it forwards the first time and then just reversed it:

for (i=2; i>=0; i--) {
   for (j=2; j>=0; j--) {
       struct list_entry *cur = malloc(sizeof(struct list_entry));
       cur->val = t[i][j];
       cur->next = head;
       head = cur;
   }
}
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now