シーケンステーブルの基本演算2

2247 ワード

/*
*Copyright (c) 2014,              
*All rights reserved
*    :test.cpp
*      :  
*    :2014 9 14 
*   :vi.10
*    :                 
*/
#include <stdio.h>
#include <malloc.h>

#define MaxSize 50    //Maxsize              
typedef int ElemType;  //ElemType                ,      int
typedef struct
{
    ElemType data[MaxSize];  //     MaxSize ElemType   
    int length;
} SqList;

//         
void InitList(SqList *&L);
bool ListInsert(SqList *&L,int i,ElemType e);
void DispList(SqList *L);
bool ListDelete(SqList *&L,int i,ElemType &e);
void DestroyList(SqList *&L);
//      
int main()
{
    SqList *sq;
    ElemType a;
    InitList(sq);
    ListInsert(sq, 1, 5);
    ListInsert(sq, 2, 3);
    ListInsert(sq, 1, 4);
    DispList(sq);
    ListDelete(sq,2,a);
    DispList(sq);
    DestroyList(sq);
    return 0;
}

//               
//      InitList(L)
void InitList(SqList *&L)
{
    L=(SqList *)malloc(sizeof(SqList));
    L->length=0;
}
//        
bool ListInsert(SqList *&L,int i,ElemType e)
{
    int j;
    if(i<1||i>L->length+1)
        return false;
    i--;
    for(j=L->length;j>i;j--)
        L->data[j]=L->data[j-1];
    L->data[i]=e;
    L->length++;
    return true;
}
//     DispList(L)  
void DispList(SqList *L)
{
    int i;
    for (i=0; i<L->length; i++)
        printf("%d ",L->data[i]);
    printf("
"); } // bool ListDelete(SqList *&L,int i,ElemType &e) { int j; if(i<1||i>L->length) return false; i--; e=L->data[i]; for(j=i;j<L->length-1;j++) L->data[j]=L->data[j+1]; L->length--; return true; } // DestroyList(L) void DestroyList(SqList *&L) { free(L); } <pre name="code" class="cpp">
<img src="http://img.blog.csdn.net/20150919161343504?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />
                ,              ,               。