线性表存储大概有两类,一个是顺序数组存储,另一个是指针链式存储。这里主要是采用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;
}
```
三、运行结果