• 【PTA】6-5 链式表操作集 (20分)


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

    裁判测试程序样例:

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

    输入样例:

    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 Position Find( List L, ElementType X ){
     2     //if(L==NULL)return NULL;
     3     while(L&&L->Data!=X){
     4         L=L->Next;
     5     }
     6     if(L==NULL){
     7         return NULL;
     8     }else if(L->Data==X){
     9         return L;
    10     }
    11 }
    12 
    13 List Insert( List L, ElementType X, Position P ){
    14     Position temp=(Position)malloc(sizeof(struct LNode));
    15     if(L==P){
    16         temp->Data=X;temp->Next=P;
    17         return temp;
    18     }
    19     List Q=L;
    20     while(L->Next&&L->Next!=P)L=L->Next;
    21     if(P==NULL){
    22         temp->Data=X;temp->Next=NULL;
    23         L->Next=temp;
    24         return Q; 
    25         
    26     }else if(P &&L->Next==NULL){
    27         printf("Wrong Position for Insertion
    ");
    28         return NULL;
    29     }
    30      temp->Data=X;temp->Next=P;
    31      L->Next=temp;
    32      return Q;
    33 }
    34 List Delete( List L, Position P ){
    35     Position Q=L;
    36     if (L == P) {
    37         L = L->Next;
    38         free(P);
    39         return L;
    40     }
    41     while (Q->Next != P && Q->Next != NULL)Q = Q->Next;
    42     if(Q->Next==NULL){
    43            printf("Wrong Position for Deletion
    ");
    44            return ERROR;
    45      }else{
    46          Q->Next=P->Next;
    47            free(P);
    48          return L;
    49       }
    50 }
  • 相关阅读:
    Python学习笔记(2)
    Python学习笔记(1)
    2020年5月记于博茨瓦纳
    20145208 蔡野 《网络对抗》免考项目 MSF学习
    20145208 蔡野 《网络对抗》Exp9 web安全基础实践
    20145208 蔡野 《网络对抗》Exp8 Web基础
    20145208 蔡野 《网络对抗》Exp7 网络欺诈技术防范
    20145122 《Java程序设计》课程总结
    20145122《 Java网络编程》实验五实验报告
    20145122 《Java程序设计》第十周学习总结
  • 原文地址:https://www.cnblogs.com/wyjgr/p/13073447.html
Copyright © 2020-2023  润新知