Data structure--c Language description Fifth (3) Cross linked list storage sparse matrix

Source: Internet
Author: User

This period of time to see C++primer, deeply understand the C + + is how complex a language, but in C + + contains a lot of C language does not have many characteristics, do not say so much, and so I swallowed it to read I should start to update the C + + blog, of course, this book I will be updated, haha.

The fifth chapter of the last content, with the cross linked table storage coefficient matrix, of course, I look at the back of the picture seems to have a cross linked list of content, I like the list of this data structure, to achieve a sense of ease, is probably only hand-cooked Seoul bar, haha. First, the logical diagram of the cross linked list.

The idea here is to keep a pointer at every beginning and column of the capital, which points to the smallest element of the row or to the smallest number of rows in the column. Here, the specific implementation of each row of the beginning of the pointer to form an array, the same as each row of the column first pointer also constitutes an array. Then we can construct the whole cross linked list by using the insertion method of the linked list. Personal humble Opinion: I think the fulfillment in the book here is flawed, the author may not have taken into account that if the beginning of the line begins with an element with a larger number of columns and then inserts a smaller number of columns before the element, the forward insertion author seems to have not considered it here, and I have considered this in the course of my implementation here, But I did not go through a lot of testing, the code may still have a lot of questions.

#include <stdio.h> #include <stdlib.h> #define MAXSIZE 1000 typedef int ELETYPE;
    typedef struct CLIST_NODE {int row;
    int col;
    Eletype e;
    struct Clist_node *right;
struct Clist_node *down;

}clistnode;
    typedef struct CLIST {int m;
    int n;
    int length;
    Clistnode **row_head;
Clistnode **col_head;

}clist;
    void Creat_crosslist (CList *m) {int I, J, Len, E;
    Clistnode *s;
    Clistnode *temp;
    Clistnode *pre;
    Get the initialized information of Crosslist scanf ("%d%d%d", &i, &j, &len);
    GetChar ();
    init the crosslist m->m = i;
    M->n = j;
    M->length = Len;
    M->row_head = (Clistnode * *) malloc ((MAXSIZE + 1) * sizeof (Clistnode *));
    M->col_head = (Clistnode * *) malloc ((MAXSIZE + 1) * sizeof (Clistnode *));
        for (i = 0; i < MAXSIZE + 1; i++) {M->col_head[i] = (Clistnode *) malloc (sizeof (Clistnode)); M->row_head[i] = (clistnode *) malloc (sizeof (Clistnode));
        M->col_head[i]->down = NULL;
    M->row_head[i]->right = NULL;
        while (1) {scanf ("%d%d%d", &i, &j, &e);
        GetChar ();
        if (i = = 0) break;
            else {S = (Clistnode *) malloc (sizeof (Clistnode));
            S->e = e;
            S->row = i;
            S->col = j;
            S->right = NULL;
            S->down = NULL;
            if (m->row_head[i]->right = = NULL) {m->row_head[i]->right = S;
                }else {pre = m->row_head[i];
                temp = m->row_head[i]->right;
                    while (temp->right!= NULL && Temp->col < j) {pre = temp;
                temp = temp->right;
                } if (Temp->col <= j) temp->right = S; else {Pre->right = S;
                S->right = temp;
            } if (M->col_head[i]->down = = NULL) {m->col_head[i]->down = S;
                }else {pre = m->col_head[i];
                temp = m->col_head[i]->down;
                    while (Temp->down!= NULL && Temp->col < j) {pre = temp;
                temp = temp->down;
                } if (Temp->col <= j) Temp->down = S;
                    else {pre->down = S;
                S->down = temp;
 }
            }
        }
    }
}

Code Summary:

The implementation of the code here still gives me a lot of exercise, I try to avoid inefficient implementation, in the structure of the pointer to use pointers instead of using an array of pointers to avoid the structure is too large, the specific implementation process, I will each line of the beginning of the elements also allocated space, the reason for doing so, Because it's much easier to implement it in the process of inserting the code forward, this code is still very interesting, and I'm going to start the chapter on the tree in the following blog.



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.