线性表_顺序存储
及四种操作
#include<iostream>
#include<cstdlib>
using namespace std;
// 顺序存储
#define MixSize 100
typedef struct LNode* List;
struct LNode{
int date[MixSize];
int Last; // last 显示数组最后一个元素的位置
};
struct LNode L; // 直接建表
List P_L; // 指针建表
// 主要操作
// 1:初始化(建空表)
List MakeEmpty()
{
List PtrL=(List)malloc(sizeof(struct LNode));
PtrL->Last=-1;
return PtrL;
}
// 2:查找元素位置
int Find(int X,List PtrL)
{
int i=0;
while(i<=PtrL->Last && PtrL->date[i]!=X)
{
i++;
}
if(i>PtrL->Last) return -1; //没找到,返回-1
else return i; // 返回元素位置
}
// 3:插入(第i个位置插入 X ) 先移动再插入
void Insert(int X,int i,List PtrL)
{
int j;
if(PtrL->Last==MixSize-1) /* 表满吗 */
{
cout<<"表满";
return;
}
if(i<1 || i>PtrL->Last+2) /* 位置合法吗 */
{
cout<<"位置不合法";
return;
}
for(j=PtrL->Last;j>=i-1;j--)
{
PtrL->date[j+1]=PtrL->date[j]; // 从尾向后移动
}
PtrL[i-1]=X; // 插入
PtrL->Last++; /* 让 Last 继续显示最后一个位置 */
return;
}
// 4:删除 (删除第 i 个位置的元素)
void del(int i,List PtrL)
{
int j;
if(i<1 || i>PtrL->Last+1)
{
cout<<"位置不合法";
return;
}
for(j=i-1;j<=MixSize-1;j++)
{
PtrL->date[j]=PtrL->date[j+1]; / 从头向前移动
}
PtrL->Last++; /* 让 Last 继续显示最后一个位置 */
}
int main()
{
return 0;
}