• 【PTA】6-6 带头结点的链式表操作集 (20分)


    【PTA】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。

    裁判测试程序样例:

     1 #include <stdio.h>
     2 #include <stdlib.h>
     3 
     4 #define ERROR NULL
     5 typedef enum {false, true} bool;
     6 typedef int ElementType;
     7 typedef struct LNode *PtrToLNode;
     8 struct LNode {
     9     ElementType Data;
    10     PtrToLNode Next;
    11 };
    12 typedef PtrToLNode Position;
    13 typedef PtrToLNode List;
    14 
    15 List MakeEmpty(); 
    16 Position Find( List L, ElementType X );
    17 bool Insert( List L, ElementType X, Position P );
    18 bool Delete( List L, Position P );
    19 
    20 int main()
    21 {
    22     List L;
    23     ElementType X;
    24     Position P;
    25     int N;
    26     bool flag;
    27 
    28     L = MakeEmpty();
    29     scanf("%d", &N);
    30     while ( N-- ) {
    31         scanf("%d", &X);
    32         flag = Insert(L, X, L->Next);
    33         if ( flag==false ) printf("Wrong Answer
    ");
    34     }
    35     scanf("%d", &N);
    36     while ( N-- ) {
    37         scanf("%d", &X);
    38         P = Find(L, X);
    39         if ( P == ERROR )
    40             printf("Finding Error: %d is not in.
    ", X);
    41         else {
    42             flag = Delete(L, P);
    43             printf("%d is found and deleted.
    ", X);
    44             if ( flag==false )
    45                 printf("Wrong Answer.
    ");
    46         }
    47     }
    48     flag = Insert(L, X, NULL);
    49     if ( flag==false ) printf("Wrong Answer
    ");
    50     else
    51         printf("%d is inserted as the last element.
    ", X);
    52     P = (Position)malloc(sizeof(struct LNode));
    53     flag = Insert(L, X, P);
    54     if ( flag==true ) printf("Wrong Answer
    ");
    55     flag = Delete(L, P);
    56     if ( flag==true ) printf("Wrong Answer
    ");
    57     for ( P=L->Next; P; P = P->Next ) printf("%d ", P->Data);
    58     return 0;
    59 }
    60 /* 你的代码将被嵌在这里 */

    输入样例:

    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 List MakeEmpty(){
     2     List L=(List)malloc(sizeof(struct LNode));
     3     L->Data=0;
     4     L->Next=NULL;
     5     return L;
     6 }
     7 Position Find( List L, ElementType X ){
     8     if (L->Next==NULL)return NULL;
     9     L=L->Next;
    10     while(L&&L->Data!=X){
    11         L=L->Next;
    12     }
    13     if(L==NULL){
    14         return NULL;
    15     }else if(L->Data==X){
    16         return L;
    17     }
    18 }
    19  
    20 bool Insert( List L, ElementType X, Position P ){
    21     Position temp=(Position)malloc(sizeof(struct LNode));
    22     if(L->Next==P){
    23         temp->Data=X;temp->Next=P;
    24         L->Next=temp;
    25         return true;
    26     }
    27     List Q=L->Next;
    28     while(Q->Next!=NULL&&Q->Next!=P)Q=Q->Next;
    29     if(P==NULL){
    30         temp->Data=X;temp->Next=NULL;
    31         Q->Next=temp;
    32         return true; 
    33         
    34     }else if(P &&Q->Next==NULL){
    35         printf("Wrong Position for Insertion
    ");
    36         return false;
    37     }
    38      temp->Data=X;temp->Next=P;
    39      Q->Next=temp;
    40      return true;
    41 }
    42  
    43 bool Delete( List L, Position P ){
    44     if (L->Next==NULL)return false;
    45     List Q=L->Next;
    46     if(P==Q){
    47         L->Next=P->Next;
    48         free(P);
    49         return true;
    50     }
    51     while(Q->Next!=P&&Q->Next!=NULL)
    52     {
    53         Q=Q->Next;
    54     }
    55     if(Q->Next==NULL){
    56            printf("Wrong Position for Deletion
    ");
    57            return false;
    58      }else{
    59          Q->Next=P->Next;
    60            free(P);
    61          return true;
    62       }
    63 }
  • 相关阅读:
    团队题目及相关介绍
    团队介绍
    寒假8
    寒假作业七
    寒假7
    寒假作业六
    寒假6
    寒假作业五
    寒假5
    寒假作四
  • 原文地址:https://www.cnblogs.com/wyjgr/p/13073480.html
Copyright © 2020-2023  润新知