6-2 顺序表操作集 (20 point(s))
本题要求实现顺序表的操作集。
函数接口定义:
List MakeEmpty();
Position Find( List L, ElementType X );
bool Insert( List L, ElementType X, Position P );
bool Delete( List L, Position P );
其中List
结构定义如下:
typedef int Position;
typedef struct LNode *List;
struct LNode {
ElementType Data[MAXSIZE];
Position Last; /* 保存线性表中最后一个元素的位置 */
};
各个操作函数的定义为:
List MakeEmpty()
:创建并返回一个空的线性表;
Position Find( List L, ElementType X )
:返回线性表中X的位置。若找不到则返回ERROR;
bool Insert( List L, ElementType X, Position P )
:将X插入在位置P并返回true。若空间已满,则打印“FULL”并返回false;如果参数P指向非法位置,则打印“ILLEGAL POSITION”并返回false;
bool Delete( List L, Position P )
:将位置P的元素删除并返回true。若参数P指向非法位置,则打印“POSITION P EMPTY”(其中P是参数值)并返回false。
在标准C语言中,线性表通过定义一个结构,以及一个指针,通过动态内存分配来建立线性表
List MakeEmpty()
{
List L = (List)malloc(sizeof(struct LNode));
L->Last = -1;
return L;
}
顺序查找通过顺序遍历线性表,查找指定元素
Position Find(List L, ElementType X)
{
if (L->Last == -1) // 如果线性表为空
return ERROR;
for (int i = 0; i <= L->Last; i++)
{
if (L->Data[i] == X)
return i;
}
return ERROR;
}
线性表的插入,需要先把插入点的元素集体后移,在插入到该位置。移动时,是从表尾开始移动
bool Insert(List L, ElementType X, Position P)
{
if (L->Last+1 == MAXSIZE) // 线性表已满
{
printf("FULL");
return false;
}
if (P<0 || P>L->Last + 1) // 不合法插入的位置
{
printf("ILLEGAL POSITION");
return false;
}
// 特殊情况 P=L->Last+1 直接插在表尾
for (int i = L->Last; i >= P; i--)
{
L->Data[i + 1] = L->Data[i];
}
L->Data[P] = X;
L->Last++;
return true;
}
删除位置P的元素,将P+1到L->Last的元素向前移动一个就行
bool Delete(List L, Position P)
{
if (L->Last == -1) // 线性表为空
return false;
if (P<0 || P>L->Last)
{
printf("POSITION %d EMPTY",P);
return false; // 不合法的位置
}
for (int i = P+1; i <= L->Last; i++)
{
L->Data[i-1] = L->Data[i];
}
L->Last--;
return true;
}
完整代码
#include <stdio.h>
#include <stdlib.h>
#define MAXSIZE 5
#define ERROR -1
//typedef enum { false, true } bool;
typedef int ElementType;
typedef int Position;
typedef struct LNode *List;
struct LNode
{
ElementType Data[MAXSIZE];
Position Last; /* 保存线性表中最后一个元素的位置 */
};
List MakeEmpty(); // 创建并返回一个空的线性表
Position Find(List L, ElementType X); // 返回线性表中X的位置。若找不到则返回ERROR;
bool Insert(List L, ElementType X, Position P); // 将X插入在位置P并返回true。若空间已满,则打印“FULL”并返回false;
// 如果参数P指向非法位置,则打印“ILLEGAL POSITION”并返回false;
bool Delete(List L, Position P); // 将位置P的元素删除并返回true。若参数P指向非法位置,则打印“POSITION P EMPTY”
// (其中P是参数值)并返回false。
int main()
{
List L;
ElementType X;
Position P;
int N;
L = MakeEmpty();
scanf_s("%d", &N);
while (N--)
{
scanf_s("%d", &X);
// 向线性表插入元素
if (Insert(L, X, 0) == false)
printf(" Insertion Error: %d is not in.\n", X);
}
for (int i = 0; i <= L->Last; i++)
{
printf("%d ", L->Data[i]);
}
printf("\n");
scanf_s("%d", &N);
while (N--)
{
scanf_s("%d", &X);
P = Find(L, X);
if (P == ERROR)
printf("Finding Error: %d is not in.\n", X);
else
printf("%d is at position %d.\n", X, P);
}
scanf_s("%d", &N);
while (N--)
{
scanf_s("%d", &P);
if (Delete(L, P) == false)
printf(" Deletion Error.\n");
if (Insert(L, 0, P) == false)
printf(" Insertion Error: 0 is not in.\n");
}
return 0;
}
/* 你的代码将被嵌在这里 */
List MakeEmpty()
{
List L = (List)malloc(sizeof(struct LNode));
L->Last = -1;
return L;
}
Position Find(List L, ElementType X)
{
if (L->Last == -1) // 如果线性表为空
return ERROR;
for (int i = 0; i <= L->Last; i++)
{
if (L->Data[i] == X)
return i;
}
return ERROR;
}
bool Insert(List L, ElementType X, Position P)
{
if (L->Last+1 == MAXSIZE) // 线性表已满
{
printf("FULL");
return false;
}
if (P<0 || P>L->Last + 1) // 不合法插入的位置
{
printf("ILLEGAL POSITION");
return false;
}
// 特殊情况 P=L->Last+1 直接插在表尾
for (int i = L->Last; i >= P; i--)
{
L->Data[i + 1] = L->Data[i];
}
L->Data[P] = X;
L->Last++;
return true;
}
bool Delete(List L, Position P)
{
if (L->Last == -1) // 线性表为空
return false;
if (P<0 || P>L->Last)
{
printf("POSITION %d EMPTY",P);
return false; // 不合法的位置
}
for (int i = P+1; i <= L->Last; i++)
{
L->Data[i-1] = L->Data[i];
}
L->Last--;
return true;
}