C/C++数据结构 线性表(顺序表)

一、数据存储结构

1. 顺序存储结构:数据元素放在地址连续的存储单元里。

2. 链式存储结构:数据元素存放在任意的存储单元里,存储单元可以不连续。

二、线性表的定义

     一个线性表是n个数据元素的有限序列。

三、线性表的顺序表示

  • 线性表的顺序表示指的是用一组地址连续的存储单元一次存储线性表的数据元素。
  • 特点:逻辑关系上相邻的两个元素在物理位置上也相邻
  • 优点:随机存取表中任一元素,它的存储位置可用一个简单、直观的公式来表示。
  • 缺点:作插入或删除操作时,需移动大量元素。

以下为源代码,主要功能有:初始化线性表;插入(插入随机数,并在插入时由小到大排序);求出线性表长度;判断线性表是否为空;得到线性表某个位置的元素;遍历线性表;查找元素e在线性表中的位置;删除线性表位置p的元素;清空线性表;销毁线性表(清空并释放分配的空间)

1. SequenceList.h

#ifndef _SEQUENCELIST_H
#define _SEQUENCELIST_H

#define SIZE 10
#define SUCCESS 10000
#define FAILURE 10001
#define TRUE    10002
#define FALSE   10003

typedef int ElemType;

struct SequenceList
{
    int length;
    ElemType *data;
};

typedef struct SequenceList SeList;

int SequenceInit(SeList *l);
int SequenceInsert(SeList *l, ElemType e);
int SequenceLength(SeList l);
int SequenceEmpty(SeList l);
int GetElem(SeList l, int p, ElemType *e);
int SequenceTraverse(SeList l, void (*p)(ElemType));
int LocateElem(SeList l, ElemType e);
int SequenceDelete(SeList *l, int p, ElemType *e);
int SequenceClear(SeList *l);
int SequenceDestroy(SeList *l);

#endif

2. SequenceList.c
 

#include "SequenceList.h"
#include <stdlib.h>
#include <stdio.h>

/*初始化,建立一个空线性表l*/
int SequenceInit(SeList *l) /*l为结构体指针*/
{
    if (NULL == l)   /*入参判断*/
    {
        return FAILURE;
    }

    l->length = 0; /*设顺序表长度为0(容量为SIZE)*/
    l->data = (ElemType *)malloc(sizeof(ElemType) * SIZE);

    if (NULL == l->data)
    {
        return FAILURE;
    }

    return SUCCESS;
}

/*在某个位置p插入元素e*/
int SequenceInsert(SeList *l, ElemType e)
{
    int i, j;

    if(l->length == 0)
    {
        l->length = 1;
        l->data[0] = e; /*将第一个元素放入第一个位置*/
        printf("%d ", l->data[0]);
        return SUCCESS;
    }

    if(l->length > 0)
    {
        printf("%d ", e);
        for(i = 0; i < l->length; i++)
        {
            if(e <= l->data[i])
            {
                for(j = 0; j < l->length - i + 1; j++)
                {
                    l->data[l->length - j] = l->data[l->length - j - 1];
                }
                l->data[i] = e;
                l->length++;
                return SUCCESS;
            }
        }
        if(e > l->data[i - 1])
        {
            l->data[i] = e;
            l->length++;
            return SUCCESS;
        }
    }
}

/*求出线性表的长度*/
int SequenceLength(SeList l)
{
    return l.length;
}

/*判断线性表是否为空*/
int SequenceEmpty(SeList l)
{
    return (l.length == 0) ? TRUE : FALSE;
}

/*得到线性表某个位置的元素*/
int GetElem(SeList l, int p, ElemType *e)
{
    if(p < 1 || p > l.length)
    {
        return FAILURE;
    }

    *e = l.data[p - 1];
    return SUCCESS;
}

/*遍历线性表*/
int SequenceTraverse(SeList l, void (*p)(ElemType))
{
    if(p == NULL)
    {
        return FAILURE;
    }
    int i;

    for(i = 0; i < l.length; i++) /*按顺序输出*/
    {
        p(l.data[i]); /*print函数打印*/
    }

    return SUCCESS;
}

