6-5 链式表操作集 (20 分)
本题要求实现链式表的操作集。
函数接口定义:
Position Find( List L, ElementType X );
List Insert( List L, ElementType X, Position P );
List Delete( List L, Position P );
其中List
结构定义如下:
typedef struct LNode *PtrToLNode;
struct LNode {
ElementType Data;
PtrToLNode Next;
};
typedef PtrToLNode Position;
typedef PtrToLNode List;
各个操作函数的定义为:
Position Find( List L, ElementType X )
:返回线性表中首次出现X的位置。若找不到则返回ERROR;
List Insert( List L, ElementType X, Position P )
:将X插入在位置P指向的结点之前,返回链表的表头。如果参数P指向非法位置,则打印“Wrong Position for Insertion”,返回ERROR;
List Delete( List L, Position P )
:将位置P的元素删除并返回链表的表头。若参数P指向非法位置,则打印“Wrong Position for Deletion”并返回ERROR。
裁判测试程序样例:
#include <stdio.h>
#include <stdlib.h>
#define ERROR NULL
typedef int ElementType;
typedef struct LNode *PtrToLNode;
struct LNode {
ElementType Data;
PtrToLNode Next;
};
typedef PtrToLNode Position;
typedef PtrToLNode List;
Position Find( List L, ElementType X );
List Insert( List L, ElementType X, Position P );
List Delete( List L, Position P );
int main()
{
List L;
ElementType X;
Position P, tmp;
int N;
L = NULL;
scanf("%d", &N);
while ( N-- ) {
scanf("%d", &X);
L = Insert(L, X, L);
if ( L==ERROR ) 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 {
L = Delete(L, P);
printf("%d is found and deleted.\n", X);
if ( L==ERROR )
printf("Wrong Answer or Empty List.\n");
}
}
L = Insert(L, X, NULL);
if ( L==ERROR ) printf("Wrong Answer\n");
else
printf("%d is inserted as the last element.\n", X);
P = (Position)malloc(sizeof(struct LNode));
tmp = Insert(L, X, P);
if ( tmp!=ERROR ) printf("Wrong Answer\n");
tmp = Delete(L, P);
if ( tmp!=ERROR ) printf("Wrong Answer\n");
for ( P=L; 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 <stdio.h> 2 #include <stdlib.h> 3 #include <iostream> 4 #define ERROR NULL 5 using namespace std; 6 typedef int ElementType; 7 typedef struct Node *PtrToNode; 8 struct Node { 9 ElementType Data; 10 PtrToNode Next; 11 }; 12 typedef PtrToNode Position; 13 typedef PtrToNode List; 14 15 Position Find( List L, ElementType X ); 16 List Insert( List L, ElementType X, Position P ); 17 List Delete( List L, Position P ); 18 19 int main() 20 { 21 List L; 22 ElementType X; 23 Position P, tmp; 24 int N; 25 26 L = NULL; 27 scanf("%d", &N); 28 while ( N-- ) { 29 scanf("%d", &X); 30 L = Insert(L, X, L); 31 if ( L==ERROR ) printf("Wrong Answer\n"); 32 } 33 scanf("%d", &N); 34 while ( N-- ) { 35 scanf("%d", &X); 36 P = Find(L, X); 37 if ( P == ERROR ) 38 printf("Finding Error: %d is not in.\n", X); 39 else { 40 L = Delete(L, P); 41 printf("%d is found and deleted.\n", X); 42 if ( L==ERROR ) 43 printf("Wrong Answer or Empty List.\n"); 44 } 45 } 46 L = Insert(L, X, NULL); 47 if ( L==ERROR ) printf("Wrong Answer\n"); 48 else 49 printf("%d is inserted as the last element.\n", X); 50 P = (Position)malloc(sizeof(struct Node)); 51 tmp = Insert(L, X, P); 52 if ( tmp!=ERROR ) printf("Wrong Answer\n"); 53 tmp = Delete(L, P); 54 if ( tmp!=ERROR ) printf("Wrong Answer\n"); 55 for ( P=L; P; P = P->Next ) printf("%d ", P->Data); 56 return 0; 57 } 58 //返回线性表中首次出现X的位置。若找不到则返回ERROR; 59 Position Find(List L, ElementType X){ 60 List p = L; 61 while(p != NULL && (p->Data != X)){ 62 p = p->Next; 63 } 64 //return p; 65 if(p){ 66 return p; 67 }else{ 68 return ERROR; 69 } 70 } 71 /* 72 将x插入在位置P指向的结点之前,返回链表的表头。 73 如果参数P指向非法的位置,则打印"Wrong Posion for Insertion" , 74 返回ERROR; 75 */ 76 List Insert(List L, ElementType X, Position P){ 77 List tmp; 78 List pre; 79 //先考虑特殊情况:P插入在表头,没有前面的节点,直接申请填装 80 if(P == L){ 81 tmp = (List)malloc(sizeof(struct Node));//(List)malloc(sizeof(struct Node)); 82 tmp->Data = X; 83 tmp->Next = P; 84 return tmp; 85 } 86 //2查找P-1个节点,如果不存在就无法插入 87 //查找P的前一个节点 88 for(pre = L; pre && pre->Next != P; pre = pre->Next); 89 if(pre == NULL){ 90 /*P所指的节点不在L中*/ 91 cout << "Wrong Position for Insertion\n" ; 92 return ERROR; 93 } 94 //存在P-1个节点,申请,填装节点 95 //4将新节点装在P-1个节点的后面 96 else{/*找到了P的前一个结点pre*/ 97 /*在P前插入新节点*/ 98 tmp = (Position)malloc(sizeof(struct Node));//(Position)malloc(sizeof(struct Node)) 99 tmp->Data= X; 100 tmp->Next = P; 101 pre->Next = tmp; 102 return L; 103 } 104 105 } 106 List Delete(List L, Position P){ 107 List removeP, temP; 108 /*如果删除的是头的话*/ 109 if(P == L){ 110 removeP = L; 111 if(L != NULL){ 112 L = L->Next; 113 } 114 //释放空间 115 free(removeP); 116 return L; 117 } 118 /*删除第P个节点*/ 119 //1首先找到P-1个节点,用temP指向 120 for(temP = L; temP && temP->Next != P; temP = temP->Next); 121 if(temP == NULL){ 122 cout << "Wrong Position for Deletion\n"; 123 return ERROR; 124 } 125 if(temP->Next == NULL){ 126 cout << "Wrong Position for Deletion\n"; 127 return ERROR; 128 } 129 //2将删除的P给removeP 130 removeP = temP->Next; 131 //3将temP的next赋值为remoP的next; 132 temP->Next = removeP->Next; 133 //4释放removeP的空间 134 free(removeP); 135 //5返回 136 return L; 137 }