数据结构与算法导论(C++)连载(一)--线性表的顺序存储(结构体实现和封装类实现)

本文介绍了线性表的两种顺序存储方式在C++中的实现,包括使用普通结构体和封装类的方法。通过具体代码展示如何在C++中创建和操作顺序数组存储的线性表。
摘要由CSDN通过智能技术生成

        线性表存储大概有两类,一个是顺序数组存储,另一个是指针链式存储。这里主要是采用C++编程实现顺序数组存储,优点主要是简单,易懂,操作容易。
        一、普通结构体实现
        在xxx.h文件中:
        

```
#include <iostream>

using namespace std;

#define MAXSIZE 1024

typedef struct LNode *List;
struct LNode
{
    int Data[MAXSIZE];
    int Last;
};

List MakeEmpty();
int Find(int x, List PtrL);
void Insert(int x, int i, List PrtL);
void Delete(int i, List PtrL);
void Print(List PtrL);
```

        在xxx.cpp中:
        

```

/* 线性表的顺序存储实现
利用数组的连续存储空间顺序存放线性表的各元素*/
#include"xxx.h"

/*************************************************************
* Name:    MakeEmpty
* Features:初始化
**************************************************************/
List MakeEmpty()
{
    List PtrL;
    PtrL = (List)malloc(sizeof(struct LNode));
    PtrL->Last = -1;
    return PtrL;
}

/*************************************************************
* Name:    Find
**************************************************************/
int Find(int x,List PtrL)
{
    int i = 0;
    while (i<=PtrL->Last && PtrL->Data[i] != x)
        i++;
    if (i > PtrL->Last) return -1;
    else return i;
}

/*************************************************************
* Name:    Insert
* Features:插入
**************************************************************/
void Insert(int x, int i, List PrtL)
{
    if (PrtL->Last == MAXSIZE - 1)
        printf("NOT SPACE\n");
    if (i<1 || i > PrtL->Last + 2)
        cout << "error\n";
    for (int j = PrtL->Last; j >= i - 1; j--)  //向后移动 
        PrtL->Data[j + 1] = PrtL->Data[j];
    PrtL->Data[i - 1] = x;    //插入新的值
    PrtL->Last++;  //指向最后的元素
}

/*************************************************************
* Name:    Delete
* Features:删除
**************************************************************/
void Delete(int i, List PtrL)
{
    if (i<1 || i>PtrL->Last + 1)
        printf("error");
    for (int j = i; j <= PtrL->Last; j++)
        PtrL->Data[j - 1] = PtrL->Data[j];
    PtrL->Last--;
}

/*************************************************************
* Name:    Print
* Features:输出一个线性表
**************************************************************/
void Print(List PtrL)
{
    int i = 0;
    i = PtrL->Last;
    if (i == -1)
    {
        cout << "error\n";
        return;
    }
    for (size_t j = 0; j <= i; j++)
    {
        cout << PtrL->Data[j]<<' ';
    }
}
```


        在main.cpp中:
        

```
#include"xxx.h"
int main()
{
    List L=NULL;
    L = MakeEmpty();
    PtrL = Insert(1, 1, PtrL);
    ...//做对应的操作
    system("pause");
    return 0;
}
```


        二、封装类实现
        创建一个类List_Order。
        在List_Order.h中:
        

```
#include"iostream"

using namespace std;
#define MAXSIZE 1024
#define ElementType int

class List_Order
{
public:
    List_Order();
    ~List_Order();
    void MakeEmpty();
    int Find(ElementType x);
    void Insert(ElementType x, int i);
    void Delete(int i);
    void Print();
private:
    ElementType Data[MAXSIZE];
    int Last;
};
```


        在List_Order.cpp中:
        

```
#include "List_Order.h"

List_Order::List_Order()
{
}


List_Order::~List_Order()
{
}

void List_Order::MakeEmpty()
{
    Last = -1; //初始化指向-1
}

int List_Order::Find(ElementType x)
{
    int i = 0;
    while (i <= Last && Data[i] != x)
        i++;
    if (i > Last) return -1;
    else return i;
}

void List_Order::Insert(ElementType x, int i)  //i应从1开始但数组是从0开始存储
{
    if (Last == MAXSIZE - 1) cout << "NOT SPACE\n";
    else if (i<1 || i>Last + 2) cout << "error\n";
    else
    {
        for (int j = Last; j >=i-1; j--) //向后移动数组
            Data[j + 1] = Data[j];
        Data[i - 1] = x;
        Last++;
    }
}

void List_Order::Delete(int i)
{
    if (i<1 || i>Last + 1) cout << "error\n";
    else
    {
        for (int j = i-1; j < Last; j++) //逐步覆盖
            Data[j] = Data[j+1];
        Last--;
    }
}

void List_Order::Print()
{
        int i = 0;
        i = Last;
        if (i == -1)
        {
            cout << "error\n";
            return;
        }
        for (size_t j = 0; j <= i; j++)
        {
            cout << Data[j]<<' ';
        }
}
//以上的函数介绍参考一
```


        在main.cpp中:
        

```
#include"List_Order.h"


int main()
{
    List_Order List;
    List.MakeEmpty();
    List.Insert(1, 1); List.Insert(2, 2); List.Insert(3, 3); List.Insert(4, 2); List.Insert(5, 1);
    List.Print();
    List.Delete(2);
    cout << "\n";
    List.Print();
    cout << "\n";
    cout<<List.Find(3)+1<<"\n";
    system("pause");
    return 0;
}
```

        三、运行结果

         



        
  线性表链式存储:https://mp.csdn.net/postedit/89436562

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值