데이터 구조 기초 - 선형 표 조작

코드:
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int maxn = 1e5+10;
const int inf = 0x3f3f3f3f;
int arr[maxn];
int n;
typedef int status;
typedef int ElemType;
typedef struct
{
    ElemType data[maxn]; /*        */
    int length; /*       */
} Sqlist;
/*    :         */
/* e  L  i       */
status Getelem(Sqlist *L,int i,ElemType *e)
{
    if(L->length==0 || i<1 ||i>L->length) return false;
    *e=L->data[i-1];
    return true;
}


/*insert */
/*
*        ,    ;
*L.length >= maxn return false;
*from the last position to the i-th position move back one position
*insert e to the i-th position
*length+=1;
*/
status ListInsert(Sqlist *L,int i,ElemType e)
{
     if(L->length>=maxn) return false;
     if(i<1 || i>L->length+1) return false;
     if(i<=L->length) /*       */
     {
         for(int k=L->length-1; k>=i-1; --k)
            L->data[k+1]=L->data[k];
     }
     L->data[i-1]=e;
     L->length++;
     return true;
}


/*delete */
/*
*         ,    
*pick up the delete e
*from the e position to the last position,than move pre one position
*length-=1;
*/
status ListDelete(Sqlist *L,int i,ElemType *e)
{
    if(L->length ==0 )return false;
    if(i<1 || i>L->length) return false;
    *e=L->data[i-1];
    if(i<L->length)
    {
        for(int k=i; k<L->length; ++k)
            L->data[k-1]=L->data[k];
    }
    L->length--;
    return true;
}
/*
          ,  ,     ,      O(1),      O(n)
  :
                          
            
  :              ,           ,       “  ”
*/

좋은 웹페이지 즐겨찾기