• 十字链表


    • 十字链表的语言描述

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

      #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();
      
      }
      

       

        

       

  • 相关阅读:
    python中is和==的区别
    深拷贝和浅拷贝
    编码和解码
    with语句处理异常
    python中运行flask报错:UnicodeDecodeError: 'utf8' codec can't decodebyte 0xd5 in position 0:invalid continuation byte
    python中update的基本使用
    python中的程序控制结构
    python中的四种存储结构总结
    python中list,tuple,dict,set特点对比总结
    解决UIScrollview无故偏移和导航条遮挡view的问题
  • 原文地址:https://www.cnblogs.com/Duskcl/p/3768554.html
Copyright © 2020-2023  润新知