顺序表操作集
6-2 顺序表操作集 (20 分)
本题要求实现顺序表的操作集。
函数接口定义:
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。
裁判测试程序样例:
#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 );
bool Insert( List L, ElementType X, Position P );
bool Delete( List L, Position P );
int main()
{
List L;
ElementType X;
Position P;
int N;
L = MakeEmpty();
scanf("%d", &N);
while ( N-- ) {
scanf("%d", &X);
if ( Insert(L, X, 0)==false )
printf(" Insertion Error: %d is not in.\n", X);
}
scanf("%d", &N);
while ( N-- ) {
scanf("%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("%d", &N);
while ( N-- ) {
scanf("%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;
}
/* 你的代码将被嵌在这里 */
输入样例:
6
1 2 3 4 5 6
3
6 5 1
2
-1 6
输出样例:
FULL Insertion Error: 6 is not in.
Finding Error: 6 is not in.
5 is at position 0.
1 is at position 4.
POSITION -1 EMPTY Deletion Error.
FULL Insertion Error: 0 is not in.
POSITION 6 EMPTY Deletion Error.
FULL Insertion Error: 0 is not in.
1 #include <iostream> 2 using namespace std; 3 #define MAXSIZE 5 4 #define ERROR -1 5 typedef int ElementType; 6 typedef int Position; 7 typedef struct LNode *List; 8 struct LNode{ 9 ElementType Data[MAXSIZE]; 10 Position Last; /*保存线性表中最后一个元素的位置*/ 11 12 }; 13 List MakeEmpty(); 14 Position Find(List L, ElementType X); 15 bool Insert(List L, ElementType X, Position P); 16 bool Delete(List L, Position P); 17 int main(){ 18 List L; 19 ElementType X; 20 Position P; 21 int N; 22 L = MakeEmpty(); 23 cin >> N; 24 while(N--){ 25 cin >> X; 26 if(Insert(L, X, 0) == false){ 27 cout << "InSertion Error:" << X << " is not in." << endl; 28 } 29 } 30 cin >> N; 31 while(N--){ 32 cin >> X; 33 P = Find(L, X); 34 if(P == ERROR){ 35 cout << "Finding Error:" << X << " is not in." << endl; 36 37 } 38 else{ 39 cout << X << "is at position " << P << endl; 40 } 41 } 42 cin >> N; 43 while(N--){ 44 cin >> P; 45 if(Delete(L, P) == false){ 46 cout << "Deletion Error." << endl; 47 } 48 if(Insert(L, 0, P) == false){ 49 cout << "Insertion Error: 0 is not in." << endl; 50 } 51 } 52 return 0; 53 } 54 List MakeEmpty(){ 55 List L; 56 L = new struct LNode();//(List)molloc(sizeof(struct Node)) 57 L->Last = -1; 58 return L; 59 } 60 Position Find(List L, ElementType X){ 61 Position i = 0; 62 while(i <= L->Last && L->Data[i] != X){ 63 i++; 64 } 65 if(i > L->Last) return ERROR; 66 else return i; 67 } 68 bool Insert(List L, ElementType X, Position P){ 69 Position i; 70 if(L->Last == MAXSIZE - 1){ 71 cout << "FULL"; 72 return false; 73 } 74 if(P < 0 || P > L->Last + 1){ 75 cout << "ILLEGAL POSITION"; 76 return false; 77 } 78 for(i = L->Last; i >= P; i--){ 79 L->Data[i + 1] = L->Data[i]; 80 } 81 L->Data[P] = X; 82 L->Last++; 83 return true; 84 } 85 bool Delete(List L, Position P){ 86 Position i; 87 if(P < 0 || P > L->Last){ 88 cout << "POSITION " << P << " EMPTY"; 89 return false; 90 } 91 for(i = P + 1; i <= L->Last; i++){ 92 L->Data[i - 1] = L->Data[i]; 93 } 94 L->Last--; 95 return true; 96 }
浙公网安备 33010602011771号