PTAデータ構造とアルゴリズムタイトルセット(中国語)4-5チェーンテーブル操作セット(20点)
3990 ワード
この問題では、チェーンテーブルの操作セットを実装する必要があります.
関数インタフェースの定義:
ここで、
各アクション関数は次のように定義されます.
審判試験プログラムのサンプル:
サンプルを入力:
出力サンプル:
プログラムコード:
関数インタフェースの定義:
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
#include
#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
");
}
scanf("%d", &N);
while ( N-- ) {
scanf("%d", &X);
P = Find(L, X);
if ( P == ERROR )
printf("Finding Error: %d is not in.
", X);
else {
L = Delete(L, P);
printf("%d is found and deleted.
", X);
if ( L==ERROR )
printf("Wrong Answer or Empty List.
");
}
}
L = Insert(L, X, NULL);
if ( L==ERROR ) printf("Wrong Answer
");
else
printf("%d is inserted as the last element.
", X);
P = (Position)malloc(sizeof(struct LNode));
tmp = Insert(L, X, P);
if ( tmp!=ERROR ) printf("Wrong Answer
");
tmp = Delete(L, P);
if ( tmp!=ERROR ) printf("Wrong Answer
");
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
プログラムコード:
Position Find( List L, ElementType X )
{
List p=L;
while(p)
{
if(p->Data==X)
return p;
p=p->Next;
}
return ERROR;
}
List Insert(List L,ElementType X,Position P)//
{
List p=L;
if(L==P)// L==P ( L==P==NULL)
{
List s=(struct LNode*)malloc(sizeof(struct LNode));
s->Data=X;
s->Next=L;
L=s;
return L;
}
if(L==NULL)
{
printf("Wrong Position for Insertion
");
return ERROR;
}
while(L->Next!=NULL&&L->Next!=P)
L=L->Next;
if(L->Next==NULL&&P!=NULL)
{
printf("Wrong Position for Insertion
");
return ERROR;
}
List s=(struct LNode*)malloc(sizeof(struct LNode));
s->Data=X;
L->Next=s;
s->Next=P;
return p;
}
List Delete( List L, Position P )
{
List p=L;
if(P==NULL||L==NULL)
{
printf("Wrong Position for Deletion
");
return ERROR;
}
if(L==P)
{
List l=L;
l=l->Next;
free(L);
return l;
}
while(L->Next!=NULL&&L->Next!=P)
{
L=L->Next;
}
if(L->Next==NULL&&P!=NULL)
{
printf("Wrong Position for Deletion
");
return ERROR;
}
L->Next=P->Next;
free(P);
return p;
}