【数据结构】表之顺序表

1.表分类

  1. 顺序结构
  2. 链式结构

2.本文讲解顺序表

  1. 创建顺序表
```
#include <iostream>
#include <malloc.h>
using namespace std;

#define MAXSIZE 50
typedef int ElemType;
typedef struct
{
    ElemType data[MAXSIZE];
    int length;
}SqList;

void CreateList(SqList*&L,ElemType a[],int n);

int main()
{
    ElemType a[10] = {1,2,3,4,5,6};
    SqList* L;
    CreateList(L,a,10);
    for (int n = 0; n < 10; n++)
    {
        cout << L->data[n] << " ";
    }
    system("pause");
    return 0;
}

void CreateList(SqList*&L, ElemType a[], int n)
{
    L = (SqList*)malloc(sizeof(SqList));
    for (int i = 0; i < n; i++)
    {
        L->data[i] = a[i];
    }
    L->length = n;
    return;
}
  1. 创建加显示显示顺序表
#include <iostream>
#include <malloc.h>
using namespace std;

#define MAXSIZE 50
typedef int ElemType;
typedef struct
{
    ElemType data[MAXSIZE];
    int length;
}SqList;

void CreateList(SqList*&L,ElemType a[],int n);//创建顺序表
void DisplayList(SqList *L);//输出顺序表
int main()
{
    ElemType a[10] = {1,2,3,4,5,6};
    SqList* L;
    CreateList(L,a,10);
    /*for (int n = 0; n < 10; n++)
    {
        cout << L->data[n] << " ";
    }*/
    DisplayList(L);
    system("pause");
    return 0;
}

void CreateList(SqList*&L, ElemType a[], int n)
{
    L = (SqList*)malloc(sizeof(SqList));
    for (int i = 0; i < n; i++)
    {
        L->data[i] = a[i];
    }
    L->length = n;
    return;
}
void DisplayList(SqList*L)
{
    if (L->length == 0)
        return;
    for (int i = 0; i < L->length; i++)
    {

        if (i != 0 && i % 5 == 0)
            cout << endl;
        cout << L->data[i] << " ";
    }
    cout << endl;
}
  • 加上一些其他的功能
    【1】判断顺序表是否为空
    【2】求顺序表的长度
    【3】得到顺序表的某个位置上的元素
    【4】按照元素的值查找在顺序表中的位置
    【5】插入元素
    【6】删除元素
    【7】顺序表的初始化

  • 下面依程序的形式给出

#include <iostream>
#include <malloc.h>
using namespace std;

#define MAXSIZE 50
typedef int ElemType;
typedef struct
{
    ElemType data[MAXSIZE];
    int length;
}SqList;

void CreateList(SqList* &L,ElemType a[],int n);//创建顺序表
void DisplayList(SqList* L);//输出顺序表
void InitSqList(SqList* &L);//初始化顺序表
int LengthSqList(SqList* L);//得到顺序表的长度
bool IsEmptySqList(SqList* L);//判断顺序表是否为空
void DestroySqList(SqList* &L);//销毁顺序表
bool GetElemFromi(SqList*L,int i,ElemType &e);//得到第i位的元素
int GetiFromElem(SqList*L,ElemType e);//得到元素e所在的位置
bool InsertElemSqList(SqList* &L,int i,ElemType e);//在位置i处插入元素e
bool DeleteElemFromiSqList(SqList* &L,int i,ElemType &e);
int main()
{
    ElemType a[10] = {1,2,3,4,5,6};
    SqList* L;
    InitSqList(L);//测试初始化顺序表

    CreateList(L,a,10);//测试创建的顺序表

    DisplayList(L);//测试显示顺序表

    if (IsEmptySqList(L))//测试顺序表的空不空判断
        cout << "Empty!" << endl;
    else
        cout << "Not Empty!" << endl;

    cout << "The sqlist Length is " << LengthSqList(L) << endl;//测试求顺序表的长度

    ElemType e;//测试得到i位置上的元素
    GetElemFromi(L,2,e);
    cout << "The 2nd element is " << e << endl;

    GetiFromElem(L,e);//测试得到元素e的位置

    InsertElemSqList(L,1,e);//测试插入元素

    DeleteElemFromiSqList(L,10,e);//测试删除顺序表中的元素
    cout << "The element deleted is " << e << endl;

    DestroySqList(L);//测试销毁顺序表
    system("pause");
    return 0;
}

void CreateList(SqList*&L, ElemType a[], int n)
{
    //L = (SqList*)malloc(sizeof(SqList));
    InitSqList(L);
    for (int i = 0; i < n; i++)
    {
        L->data[i] = a[i];
    }
    L->length = n;
    return;
}
void DestroySqList(SqList* &L)
{
    free(L);
}
void DisplayList(SqList*L)
{
    if (L->length == 0)
        return;
    for (int i = 0; i < L->length; i++)
    {

        if (i != 0 && i % 5 == 0)
            cout << endl;
        cout << L->data[i] << " ";
    }
    cout << endl;
}
void InitSqList(SqList*&L)
{
    L = (SqList*)malloc(sizeof(SqList));
    L->length = 0;
}
int LengthSqList(SqList*L)
{
    return L->length;
}
bool IsEmptySqList(SqList* L)
{
    if (L->length == 0)
        return true;
    else
        return false;
}
bool GetElemFromi(SqList*L, int i, ElemType &e)
{
    if (i < 1 || i > L->length)
        return false;
    else
    {
        e = L->data[i - 1];
        return true;
    }
}
int GetiFromElem(SqList*L, ElemType e)
{
    int i = 0;
    for (; i < L->length, L->data[i] != e; i++);
    if (i == L->length)
        return 0;
    else
        return i + 1;
}
bool InsertElemSqList(SqList* &L, int i, ElemType e)
{
    if (i < 1 || i >L->length + 1)
        return false;
    else
    {
        i--;
        for (int j = L->length; j > i; j--)
        {
            L->data[j] = L->data[j - 1];
        }
        L->data[i] = e;
        L->length += 1;
        return true;
    }
}
bool DeleteElemFromiSqList(SqList* &L, int i, ElemType &e)
{
    if (i < 1 || i > L->length)
        return false;
    else
    {
        i--;
        e = L->data[i];
        for (int j = i; j < L->length - 1; j++)
        {
            L->data[j] = L->data[j + 1];
        }
        L->length -= 1;
        return true;
    }
}

3.实验结果

这里写图片描述

4.总结

顺序表的分析还是挺简单的
【1】初始化:用mallooc函数动态生成,指定Length= 0
【2】创建:主要涉及到遍历数组的所有元素,把数组的所有元素赋值给顺序表,最后指定顺序表的长度
【3】显示:遍历顺序表的每一个元素
【4】销毁:free
【5】求长度:其实就是return L->Length
【6】判断空:其实就是L->Length 是不是为0
【7】插入:主要涉及到数组的移动;数组的物理标号和逻辑标号的区别,比如,数组a的第一个元素,其实是a[0]
【8】删除:也是涉及到数组的移动;数组的物理标号和逻辑标号的区别,比如,数组a的第一个元素,其实是a[0]
【9】得到位置i的元素e:遍历顺序表
【10】得到元素e的位置i:遍历顺序表,直到得到e

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值