数据结构学习笔记(一) -- 顺序表

顺序表

code is here

顺序表的结构

此处以长度为100的顺序表为例, 其中顺序表中的data类型为int。

#define MAXLEN 100

typedef int elementType;
typedef struct sList
{
     
    elementType data[MAXLEN];
    int iListLen;
}SeqList;

顺序表的基本操作

初始化顺序表

建立顺序表后, 需要初始化顺序表得到一个未保存任何数据的结构。

void InitializeList(SeqList (*L))
{
     
    L -> iListLen = 0;
}

得到顺序表的长度

int getLen(SeqList (*L))
{
     
    return L -> iListLen;
}

求交集、并集、差集C代码实现

/*
 * author           : liteng
 * date                 : 2019/09/19
*/
#include <stdio.h>

#define MAXLEN 100

typedef int elementType;
typedef struct sList
{
     
    elementType data[MAXLEN];
    int iListLen;
}SeqList;

int Append(SeqList(*), elementType);    // 顺序表追加元素
int isInList(int, SeqList (*));
void IntersectionGenerate(SeqList (*), SeqList (*), SeqList (*));    // 并集生成
void UnionGenerate(SeqList (*), SeqList (*), SeqList (*));   // 交集生成
void DifferencesetGenerate(SeqList (*), SeqList (*), SeqList (*));   // 差集生成
int main(void)
{
     
    SeqList a, b, c;
    a.iListLen = 5;
    b.iListLen = 5;
    for (size_t i = 0; i < 5; i++)
    {
     
        a.data[i] = i;
    }
    for (size_t i= 0; i < 5; i++)
    {
     
        b.data[i] = i + 2;
    }
    SeqList *p1 = &a;
    SeqList *p2 = &b;
    SeqList *p3 = &c;
    DifferencesetGenerate(p1, p2, p3);
    printf("size ->%d\n", p3 ->iListLen);
    for (size_t i = 0; i < p3 ->iListLen; i++)
    {
     
        printf("%d   ", p3 -> data[i]);
    }
    return 0;
}


/*
 * name             : Append
 * function         : append a data to SeqList
*/
int Append(SeqList (*A), elementType Append_content)
{
     
    A -> iListLen ++;
    A -> data[A -> iListLen - 1] = Append_content;
}

/*
 * name             : isInList
 * function         : judge a num whether is in a SeqList
*/
int isInList(int num, SeqList (*List))
{
     
    for (size_t i = 0; i < List -> iListLen; i++)
    {
     
        if (num == List -> data[i])
        {
     
            return 0;
        }
    }
    return 1;
}

/*
 * name             : IntersectionGenerate
 * function         : generate a Intersection C of A and B
*/
void IntersectionGenerate(SeqList (*A), SeqList (*B), SeqList (*C))
{
     
    for (size_t i = 0; i < A -> iListLen; i++)  // 复制A到C
    {
     
        C -> data[i] = A -> data[i];
    }
    C ->iListLen = A -> iListLen;
    for (size_t i = 0; i < B -> iListLen; i++)
    {
     
        if ((isInList(B -> data[i], A)))
        {
     
            C -> iListLen ++;
            C -> data[C -> iListLen - 1] = B -> data[i];
        }
    }
}


/*
 * name             : UnionGenerate
 * function         : generate Union C from A and B
*/
void UnionGenerate(SeqList (*A), SeqList (*B), SeqList (*C))
{
     
    C ->iListLen = 0;
    for (size_t i = 0; i < A ->iListLen; i++)
    {
     
        if (!(isInList(A -> data[i], B)))
        {
     
            C ->iListLen ++;
            C -> data[C -> iListLen - 1] = A -> data[i];
        }
    }
}


/*
 * name             : UnionGenerate
 * function         : generate diference set C from A and B
*/
void DifferencesetGenerate(SeqList (*A), SeqList (*B), SeqList (*C))
{
     
    C -> iListLen = 0;
    for (size_t i = 0; i < A -> iListLen; i++)
    {
     
        if (isInList(A -> data[i], B))
        {
     
            C -> iListLen ++;
            C -> data[C -> iListLen - 1] = A -> data[i];
        }
    }
    for (size_t i = 0; i < B -> iListLen; i++)
    {
     
        if (isInList(B -> data[i], A))
        {
     
            C -> iListLen ++;
            C -> data[C -> iListLen - 1] = B -> data[i];
        }
    }
}

你可能感兴趣的:(数据结构学习笔记(一) -- 顺序表)