/*查找元素e在线性表中的位置*/
int LocateElem(SeList l, ElemType e)
{
    int i;

    for(i = 0; i < l.length; i++)
    {
        if(e == l.data[i])
        {
            return i + 1;
        }
    }

    return FAILURE;
}

/*删除线性表位置p的元素*/
int SequenceDelete(SeList *l, int p, ElemType *e)
{
    if(NULL == l)
    {
        return FAILURE;
    }

    if(p < 1 || p > l->length)
    {
        return FAILURE;
    }

    int i;

    *e = l->data[p - 1];  /*将该位置的值赋给e*/

    for(i = 0; i < l->length - p; i++)  /*删除位置后面的数依次向前移动*/
    {
        l->data[p - 1 + i] = l->data[p + i];
    }

    l->length--; /*长度-1删除成功*/
    return SUCCESS;
}

/*清空线性表*/
int SequenceClear(SeList *l)
{
    if(l == NULL)
    {
        return FAILURE;
    }

    l->length = 0;

    return SUCCESS;
}

/*销毁线性表*/
int SequenceDestroy(SeList *l)
{
    if(l == NULL)
    {
        return FAILURE;
    }

    l->length = 0;
    free(l->data);
    l->data = NULL;

    return SUCCESS;
}

3. TestSequenceList.c 

#include "SequenceList.h"
#include <stdio.h>
#include <stdlib.h>

void print(ElemType e)
{
    printf("%d ", e);
}

int main()
{
    int ret, i;
    SeList list;  /*建立一个结构体对象list*/

    srand(time(NULL)); /*以系统时间作为随机数种子产生随机数*/

    ret = SequenceInit(&list); /*初始化,建立一个空线性表*/
    if(ret == SUCCESS)
    {
        printf("Init Success!\n");
    }
    else
    {
        printf("Init Failure!\n");
    }

    for(i = 0; i < 5; i++)
    {
        ret = SequenceInsert(&list, rand()%10); /*插入随机数*/

        if(FAILURE == ret) /*判断是否插入成功*/
        {
            printf("Insert Failure!\n");
        }
        else
        {
            printf("Insert Success!\n");
        }
    }

    ret = SequenceLength(list); /*输出线性表长度*/
    printf("length is %d\n", ret);

    ret = SequenceEmpty(list); /*判断线性表是否为空*/
    if(ret == TRUE)
    {
        printf("is empty!\n");
    }
    else
    {
        printf("not empty!\n");
    }

    /*得到线性表某个位置p的元素*/
    int p = 3; 
    ElemType e;
    ret = GetElem(list, p, &e);
    if(FAILURE == ret) /*判断是否得到元素*/
    {
        printf("Get Element Failure!\n");
    }
    else
    {
        printf("%dth element is %d\n", p, e);
    }

    /*遍历线性表*/
    ret = SequenceTraverse(list, print);
    if(ret == FAILURE)
    {
        printf("Traverse Failure!\n");
    }
    else
    {
        printf("Traverse Success!\n");
    }

    /*查找元素e在线性表中的位置*/
    e = 3;
    ret = LocateElem(list, e);
    if(ret == FAILURE)
    {
        printf("%d not exist!\n", e);
    }
    else
    {
        printf("%d is %dth element!\n", e, ret);
    }

    /*删除指定位置p的数*/
    p = 3;
    ret = SequenceDelete(&list, p, &e);
    if(SUCCESS == ret)
    {
        printf("Delete %d Success!\n", e);
    }
    else
    {
        printf("Delete Failure!\n");
    }

    /*清空线性表*/
    ret = SequenceClear(&list);
    if(SUCCESS == ret)
    {
        printf("Clear Success!\n");
    }
    else
    {
        printf("Clear Failure!\n");
    }

    /*销毁线性表*/
    ret = SequenceDestroy(&list);
    if(SUCCESS ==ret)
    {
        printf("Destroy Success!\n");
    }
    else
    {
        printf("Destroy Failure!\n");
    }

    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值