【数据结构】线性表的函数


#include "stdio.h"
#include "malloc.h"

#define LIST_INIT_SIZE 100
#define LISTINCREMENT 10
#define OK 1
#define ERROR 0

typedef int Status;
typedef int ElemType;

typedef struct
{
    ElemType* elem;
    int length;
    int listsize;
}SqList;  //顺序表类型定义

int  ListLength(SqList L);//返回表L的长度
void  ListPrint(SqList L); //1     打印输出表L中的各个元素
Status InitList_Sq(SqList& L); //1    初始化表L为空表,分配空间为100个元素的大小
Status ListEmpty(SqList L);//1   判断表L是否为空
Status GetElem_Sq(SqList L, int i, ElemType& e); //e返回表L中位序为i的元素值
Status ListInsert_Sq(SqList& L, int i, ElemType e);  //1    再表L中位序i之前插入元素e
Status ListDelete_Sq(SqList& L, int i, ElemType& e);//1  删除L中位序为i的元素,返回再e中
int LocateElem_Sq(SqList L, ElemType e);   //1    在顺序线性表L中查找第1个值与e相等的位置。若找到,则返回其在L中的位序,否则返回0。
void MergeList(SqList La, SqList Lb, SqList& Lc);//1 实现有序表La和Lb的合并,返回表Lc
void Difference(SqList La, SqList Lb, SqList& Lc);

int main()//通过主函数进行测试
{

    SqList La, Lb, Lc;
    int n, m, i,mid;
    ElemType x;

    InitList_Sq(La);
    scanf("%d",&n);
    for (i = 1; i <= n; i++)
    {
        scanf("%d",&x);
        ListInsert_Sq(La, La.length + 1, x);
    }

    InitList_Sq(Lb);
    scanf("%d",&m);
    for (i = 1; i <= m; i++)
    {
        scanf("%d",&x);
        ListInsert_Sq(Lb, Lb.length + 1, x);
    }//输入函数的

    MergeList (La,Lb,Lc);
    mid = (1 + Lc.length) / 2;
    printf("%d",Lc.elem[mid-1]); //打印整张表
    return 0;

}
//利用已经实现的基本操作设计实现void MergeList(SqList La, SqList Lb, SqList &Lc) ;即可
Status InitList_Sq(SqList& L) {
    L.elem = (ElemType*)malloc(sizeof(ElemType) * LIST_INIT_SIZE);
    L.length = 0;
    if (L.elem == NULL)
    {
        return ERROR;
    }
    else
    {
        return OK;
    }

};  //分配空间,初始化为空表。若空间分配成功,返回OK,否则返回ERROR
Status ListEmpty(SqList L) {
    if (L.length == NULL)
    {
        return OK;
    }
    else
    {
        return ERROR;
    }
};//判断顺序表是否为空,如果表空返回OK, 非空返回ERROR
Status ListInsert_Sq(SqList& L, int i, ElemType e) {

    if (i <= 0 || i > L.length + 1)
    {
        printf("weixu Error\n");
        return ERROR;
    }
    else
    {
        for (int j = L.length - 1; j >= i - 1; j--)
        {
            L.elem[j + 1] = L.elem[j];
        }
        L.elem[i - 1] = e;
        L.length++;
        return OK;
    }

}
int LocateElem_Sq(SqList L, ElemType e)
{
    for (int j = 0; j < L.length; j++)
        if (L.elem[j] == e)
            return j + 1;
    return 0;
}
Status ListDelete_Sq(SqList& L, int i, ElemType& e)
{
    ElemType* p, * q;
    if (i<1 || i>L.length + 1)
    {
        printf("weixu Error\n");
        return ERROR;
    }
    else
    {
        p = L.elem + i - 1;
        e = *p;
        q = L.elem + L.length - 1;
        for (++p; p <= q; ++p)
            *(p - 1) = *p;
        L.length--;
        return OK;
    }
}
void ListPrint(SqList L) //打印输出顺序表的所有元素
{
    int i;
    if (ListEmpty(L))   //如果表为空,则输出NULL
    {
        printf("NULL");
        return;
    }
    for (i = 1; i <= L.length; i++) //表非空,依次输出各个元素值,用空格隔开。主要最后一个元素后面没有空格
    {
        if (i == 1)
            printf("%d", L.elem[i - 1]);
        else printf(" %d", L.elem[i - 1]);
    }
}
void MergeList(SqList La, SqList Lb, SqList& Lc)
{
    int La_len, Lb_len;
    ElemType x, y;
    InitList_Sq(Lc);
    int i = 1;
    int j = 1;
    int k = 1;
    La_len = ListLength(La);
    Lb_len = ListLength(Lb);
    while (i <= La_len && j <= Lb_len)
    {
        GetElem_Sq(La, i, x);
        GetElem_Sq(Lb, j, y);

        if (x <= y)
        {
            ListInsert_Sq(Lc, k, x);
            i++;
            k++;
        }
        else
        {
            ListInsert_Sq(Lc, k, y);
            j++;
            k++;
        }
    }
    while (i <= La_len)
    {
        GetElem_Sq(La, i, x);
        ListInsert_Sq(Lc, k, x);
        i++;
        k++;
    }
    while (j <= Lb_len)
    {
        GetElem_Sq(Lb, j, y);
        ListInsert_Sq(Lc, k, y);
        j++;
        k++;
    }
    return;
}
int  ListLength(SqList L) {
    return L.length;
}
Status GetElem_Sq(SqList L, int i, ElemType& e) {
     
        if (i <= 0 || i >= L.length + 1)
        {
            printf("weixu Error\n");
            return ERROR;
        }
        else
        {
            e = L.elem[i - 1];
            return OK;
        }
    
}
void Difference(SqList La, SqList Lb, SqList& Lc) {
    int i = 1, k = 1;
    int la_len;
    ElemType x;

    la_len = ListLength(La);

    InitList_Sq(Lc);

    while (i<=la_len)
    {
        GetElem_Sq(La, i, x);
        if (LocateElem_Sq(Lb,x)==0)
        {
            ListInsert_Sq(Lc, k, x);
            k++;
        }
        i++;
    }
}

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM