선형 표 순서 저장 구조 완전 실행 가능 코드

13238 단어 데이터 구조
#include 
#include 
#include
#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0

#define MAXSIZE 20 /*           */
typedef int ElemType;   /* ElemType          ,     int */
typedef int Status;     /* Status      ,           , OK  */

typedef struct
{
  ElemType data[MAXSIZE]; /*   ,       */
  int length;             /*         */
}SqList;

/*          */
Status InitList(SqList *L)
{
    L->length=0;
    return OK;
}

//      
SqList Create(SqList L)
{
    int i;
    srand((unsigned)time(NULL));
  for(i=0; i < 10; i++)
  {
    L.data[i] = rand()%100;
        L.length++;
  }
    return L;
}

/*     :     L   。*/
/*     : L      */
Status ClearList(SqList *L)
{
    L->length = 0;
    return OK;
}


/*    :ListTraverse     */
/*    :       */
Status visit(ElemType c)
{
    printf("%d ",c);
    return OK;
}

/*     :     L    */
/*     :   L          */
Status ListTraverse(SqList L)
{
  int i;
    for(i=0;i < L.length;i++)
        visit(L.data[i]);
    printf("
"
); return OK; } /* : L ,1≤i≤ListLength(L), */ /* : L i e,L 1 */ Status ListInsert(SqList *L,int i,ElemType e) { int k; if (L->length==MAXSIZE) /* */ return ERROR; if (i < 1 || i>L->length+1)/* i */ return ERROR; if (i <= L->length) /* */ { for(k=L->length-1;k>=i-1;k--) /* */ L->data[k+1]=L->data[k]; } L->data[i-1]=e; /* */ L->length++; return OK; } /* : L ,1≤i≤ListLength(L) */ /* : L i , e ,L 1 */ Status ListDelete(SqList *L,int i,ElemType *e) { int k; if (L->length==0) /* */ return ERROR; if (i < 1 || i>L->length) /* */ return ERROR; *e=L->data[i-1]; if (i < L->length) /* */ { for(k=i; k < L->length; k++)/* */ L->data[k-1]=L->data[k]; } L->length--; return OK; } /* : L ,1≤i≤ListLength(L) */ /* : e L i , i , 1 0 */ Status GetElem(SqList L,int i,ElemType *e) { if(L.length==0 || i < 1 || i>L.length) return ERROR; *e=L.data[i-1]; return OK; } /* : L */ /* : L 。 */ int ListLength(SqList L) { return L.length; } /* : L */ /* : L , TRUE, FALSE */ Status ListEmpty(SqList L) { return (L.length == 0)? TRUE : FALSE; } /* : L */ /* : L 1 e 。 */ /* , 0 */ int LocateElem(SqList L,ElemType e) { int i; if (L.length==0) return 0; for(i=0;i < L.length;i++) { if (L.data[i]==e) break; } if(i>=L.length) return 0; return i+1; } int main() { SqList L; ElemType e; Status i; int opp; int j,k; int pos; ElemType value; i=InitList(&L); printf(" ,L.length=%d
"
,L.length); printf("
1.
2.
3.
4.
5.
6.
7.
8.
9.
0.

"
); while(opp != 0){ scanf("%d",&opp); switch(opp){ case 1: ListTraverse(L); printf("
"
); break; case 2: L = Create(L); printf(" :L.data="); ListTraverse(L); printf("
"
); break; case 3: i=ClearList(&L); printf(" L :L.length=%d
"
,L.length); ListTraverse(L); printf("
"
); break; case 4: printf(" :"); scanf("%d",&pos); printf(" :"); scanf("%d",&value); i = ListInsert(&L,pos,value); printf(" , :
"
); ListTraverse(L); printf("
"
); break; case 5: printf(" ? "); scanf("%d",&pos); GetElem(L,pos,&e); printf(" %d :%d
"
,pos,e); break; case 6: printf(" : "); scanf("%d",&e); k=LocateElem(L,e); // if(k) printf(" %d %d
"
,e,k); else printf(" %d
"
,e); break; case 7: printf(" ?"); scanf("%d",&pos); ListDelete(&L,pos,&e); printf(" , :
"
); ListTraverse(L); printf("
"
); break; case 8: k = ListLength(L); printf(" : %d
"
, k); break; case 9: i = ListEmpty(L); if (i) printf(" .
"
); else printf("
"
); break; case 0: exit(0); } } }

좋은 웹페이지 즐겨찾기