首页 > 其他 > 详细

十字链表

时间:2014-06-05 18:25:32      阅读:504      评论:0      收藏:0      [点我收藏+]
  • 十字链表的语言描述

  • 基本运算的算法——建立稀疏矩阵的十字链表的算法、输出稀疏矩阵十字链表的算法

    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
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    #include<iostream>
    using namespace std;
    template <class T>
    class shizi
    {
    private:
        struct Node
        {
            int i;
            int j;
            T data;
            Node * right;
            Node * down;
            Node():right(NULL),down(NULL) {}
            Node(int x, int y , T a)
            {
                i = x;
                j = y;
                data = a;
                right = NULL;
                down = NULL;
            }
        };
        Node *rhead;
        Node *chead;
        Node *rcur;
        Node *ccur;
        int row,col,number;
    public:
        shizi( int x, int y , int num)
        {
            row = x;
            Node * head1 = new Node();
            rhead = head1;
            rcur = head1;
            for(int i = 0 ; i < row ; i++)//建立一列空的行节点
            {
     
                Node * p = new Node();
                rcur->down = p;
                rcur = rcur -> down;
            }
            col = y;
            Node * head2 = new Node();
            chead = head2;
            ccur = head2;
            for(int i = 0 ; i < col ; i++)//建立一行空的列节点
            {
     
                Node * p = new Node();
                ccur -> right = p;
                ccur = ccur -> right;
            }
            number = num;
        }
        int returnx()//返回x值
        {
            return rcur->i;
        }
        int returny()//返回y值
        {
            return rcur->j;
        }
        T returndata()//返回data值
        {
            return rcur->data;
        }
        void charu()//插入
        {
            for(int i = 0 ; i < number ; i++)
            {
                cout<<"please input x y data"<<endl;
                int x1,y1;
                T data1;
                cin>>x1>>y1>>data1;
                rcur = rhead;
                ccur = chead;
                //行---建立节点
                Node * p = new Node(x1,y1,data1);
                for(int i = 0 ; i < x1 ; i++ )//找到要插入的行
                {
                    rcur = rcur -> down;
                }
                for(int i = 0 ; i < y1 ; i++)//找到要插入的列
                {
                    if(rcur -> right == NULL)//如果列的元素为空
                    {
     
                        rcur -> right = p;
                        break;
                    }
                    else
                    {
                        rcur = rcur -> right;
                    }
                }
     
                //列指针的连接
                for(int i = 0 ; i < y1 ; i++)
                {
                    ccur = ccur -> right;
                }
                for(int i = 0 ; i < x1 ; i++)
                {
                    if(ccur -> down == NULL )
                    {
                        ccur -> down = p;
                        break;
                    }
                    else
                    {
                        ccur = ccur -> down;
                    }
                }
     
            }
     
        }
        void print()//打印
        {   Node * tem = rhead;
            rcur = rhead;
            tem = tem->down;
            rcur = rcur->down;
            for(int i = 0 ; i < row ; i++)//按行打印,遍历一行所有元素,然后移到下一行
            {
                while(rcur!=NULL)
                {
                    if(returnx()!=0&&returny()!=0)
                    cout<<returnx()<<" "<<returny()<<" "<<returndata()<<endl;
                    rcur = rcur -> right;
                }
     
                rcur = tem -> down;
                tem = tem -> down;
            }
        }
     
    };
    int main()
    {   int x,y,n;
        cout<<"please input row col number"<<endl;
        cin>>x>>y>>n;
        shizi < int > dusk(x,y,n);
        dusk.charu();
        dusk.print();
     
    }

     

      

     

十字链表,布布扣,bubuko.com

十字链表

原文:http://www.cnblogs.com/Duskcl/p/3768554.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!