데이터 구조 (1): 선형 표 의 순서 표시 와 실현

15560 단어 데이터 구조
//            
#include 
#define OK 1
#define ERROR 0
#define OVERFLOW -2
#define MAXSIZE 100
// Status        ,           
typedef int Status;
// ElemType        
typedef int ElemType;
//     
int i,j; 
using namespace std;

//        
typedef struct
{
    ElemType *elem; //         
    int length;     //     
}SqList;            //          SqList

//        
Status InitList(SqList &L)
{   //          L
    //            MAXSIZE     
    L.elem = new ElemType[MAXSIZE]; 
    if(!L.elem) exit(OVERFLOW);     //         
    L.length = 9;
    return OK;
}

//       
Status GetElem(SqList L,int i,ElemType &e)
{
    //   i     ,    ,  ERROR
    if(i<1 || i>L.length) return ERROR; 
    e = L.elem[i-1];            // L.elem[i-1]   i     
    return OK;  
}

//       
int LocateElem(SqList L,ElemType e)
{   //     L     e     ,     
    for(i=0; i < L.length; ++i)
        if(L.elem[i] == e) return i+1;  //     ,    i+1
    return ERROR;                       //     ,  0
}

//       
Status ListInsert(SqList &L,int i,ElemType e)
{   //     L  i           e,i       
    //  1<=i<=L.length+1
    if(i<1 || i>L.length+1) return ERROR;   // i    
    if(L.length == MAXSIZE) return ERROR;   //         
    for(j=L.length; j >= i-1; --j)
        L.elem[j+1] = L.elem[j];    //                      
    L.elem[i-1] = e;                //      e   i   
    ++L.length;                             //    1
    return OK;
}

//       
Status ListDelete(SqList &L,int i)
{   //     L    i   ,i       1<=i<=L.length
    if(i<1 || i>L.length) return ERROR; // i    
    for(j=i; j <= L.length-1; --j)
        L.elem[j-1] = L.elem[j];    //             
    --L.length;                         //    1
    return OK;
}

int main()
{
    /***************************************************/
}

최적화 판:
#include 
using namespace std;
#define OK 1
#define ERROR 0
#define OVERFLOW -2
#define MAXSIZE 100

typedef int Status;
typedef int ElemType;

typedef struct
{
    ElemType *elem;
    int length;
}SqList; 

// 1、   
Status InitList(SqList &L)
{
    L.elem = new ElemType[MAXSIZE];
    if(!L.elem) exit(OVERFLOW);
    L.length = 0;
    return OK;
}

// 2、  
Status GetElem(SqList L, int i, ElemType &e)
{
    if(i-1<0 || i>L.length) return ERROR;
    e = L.elem[i-1];
    return OK;
}

// 3、  
int LocateElem(SqList L, ElemType e)
{
    for(int i=0; i < L.length; ++i)
        if(L.elem[i] == e)
            return i+1;
    return ERROR;
}

// 4、  
Status ListInsert(SqList &L, int i, ElemType e)
{
    if(i-1<0 || i>L.length+1) return ERROR;
    if(L.length == MAXSIZE) return ERROR;
    for(int j=L.length-1; j >= i-1; --j)
        L.elem[j+1] = L.elem[j];
    L.elem[i-1] = e;
    ++L.length;
    return OK;
}     

// 5、  
Status ListDelete(SqList &L, int i)
{
    if(i-1<0 || i>L.length) return ERROR;
    for(int j=i-1; j < L.length-1; ++j)
        L.elem[j] = L.elem[j+1];
    --L.length;
    return OK;
}

int main()
{
    cout<<"************************************"<cout<<"*      1、                      *"<cout<<"*      2、                       *"<cout<<"*      3、                       *"<cout<<"*      4、                       *"<cout<<"*      5、                       *"<cout<<"*      6、                    *"<cout<<"*      7、                       *"<cout<<"************************************"<int n,m,i,choice;
    while(1)
    {
        cout<<"       : ";
        cin>>choice;
        switch(choice)
        {
            case 1:
                if(InitList(L)) cout<<"     !"<else cout<<"     !"<continue;
            case 2:
                cout<<"           : ";
                cin>>n;
                if(GetElem(L,n,e)) cout<<"    !"<"     : "<else cout<<"    !        ,     。"<continue;
            case 3:
                cout<<"        : ";
                cin>>e;
                if(LocateElem(L,e)) cout<<"  "<"    "<"  ."<else cout<<"  "<"     !              .      。"<continue;
            case 4:
                cout<<"           ( <" ): ";
;               cin>>n;
                if(n > MAXSIZE-L.length) 
                {
                    cout<<"    ,     ."<continue;
                }
                for(i=0; icout<"         : ";
                    cin>>m;
                    cout<<"         : ";
                    cin>>e;
                    if(ListInsert(L,m,e)) cout<<"  "<"     !"<else { cout<<"   "<"     !     ."<continue;
            case 5:
                cout<<"           : ";
                cin>>m;
                if(ListDelete(L,m)) cout<<"     !"<else cout<<"     ."<continue;
            case 6:
                for(i=0;icout<" ";
                }
                cout<continue;
            case 7:
                cout<<"    ..."<for(i=0;i<99999999;++i){}
                cout<<"      .       0.0"<break;
            default:
                for(i=0;i<99999999;++i){}
                cout<<"      ! "<break;
        }
        break;
    }
    return 0;
}

좋은 웹페이지 즐겨찾기