学习c语言的第五天

链式表的各类操作

输入样例:

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 

 第一版本:


#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;
}

Position Find( List L, ElementType X ){
    List q=L;//定义一个指针指向链表的首端
    while(q!=NULL){
        if(q->Data==X){
            return  q;
            break;
        }
        q=q->Next;
    }
    if(q==NULL)
        return ERROR;
}

List Insert( List L, ElementType X, Position P ){
    List q=L;//定义一个指针指向链表的首端
    List p=(List)malloc(sizeof(List));
    p->Data=X;//建立一个节点p,把X赋给p,方便后续插入。
    if(P==q){
        p->Next=L;
        return p;//此为要求在首端插入的情况。
    }else {
        while(q!=NULL){
            if(q->Next==P){
                p->Next=P;
                q->Next=p;
                return L;
                break;
            }
        q=q->Next;
       }
    }
    if(q==NULL){
        printf("Wrong Position for Insertion");
        return ERROR;
    }
}

List Delete( List L, Position P ){
    List q=L;//定义一个指针指向链表的首端
    if(P==q){
        q=q->Next;
        return q;//此为在首端的情况。
    }else if(P==NULL){
        printf("Wrong Position for Deletion");
        return ERROR;
    }else{
        while(q!=NULL){
            if(q->Next==P){
                q->Next=P->Next;
                return L;
                break;
            }
        q=q->Next;
       }
    }
}

 第一版本输出结果:

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 Insertion10 4 2 5 

问题:这一版的代码确实有点复杂,并且在最后的结果上出现了问题,也不能很好满足各种不同的例子的结果,比如空表操作就不能完成。

优化目标:希望在第二版的代码中能够完成三个功能上的优化,处理空表操作上的问题等。

第二版:

#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;
}
Position Find( List L, ElementType X ){
    while(L){
        if(L->Data==X){
            return  L;
        }
        L=L->Next;
    }
    return ERROR;
}
List Insert( List L, ElementType X, Position P ){
    List q=L;//定义一个指针指向链表的首端
    List p=(List)malloc(sizeof(List));
    p->Data=X;//建立一个节点p,把X赋给p,方便后续插入。
    if(P==L){
        p->Next=L;
        return p;//此为要求在首端插入的情况。
    }
    while(q!=NULL){
        if(q->Next==P){
            p->Next=P;
            q->Next=p;
            return L;
            }
        q=q->Next;
       }
    printf("Wrong Position for Insertion\n");
    return ERROR;
}
List Delete( List L, Position P ){
    List q=L;//定义一个指针指向链表的首端
    if(P==q){
        q=q->Next;
        return q;//此为在首端的情况。
    }
    while(q!=NULL){
        if(q->Next==P){
            q->Next=P->Next;
            return L;
         }
        q=q->Next;
       }
    printf("Wrong Position for Deletion\n");
    return ERROR;
}

第二版输入:

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 

反思:通过调试发现了为什么在PTA平台上会出现输出结果错误的情况。

1.在最后的输出里没有加入\n换行。

2.在代码里用的是if 和else if等一系列我常用的判断,这种判断形式如果有情况我没有考虑到,就不会有结果。就像是如果有一个方块,我一直全部使用判断语句的话,如果有我没有考虑到的情况,就会让这个方块里有部分被忽略,但是如果我只是偶尔用判断语句,就像把这个方块分成了需要判断和不需判断的两块,最后一定是所有结果都囊括到了的,如果结果出问题的话也更方便找原因。

今天的链表练习加入了注释,但是也慢慢发现如果变量名称使用一些有标志性的字母,会更加利于理解,比如说temp就能让人很清晰了解这个变量的作用,变量如果是使用随机小写字母,就算是重新看自己的代码的时候也会有不清晰的地方,之后会加以改正,明天会继续学习顺序表或者链表的内容。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值