线性表之顺序表

#include<iostream>
#include<stdio.h>

using namespace std;
#define Max 100
#define Size 10

typedef int Type;

typedef struct {
   Type *data;
   int length;
   int listsize;
   int increment;
}Sqlist;

void InitList(Sqlist &L)
{
    L.data = (Type *)malloc(Max*sizeof(Type));
    L.length = 0;
    L.listsize = Max;
    L.increment = Size;
}

void DestroyList(Sqlist &L)
{
    if(L.data != NULL) free(L.data);
    L.data = NULL; //attention
    L.length = 0;
    L.listsize = 0;
    L.increment = 0;
}

void ClearList(Sqlist &L)
{
   L.length = 0;  //注意跟元素初始化的区别
}

bool ListEmpty(Sqlist &L)
{
    if(L.length == 0) return true;  //注意空表和表不存在的区别
    else return false;
}

int ListLength(Sqlist &L)
{
     return L.length;    //表存在,返回元素的个数
}


Type GetElem(Sqlist &L, int i , Type *e)
{
    if(i > L.length + 1 || i < 1) {
        cout << "error" << endl;
        return 0;
    }
    e = &(L.data[i-1]);
    return *e;   //保存的下来以及返回类型
}


int LocateElem(Sqlist &L, Type e)
{
    for(int i = 0; i < L.length; i ++)  
            //或者改为指针查找,还有较函数必须改变
        if(L.data[i] == e) return i+1;
    if(i > L.length) {
        cout << "error" << endl;
        return 0;
    }
}

int PriorElem(Sqlist &L, Type cur_e; Type *e)
{
    for(int i = 2; i < L.length; i ++)
        if(cur_e == L.data[i]) 

}


int mian()
{
    
   return 0;
}

 

posted @ 2013-10-08 17:34  i梦  阅读(142)  评论(0)    收藏  举报