zoukankan      html  css  js  c++  java
  • C++程序设计实践指导1.10二维数组元素换位改写要求实现

    改写要求1:改写为以单链表和双向链表存储二维数组

    改写要求2:添加函数SingleLinkProcess()实现互换单链表中最大结点和头结点位置,最小结点和尾结点位置

    改写要求3:添加函数DoubleLinkProcess()实现互换双向链表中最大结点和头结点位置,最小结点和尾结点位置

    #include <cstdlib>
    #include <iostream>
    
    using namespace std;
    #define M 3
    #define N 4
    
    struct SingleLink
    {
           int Row;
           int Column;
           int Data;
           SingleLink *next;
    };
    
    struct DoubleLink
    {
           int Row;
           int Column;
           int Data;
           DoubleLink *prior;
           DoubleLink *next;
    };
    class Matrix
    {
          int a[M][N];
          public:
                 SingleLink* creatSingle(int a[][N]);
                 DoubleLink* creatDouble(int a[][N]);
                 void SingleLinkProcess(SingleLink* Shead);
                 void DoubleLinkProcess(DoubleLink* Dhead);
                 void showSingle(SingleLink* Shead)
                 {
                      SingleLink* p=Shead->next;
                      int i=0;
                      while(p)
                      {
                              cout<<p->Data<<'	';
                              i++;
                              if(i%4==0)
                              cout<<endl;
                              p=p->next;
                      }
                      cout<<endl;
                 }
                 void showDouble(DoubleLink* Dhead)
                 {
                      DoubleLink* p=Dhead->next;
                      int i=0;
                      while(p)
                      {
                              cout<<p->Data<<'	';
                              i++;
                              if(i%4==0)
                              cout<<endl;
                              p=p->next;
                      }
                      cout<<endl;
                 }
                 void showDoublePrior(DoubleLink* Dhead)
                 {
                      DoubleLink* p=Dhead->next;
                      DoubleLink* t;
                      while(p)
                      {
                              t=p;
                              p=p->next;
                      }
                      int i=0;
                      while(t)
                      {
                              cout<<t->Data<<'	';
                              i++;
                              if(i%4==0)
                              cout<<endl;
                              t=t->prior;
                      }
                      cout<<endl;
                 }
    };
    
    SingleLink* Matrix::creatSingle(int a[][N])
    {
              SingleLink* Shead=new SingleLink;
              Shead->next=NULL;
              SingleLink *p;
              p=Shead;
              int i,j;
              for(i=0;i<M;i++)
              for(j=0;j<N;j++)
              {
                     SingleLink* newSingleLink=new SingleLink;
                     newSingleLink->next=NULL;
                     newSingleLink->Data=a[i][j];
                     newSingleLink->Row=i;
                     newSingleLink->Column=j;
                     p->next=newSingleLink;
                     p=newSingleLink;                 
              }  
              return Shead;
    }
    
    void Matrix::SingleLinkProcess(SingleLink* Shead)
    {
         SingleLink* max,*maxpre,*maxtail,*hpre,*htail,*head,*ppre;
         SingleLink* min,*minpre,*mintail,*tpre,*tail;
         SingleLink* p,*q;
         p=Shead->next;
         max=p;
         head=p;
         hpre=Shead;
         htail=head->next;
         ppre=p;
         while(p)
         {
            
            if(max->Data<p->Data)
            {
               max=p;
               maxpre=ppre;
               maxtail=p->next;  
            } 
            ppre=p;
            p=p->next;
         }
         hpre->next=max;
         maxpre->next=head;
         head->next=maxtail;
         max->next=htail;
         
         p=Shead->next;
         int i=M*N-1;
    
         while(i)
         {
              tpre=p;
              p=p->next;  
              tail=p;
              i--; 
         }     
         p=Shead->next;
         min=p;
         ppre=p;
         while(p)
         {
            
            if(min->Data>p->Data)
            {
               min=p;
               minpre=ppre;
               mintail=p->next;          
            }
            ppre=p;
            p=p->next;
         }
         minpre->next=tail;
         tpre->next=min;
         tail->next=min->next;
         min->next=NULL;
    }
    
    DoubleLink* Matrix::creatDouble(int a[][N])
    {
         DoubleLink* Dhead=new DoubleLink;
         Dhead->next=NULL;
         Dhead->prior=NULL;
         DoubleLink* p=Dhead;
         int i,j;
         for(i=0;i<M;i++)
         for(j=0;j<N;j++)
         {
             DoubleLink* newDoubleLink=new DoubleLink;
             newDoubleLink->prior=NULL;
             newDoubleLink->next=NULL;
             newDoubleLink->Data=a[i][j];
             newDoubleLink->Row=i;
             newDoubleLink->Column=j;
             p->next=newDoubleLink;
             newDoubleLink->prior=p;
             p=newDoubleLink;
         }
         return Dhead;
    }
    
    void Matrix::DoubleLinkProcess(DoubleLink* Dhead)
    {
         DoubleLink* max,*maxpre,*maxtail,*hpre,*htail,*head;
         DoubleLink* min,*minpre,*mintail,*tail,*tpre;
         DoubleLink* p;
         p=Dhead->next;
         head=p;
         hpre=Dhead;
         htail=head->next;
         max=p;
         while(p)
         {
            if(max->Data<p->Data)
               {
                  max=p;
                  maxpre=p->prior;
                  maxtail=p->next;
               }
               p=p->next;
         }
         hpre->next=max;
         max->prior=hpre;
         max->next=htail;
         htail->prior=max;
         maxpre->next=head;
         head->prior=maxpre;
         head->next=maxtail;
         maxtail->prior=head;
         
         p=Dhead->next;
         while(p)
         {
            tail=p;
            p=p->next;
         }
         p=Dhead->next;
         min=p;
         tpre=tail->prior;
        while(p)
         {
            if(min->Data>p->Data)
            {
                min=p;
                minpre=p->prior;
                mintail=p->next;
            }
            p=p->next;
         }
        if(tpre==min)//结点是否相邻要分开处理
        {
           minpre->next=tail;
           tail->prior=minpre;
           tail->next=min;
           min->prior=tail;
           min->next=NULL;
        }
        else{ 
              minpre->next=tail;
              tail->prior=minpre;
              tail->next=mintail;
              mintail->prior=tail;
              tpre->next=min;
              min->prior=tpre;
              min->next=NULL;
        }     
    }
    
    int main(int argc, char *argv[])
    {
        int b[M][N]={{1,4,5,6},{7,2,10,11},{8,9,12,3}};
        SingleLink* Shead;
        DoubleLink* Dhead;
        Matrix ma;
        Shead=ma.creatSingle(b);
        ma.showSingle(Shead);
        ma.SingleLinkProcess(Shead);
        ma.showSingle(Shead);
        
        Dhead=ma.creatDouble(b);
        ma.showDouble(Dhead);
        ma.DoubleLinkProcess(Dhead);
        ma.showDouble(Dhead);
        system("PAUSE");
        return EXIT_SUCCESS;
    }
  • 相关阅读:
    xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!
    xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!
    xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!
    http 请求code状态码
    js 跳转链接
    a标签-伪类
    在linux中如何解压.tgz
    dyld: Library not loaded: /usr/local/opt/icu4c/lib/libicui18n.63.dylib
    nginx: [warn] conflicting server name "localhost" on 0.0.0.0:80, ignored
    微信小程序实现左滑删除效果(原生/uni-app)
  • 原文地址:https://www.cnblogs.com/c5395348/p/4280730.html
Copyright © 2011-2022 走看看