线性表的顺序表示以及实现

#include <iostream>
using namespace std;
#define LIST_INIT_SIZE 100
#define LISTINCREMENT 10
#define OVERFLOW -2
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE -1

typedef int ElemType;
typedef int status;

//线性表结构
typedef struct{
    ElemType *elem;
    int length;
    int listsize;
}SqList;


//初始化线性表
status InitList_Sq(SqList &L, int number = 0 )
{
    L.elem = new ElemType[LIST_INIT_SIZE];
    if ( !L.elem )
    {
        exit(OVERFLOW);
    }
    L.length = number;
    L.listsize = LIST_INIT_SIZE;
    cout << "Input the value of the list:"<< endl;
    ElemType value;
    for ( int i = 0; i < number; i++ )
    {
        cin >> value;
        L.elem[i] = value;
    }
    return OK;
}

//在第i个元素之前插入一个元素e
status ListInsert_Sq(SqList &L, int i, ElemType e )
{
    if ( i < 1|| i > L.length + 1 )
    {
        return ERROR;
    }

    if ( L.length >= L.listsize )
    {
        ElemType* newbase = (ElemType*)realloc(L.elem, (L.listsize + LISTINCREMENT)*sizeof(ElemType));
        if ( !newbase)
        {
            exit(OVERFLOW);
        }
        L.elem = newbase;
        L.listsize += LISTINCREMENT;
    }
    //int rules = 
    ElemType* q =L.elem + i - 1;
    ElemType* end = L.elem + L.length - 1;
    for (ElemType* p = end; p >= q; p-- )
    {
        *(p+1) = *p;
    }
    *q = e;
    L.length++;
    return OK;
}

status ListDelete_Sq( SqList &L, int i, ElemType &e )
{
    if ( i < 1 || i > L.length )
    {
        return ERROR;
    }

    e = L.elem[i];
    ElemType *q = &L.elem[L.length-1];
    for ( ElemType* p = &L.elem[i]; p <= q; p++ )
    {
        *(p-1) = *p;
    }
    L.length--;
    return OK;
}

int LocateElem_Sq( SqList L, ElemType e )
{
    int location = 0;
    ElemType* p = &L.elem[0];
    ElemType* q = &L.elem[L.length-1];
    while ( *p != e && p < q )
    {
        p++;
        location++;
    }
    return location+1;
}

void MergeList_Sq( SqList La, SqList Lb, SqList &Lc )
{
    Lc.length = La.length + Lb.length;
    Lc.listsize = Lc.length;
    ElemType* pa = La.elem;
    ElemType* pb = Lb.elem;
    Lc.elem = new ElemType[Lc.listsize];
    if (!Lc.elem)
    {
        exit(OVERFLOW);
    }
    ElemType* pc = Lc.elem;
    ElemType* pa_End = La.elem + La.length - 1;
    ElemType* pb_End = Lb.elem + Lb.length - 1;
    while( pa <= pa_End && pb < pb_End )
    {
        if ( *pa < *pb )
        {
            *pc = *pa;
            pa++;
        }
        else
        {
            *pc = *pb;
            pb++;
        }
        pc++;
    }
    while( pa <= pa_End )
    {
        *pc++ = *pa++;
    }
    while( pb <= pb_End )
    {
        *pc++ = *pb++;
    }
}

void output_List( SqList &L )
{
    ElemType *q = &L.elem[L.length-1];
    for ( ElemType *p = &L.elem[0]; p <= q; p++ )
    {
        cout << *p << "  ";
    }
    cout << endl;
}

int main()
{
    SqList L;
    
    cout <<"Input the number of the value of List:"<< endl;
    int t;
    cin >> t;
    InitList_Sq( L, t );
    output_List(L);

    cout << "Input the insert location and the value:"<<endl;
    int Insert_location, Insert_value;
    cin >> Insert_location >> Insert_value;
    ListInsert_Sq( L, Insert_location, Insert_value );
    output_List(L);
    

    cout << "Input the delete location:"<< endl;
    int Delete_location;
    ElemType Returned_value;
    cin >> Delete_location;
    ListDelete_Sq( L, Delete_location, Returned_value );
    output_List(L);
    cout << Returned_value << endl;

    cout << "Input the element to be located:"<<endl;
    int elem;
    cin >> elem;
    int g = LocateElem_Sq( L, elem );
    cout << g << endl;

    SqList La;
    SqList Lb;
    SqList Lc;
    int ta,tb;
    cin >> ta;
    InitList_Sq( La, ta );
    output_List(La);
    cin >> tb;
    InitList_Sq( Lb, tb );
    output_List(Lb);
    MergeList_Sq(La,Lb, Lc);
    output_List(Lc);
    return 0;
}

 

posted @ 2013-07-11 10:54  2011winseu  阅读(192)  评论(0编辑  收藏  举报