Convert an array into a link list.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
typedef struct Elem {
    int x;
    unsigned char value;
    struct Elem *next;
} Elem;

typedef struct Row {
    int y;
    Elem *elems;
    struct Row *next;
} Row;


Row *dense_to_sparse(unsigned char *dense_matrix, int width, int height) {
	int iarray[height][width];
	int icounter = 0;    
	for (int i = 0; i < height; i++){
	   for (int j = 0; j < width; j++){
		iarray[i][j] = image[icounter];
		icounter++;}}
/* So dense_matrix is a 1d array. it's too abstract for me. So for my own convenient i convert it to a 2d array. Easier for using for loop anyways. so for now iarray is something as simple as this:
(0,0) = 1
(0,1) = 245

I'm going to hard code the link list before I am able to do anything. */

        char* temp_pointer = null; // create a temp pointer 
	Elem *elem = (Elem *) malloc(sizeof(Elem)); // allocate space 
	elem.next = temp_pointer; // elem.next = null
	elem.x = 0; elem.value = 1; // set value (0,0) = 1; 
	temp_pointer = elem; // linking the nodes 
	Elem *elem = (Elem *) malloc(sizeof(Elem)); // again allocating space for next node
	elem.next = temp_pointer;
        elem.x = 1; elem.value = (0,1) = 245; // set value. 
        temp_pointer = elem;
}


I am just trying to link 2 nodes together. I don't know if i'm doing it right or not, I don't even have the way to test it. S=. Also I learn that whenever i malloc something I need to free it. I didn't do it here.
Last edited on
Topic archived. No new replies allowed.