6-6先頭ノードのチェーンテーブル操作セット
8548 ワード
https://pintia.cn/problem-sets/15/problems/729
本題では,先頭ノードのチェーンテーブル操作セットを実現することを要求する.
関数インタフェースの定義:
ここで、
各アクション関数は次のように定義されます.
審判試験プログラムのサンプル:
サンプルを入力:
出力サンプル:
本題では,先頭ノードのチェーンテーブル操作セットを実現することを要求する.
関数インタフェースの定義:
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
#include
#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
");
}
scanf("%d", &N);
while ( N-- ) {
scanf("%d", &X);
P = Find(L, X);
if ( P == ERROR )
printf("Finding Error: %d is not in.
", X);
else {
flag = Delete(L, P);
printf("%d is found and deleted.
", X);
if ( flag==false )
printf("Wrong Answer.
");
}
}
flag = Insert(L, X, NULL);
if ( flag==false ) printf("Wrong Answer
");
else
printf("%d is inserted as the last element.
", X);
P = (Position)malloc(sizeof(struct LNode));
flag = Insert(L, X, P);
if ( flag==true ) printf("Wrong Answer
");
flag = Delete(L, P);
if ( flag==true ) printf("Wrong Answer
");
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
提交代码(not mine)
List MakeEmpty()
{
List tmp = (List)malloc(sizeof(struct LNode));
tmp->Next=NULL;
return tmp;
}
Position Find( List L, ElementType X )
{
while(L&&L->Next)
{
if(L->Data == X)
return L;
L = L->Next;
}
if(L->Data==X)
return L;
return ERROR;
}
bool Insert( List L, ElementType X, Position P )
{
List p=(List)malloc(sizeof(struct LNode));
p->Data=X;
p->Next=NULL;
List pb=L;
if(L==P)
{
p->Next=L;
L=p;
return true;
}
while(pb->Next!=P&&pb->Next!=NULL)
{
pb=pb->Next;
}
if(pb->Next==P)
{
p->Next=P;
pb->Next=p;
return true;
}
else
printf("Wrong Position for Insertion
");
return false;
}
bool Delete( List L, Position P )
{
List pb=L;
if(L==NULL)
{
printf("Wrong Position for Deletion
");
return false;
}
if(L==P)
{
L=L->Next;
return true;
}
while(pb->Next!=P&&pb->Next!=NULL)
{
pb=pb->Next;
}
if(pb->Next==P)
{
pb->Next=P->Next;
return true;
}
else
printf("Wrong Position for Deletion
");
return false;
}