Solved

Posted on 2006-11-16
867 Views
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
could someone give me a detail example or sample code..

thanks

0
Question by:rmtogether
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• 2

LVL 19

Expert Comment

ID: 17960601
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

ID: 17960829
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;
};

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];
}
}
0

LVL 1

Accepted Solution

pattjin earned 500 total points
ID: 17960844
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];
}
}
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

### Suggested Solutions

Programmer's Notepad is, one of the best free text editing tools available, simply because the developers appear to have second-guessed every weird problem or issue a programmer is likely to run into. One of these problems is selecting and deleti…
An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…
###### Suggested Courses
Course of the Month8 days, 23 hours left to enroll