PTA顺序表

 

6-1 顺序表操作集(40 分)

本题要求实现顺序表的操作集。

函数接口定义:

List MakeEmpty(); 
Position Find( List L, ElementType X );
bool Insert( List L, ElementType X, Position P );
bool Delete( List L, Position P );

其中List结构定义如下:

typedef int Position;
typedef struct LNode *List;
struct LNode {
    ElementType Data[MAXSIZE];
    Position Last; /* 保存线性表中最后一个元素的位置 */
};

各个操作函数的定义为:

List MakeEmpty():创建并返回一个空的线性表;

Position Find( List L, ElementType X ):返回线性表中X的位置。若找不到则返回ERROR;

bool Insert( List L, ElementType X, Position P ):将X插入在位置P并返回true。若空间已满,则打印“FULL”并返回false;如果参数P指向非法位置,则打印“ILLEGAL POSITION”并返回false;

bool Delete( List L, Position P ):将位置P的元素删除并返回true。若参数P指向非法位置,则打印“POSITION P EMPTY”(其中P是参数值)并返回false。

裁判测试程序样例:

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

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

输入样例:

6
1 2 3 4 5 6
3
6 5 1
2
-1 6

输出样例:

FULL Insertion Error: 6 is not in.
Finding Error: 6 is not in.
5 is at position 0.
1 is at position 4.
POSITION -1 EMPTY Deletion Error.
FULL Insertion Error: 0 is not in.
POSITION 6 EMPTY Deletion Error.
FULL Insertion Error: 0 is not in.
List MakeEmpty()
{
  List a=(List)malloc(sizeof(struct LNode));
  a->Last=0;
  return a;
} 
Position Find( List L, ElementType X )
{
    if(!L)return ERROR;
    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 )
{
    if(!L)return false;
    if(L->Last==MAXSIZE){
        printf("FULL");return false;
    }   if(P<0||P>L->Last){
        printf("ILLEGAL POSITION");return false;
    }

    for(int i=L->Last;i>P;i--){
        L->Data[i]=L->Data[i-1];
    }L->Data[P]=X;
    ++L->Last;
    return true;

}

bool Delete( List L, Position P )
{
    if(!L)return false;
    if(P>=L->Last||P<0){
        printf("POSITION %d EMPTY",P);return false;
    }
    --L->Last;
    for(int i=P;i<L->Last;i++){
        L->Data[i]=L->Data[i+1];
    }
    return true;
}

6-2 数组元素的区间删除(30 分)

给定一个顺序存储的线性表,请设计一个函数删除所有值大于min而且小于max的元素。删除后表中剩余元素保持顺序存储,并且相对位置不能改变。

函数接口定义:

int Delete( int A[], int L, int minA, int maxA );

其中A是整型数组,存储原始线性表的元素;L是表长,即A中元素的个数;minAmaxA分别为待删除元素的值域的下、上界。函数Delete应将A中所有值大于minA而且小于maxA的元素删除,同时保证表中剩余元素保持顺序存储,并且相对位置不变,最后返回删除后的表长。

裁判测试程序样例:

#include <stdio.h>

#define MAXN 20

int Delete( int A[], int L, int minA, int maxA );

int main()
{
    int A[MAXN], L, minA, maxA, i;

    scanf("%d", &L);
    for (i=0; i<L; i++) scanf("%d", &A[i]);
    scanf("%d %d", &minA, &maxA);
    L = Delete(A, L, minA, maxA);
    for (i=0; i<L; i++) printf("%d ", A[i]);
    printf("\n");

    return 0;
}

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

输入样例:

10
4 -8 2 12 1 5 9 3 3 10
0 4

输出样例:

4 -8 12 5 9 10
int Delete( int A[], int L, int minA, int maxA )
{
    int i,j,p,B[L];
    p=L;
    for(i=0,j=0;i<L;i++)
    {
        if(A[i]>=maxA||A[i]<=minA)
        {
            B[j]=A[i];
            j++;
        }
        else
        {
          p--;
        }
    }
    for(i=0;i<j;i++)
    {
      A[i]=B[i];
    }
    return p;
}

6-3 合并两个有序数组(30 分)

要求实现一个函数merge,将长度为m的升序数组a和长度为n的升序数组b合并到一个新的数组c,合并后的数组仍然按升序排列。

函数接口定义:

void printArray(int* arr, int arr_size);           /* 打印数组,细节不表 */
void merge(int* a, int m, int* b, int n, int* c);  /* 合并a和b为c */

其中a和b是按升序排列的数组,m和n分别为数组a、b的长度;c为合并后的升序数组。

裁判测试程序样例:

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

void printArray(int* arr, int arr_size);          /* 打印数组,细节不表 */
void merge(int* a, int m, int* b, int n, int* c); /* 合并a和b为c */

int main(int argc, char const *argv[])
{
    int m, n, i;
    int *a, *b, *c;

    scanf("%d", &m);
    a = (int*)malloc(m * sizeof(int));
    for (i = 0; i < m; i++) {
        scanf("%d", &a[i]);
    }

    scanf("%d", &n);
    b = (int*)malloc(n * sizeof(int));
    for (i = 0; i < n; i++) {
        scanf("%d", &b[i]);
    }
    c = (int*)malloc((m + n) * sizeof(int));
    merge(a, m, b, n, c);
    printArray(c, m + n);

    return 0;
}

/* 请在这里填写答案 */

输入样例:

输入包含两行。 第一行为有序数组a,其中第一个数为数组a的长度m,紧接着m个整数。 第二行为有序数组b,其中第一个数为数组b的长度n,紧接着n个整数。

7 1 2 14 25 33 73 84
11 5 6 17 27 68 68 74 79 80 85 87

输出样例:

输出为合并后按升序排列的数组。

1 2 5 6 14 17 25 27 33 68 68 73 74 79 80 84 85 87
void merge(int* a, int m, int* b, int n, int* c)
{
  int i=0,j=0,k=0;
  while(i+j<m+n)
  {
    if(j>=n) 
      c[k++]=a[i++];
    else if(i>=m) 
      c[k++]=b[j++];
    else if(a[i]<b[j]) 
      c[k++]=a[i++];
    else 
      c[k++]=b[j++];
  }
}

 

posted @ 2017-10-21 18:52  张晓楠  阅读(2711)  评论(0编辑  收藏  举报