2021年10月12日 6-2 顺序表操作集 (20 分)

 裁判程序(阅读请跳过)

#include <stdio.h>
#include <stdlib.h>

#define MAXSIZE 5
#define ERROR -1
typedef enum {false, true} bool;
typedef int ElementType;
typedef int Position;
typedef struct LNode *List;
struct LNode {
    ElementType Data[MAXSIZE];
    Position Last; /* 保存线性表中最后一个元素的位置 */
};

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;

    L = MakeEmpty();
    scanf("%d", &N);
    while ( N-- ) {
        scanf("%d", &X);
        if ( Insert(L, X, 0)==false )
            printf(" Insertion Error: %d is not in.\n", X);
    }
    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
            printf("%d is at position %d.\n", X, P);
    }
    scanf("%d", &N);
    while ( N-- ) {
        scanf("%d", &P);
        if ( Delete(L, P)==false )
            printf(" Deletion Error.\n");
        if ( Insert(L, 0, P)==false )
            printf(" Insertion Error: 0 is not in.\n");
    }
    return 0;
}

/* 你的代码将被嵌在这里 */

创建空表

List MakeEmpty(){
    List L=(List)malloc(sizeof(struct LNode));
    L->Last=-1;
    return L;
}

找到元素位置并返回

(1)自己的写法

Position Find( List L, ElementType X ){
    int i,flag=0;
    if(!L)
        i=ERROR;
    for(i=0;i<=L->Last;i++){
        if(L->Data[i]==X){
            flag=1; //表示找到了
            break;//结束循环
        }
    }
    if(!flag)
        i=ERROR;//处理没找到的情况,此时flag还是0
    return i;//单一出口设置
}

很繁琐复杂,不够简洁(注意这里,L->Last和MAXSIZE不要混为一谈!!!)

(2)大佬的写法

Position Find( List L, ElementType X )
{
  for(int i=0;i<=L->Last;i++){
    if(L->Data[i]==X)
    {
      return i;
 }
}
  return ERROR;
}

相当简洁,不过是多出口,无伤大雅 


插入

bool Insert( List L, ElementType X, Position P ){
    int i;
    if(!L)
        return false;
    //表满了
    if(L->Last==MAXSIZE-1){
        printf("FULL");
        return false;
        }
    //非法位置
    else if(P<0||P>L->Last+1){
        printf("ILLEGAL POSITION");
        return false;
    }
    //正戏
    else {
        for(i=L->Last;i>=P;i--){
            //这个括号这里出了太多的问题,主要是判断条件,应该仔细思考不想当然
            L->Data[i+1]=L->Data[i];
            //这里要注意不能写成“L->Data[i]=L->Data[i-1]”,不然得改括号里的条件
        }
        //循环结束,完成元素后挪
        L->Data[P]=X;
        L->Last++;//最后下标增加
        return true;
    }
}

删除某项 

bool Delete( List L, Position P ){
    int i;
    if(!L)
        return false;
//非法位置
    if(P<0||P>L->Last){
        printf("POSITION P EMPTY");
        return false;
    }
//正片
    else{
        for(i=P;i<L->Last;i++)
//这里判断条件用 "<" or "<=" 好像都没问题?只涉及最后一个元素的操作?
        L->Data[i]=L->Data[i+1];
        L->Last--;
//改变Last的值很重要
        return true;
    }
}

总结:

 呃,一开始没搞清楚Last到底是实际顺序数(第n个)还是数组下标,看了别人写的,原来是数组下标啊,那就明白为什么第一次写的时候全错哈哈哈哈哈哈哈哈哈。

17点19分 晚饭吃啥好呢?想晔晔。。。

晚上看看波兰算数吧,有时间就写kmp(好贪心),

不想写数学,虽然数学很重要

Guess you like

Origin blog.csdn.net/LEewhITe2003/article/details/120726852
6-2