带头结点的链式表操作集
6-6 带头结点的链式表操作集 (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 struct LNode *PtrToLNode;
struct LNode {
ElementType Data;
PtrToLNode Next;
};
typedef PtrToLNode Position;
typedef PtrToLNode List;
各个操作函数的定义为:
List MakeEmpty():创建并返回一个空的线性表;
Position Find( List L, ElementType X ):返回线性表中X的位置。若找不到则返回ERROR;
bool Insert( List L, ElementType X, Position P ):将X插入在位置P指向的结点之前,返回true。如果参数P指向非法位置,则打印“Wrong Position for Insertion”,返回false;
bool Delete( List L, Position P ):将位置P的元素删除并返回true。若参数P指向非法位置,则打印“Wrong Position for Deletion”并返回false。
裁判测试程序样例:
#include <stdio.h>
#include <stdlib.h>
#define ERROR NULL
typedef enum {false, true} bool;
typedef int ElementType;
typedef struct LNode *PtrToLNode;
struct LNode {
ElementType Data;
PtrToLNode Next;
};
typedef PtrToLNode Position;
typedef PtrToLNode List;
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;
bool flag;
L = MakeEmpty();
scanf("%d", &N);
while ( N-- ) {
scanf("%d", &X);
flag = Insert(L, X, L->Next);
if ( flag==false ) printf("Wrong Answer\n");
}
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 {
flag = Delete(L, P);
printf("%d is found and deleted.\n", X);
if ( flag==false )
printf("Wrong Answer.\n");
}
}
flag = Insert(L, X, NULL);
if ( flag==false ) printf("Wrong Answer\n");
else
printf("%d is inserted as the last element.\n", X);
P = (Position)malloc(sizeof(struct LNode));
flag = Insert(L, X, P);
if ( flag==true ) printf("Wrong Answer\n");
flag = Delete(L, P);
if ( flag==true ) printf("Wrong Answer\n");
for ( P=L->Next; P; P = P->Next ) printf("%d ", P->Data);
return 0;
}
/* 你的代码将被嵌在这里 */
输入样例:
6
12 2 4 87 10 2
4
2 12 87 5
输出样例:
2 is found and deleted.
12 is found and deleted.
87 is found and deleted.
Finding Error: 5 is not in.
5 is inserted as the last element.
Wrong Position for Insertion
Wrong Position for Deletion
10 4 2 5
1 #include <iostream> 2 #include <stdio.h> 3 #include <stdlib.h> 4 5 #define ERROR NULL 6 using namespace std; 7 8 typedef int ElementType; 9 typedef struct LNode *PtrToLNode; 10 struct LNode { 11 ElementType Data; 12 PtrToLNode Next; 13 }; 14 typedef PtrToLNode Position; 15 typedef PtrToLNode List; 16 17 List MakeEmpty(); 18 Position Find( List L, ElementType X ); 19 bool Insert( List L, ElementType X, Position P ); 20 bool Delete( List L, Position P ); 21 22 int main() 23 { 24 List L; 25 ElementType X; 26 Position P; 27 int N; 28 bool flag; 29 30 L = MakeEmpty(); 31 scanf("%d", &N); 32 while ( N-- ) { 33 scanf("%d", &X); 34 flag = Insert(L, X, L->Next); 35 if ( flag==false ) printf("Wrong Answer\n"); 36 } 37 scanf("%d", &N); 38 while ( N-- ) { 39 scanf("%d", &X); 40 P = Find(L, X); 41 if ( P == ERROR ) 42 printf("Finding Error: %d is not in.\n", X); 43 else { 44 flag = Delete(L, P); 45 printf("%d is found and deleted.\n", X); 46 if ( flag==false ) 47 printf("Wrong Answer.\n"); 48 } 49 } 50 flag = Insert(L, X, NULL); 51 if ( flag==false ) printf("Wrong Answer\n"); 52 else 53 printf("%d is inserted as the last element.\n", X); 54 P = (Position)malloc(sizeof(struct LNode)); 55 flag = Insert(L, X, P); 56 if ( flag==true ) printf("Wrong Answer\n"); 57 flag = Delete(L, P); 58 if ( flag==true ) printf("Wrong Answer\n"); 59 for ( P=L->Next; P; P = P->Next ) printf("%d ", P->Data); 60 return 0; 61 } 62 List MakeEmpty(){ 63 List L = new struct LNode(); 64 L->Next = NULL; 65 return L; 66 } 67 68 Position Find(List L, ElementType X){ 69 L = L->Next; 70 while(L != NULL){ 71 if(L->Data == X){ 72 return L; 73 } 74 L=L->Next; 75 } 76 return ERROR; 77 } 78 79 bool Insert(List L, ElementType X, Position P){ 80 List head = L, pre = L; 81 L = L->Next; 82 while(L != NULL){ 83 if(L == P){ 84 List tmp = new struct LNode(); 85 tmp->Data = X; 86 tmp->Next = pre->Next; 87 pre->Next = tmp; 88 return true; 89 } 90 pre = L; 91 L = L->Next; 92 } 93 if(P == NULL){ 94 List tmp = new struct LNode(); 95 tmp->Data = X; 96 tmp->Next = NULL; 97 pre->Next = tmp; 98 return true; 99 } 100 cout << "Wrong Position for Insertion\n"; 101 return false; 102 } 103 bool Delete(List L, Position P){ 104 List head = L, pre = L; 105 L=L->Next; 106 while(L != NULL){ 107 if(L == P){ 108 pre->Next = L->Next; 109 delete L; 110 return true; 111 } 112 pre = L; 113 L = L->Next; 114 } 115 cout << "Wrong Position for Deletion\n"; 116 return false; 117 }
浙公网安备 33010602011771号