顺序表

顺序表(传递地址)

建表、输入、输出

typedef int Typedef;
const int MAXSIZE = 800;

typedef struct
{
    Typedef data[MAXSIZE];
    Typedef last;
}SeqList;

SeqList *init_SeqList()
{
    SeqList *L;
    L = (SeqList*)malloc(sizeof(SeqList)); // L= new SeqList;
    L->last = -1;
    return L;
}

void input(SeqList *L)
{
    L->last++;
    while( cin >> L->data[L->last] )//读到文件结束符EOF 
        L->last++;
    L->last--; 
}

void print(SeqList *L)
{
    for( int i = 0; i <= L->last; i++)
        cout << L->data[i] << ' ';
    cout << endl << L->last + 1; 
} 

顺序表(传递引用)

建表、输出

void init_SeqList(SeqList &L)
{ 
    L.last = -1;       
}
//顺序表输出 
void print(const SeqList &L)//const 避免 L 在该函数中被无意修改
{
    for(int i = 0; i < L.last; i++)
        cout << L.data[i] << " ";
    cout << endl << L.last + 1;
}

 插入

Typedef Insert_SeqList(SeqList &L,int i,Typedef x)
{
    if(L.last==MAXSIZE-1)
    {
        printf("Full");return (-1);
    }
    if(i<1 || i>L.last+2)
    {
        printf("wrong");return (0);
    }
    for(int j=L.last; j>=i-1; j--)
        L.data[j+1]=L.data[j];
    L.data[i-1]=x;
    L->last++;
    return 1;
}

删除

Typedef Delete_SeqList(SeqList &L,int i)
{
    if(i<0 || i>L.last)
    {
        printf("wrong");return 0;
    }
    for(int j=i; j<L.last; j++)
        L.data[j]=L.data[j+1];
    L->last--;
    return 1;
}

交换前后两部分:先整体反向,再在每部分反向。

删除顺序表中多余的元素

void purge_SeqList(SeqList &L)
{
    k=-1;
    for(int i=0;i<=L.last;i++)
    {
        j=0;
        while(j<=k && L.data[i]!=L.data[j])
            ++j;
        if(k==-1 || j>k)
            L.data[++k]=L.data[i];
    }
    L.last=k;
}
原文地址:https://www.cnblogs.com/Cindy-Chan/p/11178265.html