程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> C++程序設計實踐指導1.10二維數組元素換位改寫要求實現,程序設計實踐1.10

C++程序設計實踐指導1.10二維數組元素換位改寫要求實現,程序設計實踐1.10

編輯:C++入門知識

C++程序設計實踐指導1.10二維數組元素換位改寫要求實現,程序設計實踐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<<'\t';
                          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<<'\t';
                          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<<'\t';
                          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;
}

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved