#数据结构#第二章:顺序表

判断题

1-1 .对于顺序存储的长度为N的线性表,访问结点和增加结点的时间复杂度分别对应为O(1)和O(N)。

T

1-2 .若某线性表最常用的操作是存取任一指定序号的元素和在最后进行插入和删除运算,则利用顺序表存储最节省时间。

T,时间复杂度都为O(1)

1-3 .对于顺序存储的长度为N的线性表,删除第一个元素和插入最后一个元素的时间复杂度分别对应为O(1)和O(N)。

F, 顺序表删除第一个元素的时间复杂度为O(N),插入最后一个元素的时间复杂度为O(1)

1-4 .在顺序表中逻辑上相邻的元素,其对应的物理位置也是相邻的。

T,顺序表是一种随机存储结构,所以逻辑相邻的元素在物理位置上也是相邻的

1-5 .所谓随机存取,就是通过首地址和元素的位序号值可以在O(1)的时间内找到指定的元素.

T

1-6 .顺序存储的线性表不支持随机存取。

F

1-7 .在顺序表上进行插入、删除操作时需要移动元素的个数与待插入或待删除元素的位置无关.

F,顺序表当中进行插入与删除操作与所在位置有关

选择题

2-1 对于顺序存储的长度为N的线性表,访问结点和增加结点的时间复杂度为:

A O(1), O(1)
B O(1), O(N)
C O(N), O(1)
D O(N), O(N)

B

2-2 在N个结点的顺序表中,算法的时间复杂度为O(1)的操作是:

A 访问第i个结点(1≤i≤N)和求第i个结点的直接前驱(2≤i≤N)
B 在第i个结点后插入一个新结点(1≤i≤N)
C 删除第i个结点(1≤i≤N)
D 将N个结点从小到大排序

A

2-3 .若某线性表最常用的操作是存取任一指定序号的元素和在最后进行插入和删除运算,则利用哪种存储方式最节省时间?

A 双链表
B 单循环链表
C 带头结点的双循环链表
D 顺序表

D

2-4 .顺序表中第一个元素的存储地址是100,每个元素的长度为2,则第5个元素的地址是

A.100
B 105
C 108
D 110

C

2-5 .线性表的顺序存储结构是一种

A 随机存取的存储结构
B 顺序存取的存储结构
C 索引存取的存储结构
D 散列存取的存储结构

A

2-6 .一个顺序表所占用的存储空间大小与()无关

A 表的长度
B 元素的类型
C 元素的存放顺序
D 元素中各字段的类型

C

2-7 .要将一个顺序表{a​0,a1,……,an−1}中第i个数据元素​i(0≤i≤n-1)删除,需要移动()个数据元素。

A i
B n-i-1
C n-i
D n-i+1

B

2-8 .用数组表示线性表的优点是()

A 便于插入和删除操作
B 便于随机存取
C 可以动态地分配存储空间
D 不需要占用一片相邻的存储空间

B

2-9 .若长度为n的线性表采用顺序存储结构,那么删除它的第i个数据元素之前,需要它一次向前移动()个数据元素。

A n-i
B n+i
C n-i-1
D n-i+1

A

2-10 .若长度为n的线性表采用顺序结构,在第i个数据元素之前插入一个元素,需要它依次向后移动()个元素。
A n-i

B n-i+1
C n-i-1
D i

B

2-11 .线性表L=(a1, a2 ,……,an )用一维数组表示,假定删除线性表中任一元素的概率相同(都为1/n),则删除一个元素平均需要移动元素的个数是

A n/2
B (n+1)/2
C (n-1)/2
D n

C

函数题

6-1 顺序表操作集

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

函数接口定义:
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 L;
    L = (List)malloc(sizeof(struct LNode));
    //L = new LNode;自己运没问题,但在pta上会编译错误
    L -> Last = -1;
    return L;
}//创建并返回一个新的链表

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

}
bool Insert( List L, ElementType X, Position P )
{
    if(L -> Last + 1 == MAXSIZE)//判断空间已满
    {
        printf("FULL");
        return false;
    }

    if(P < 0 || P > L -> Last + 1)//判断非法位置
    {
        printf("ILLEGAL POSITION");
        return false;
    }
    int i;
    for(i = L -> Last; i >= P; i--)
    {
        L -> Data[i + 1] = L -> Data[i];
    }
    L -> Data[P] = X;
    L -> Last++;
    return true;
}

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

6-2 合并两个有序数组

要求实现一个函数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 */

裁判测试程序样例:

#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 printArray(int* arr, int arr_size); 细节不表,说明不用写,在测试代码的时候,注释掉即可    
void merge(int* a, int m, int* b, int n, int* c)
{
    int i = 0, j = 0, k = 0;
    while(i < m || j < n)
    {
        if(i >= m)
            c[k++] = b[j++];
        else if(j >= n)
            c[k++] = a[i++];
        else if(a[i] < b[j])
            c[k++] = a[i++];
        else
            c[k++] = b[j++];
    }
}      

6-3 jmu-ds-有序表插入数据

要求实现3个函数,在递增的顺序表中插入一个新整数,并保持该顺序表的有序性。
数据结构定义
typedef int ElemType;
typedef struct
{ ElemType data[MaxSize]; //存放顺序表元素
int length ; //存放顺序表的长度
} List;
typedef List *SqList; */

函数接口定义:
void CreateSqList(SqList &L,int n);
void InsertSq(SqList &L,int x) ;
void DispSqList(SqList L);*/

需实现以上3个函数,分别为:

  • CreateSqList:创建有序表,连续输入n个正数存入有序表中。L表示顺序表指针,n表示输入数据个数。
  • InsertSq(SqList *&L,int x):顺序表L中插入数据x。
  • DispSqList:输出顺序表L中所有数据。数据间空格隔开,尾部不能有空格。

裁判测试程序样例:

#include <iostream>
#define MaxSize 50
using namespace std;
typedef int ElemType; 
typedef struct {	
     ElemType data[MaxSize];		//存放顺序表元素
   	int length ;					//存放顺序表的长度
}List;	
typedef List *SqList;
void CreateSqList(SqList &L,int n);
void InsertSq(SqList &L,int x) ;
void DispSqList(SqList L);
void DestroySqList(SqList &L);//销毁线性表
int main()
{
     int n;
	SqList L;
	cin>>n; 
	CreateSqList(L,n)  ;
	 if(n>0)
	{
	 cin>>n;
	 InsertSq(L,n) ;
    }
	 DispSqList(L);  
	 DestroySqList(L);//细节不表
}
/* 请在这里填写答案 */

输入说明

共两行。 行1:顺序表结点个数n,后面输入n个正整数。题目保证输入数是有序。 行2:输入插入的数据值。。

输出说明

输出插入新数据后的顺序表表内容,数和数之间空格隔开,尾部不能有空格。若有序表为空,输出 error

输入样例

5 1 2 6 9 10
3

输出样例

1 2 3 6 9 10

参考代码

void CreateSqList(SqList &L,int n)
{
    L = new List;
    L -> length = n;
    for(int i = 0; i < L -> length; i++)
    {
        cin>> L -> data[i];
    }
}
void InsertSq(SqList &L,int x)
{
    int flag = -1, i, j, order = 0;
    for(i = 0; i < L -> length; i++)
    {
        if(L -> data[i] >= x)
            break;
    }
    for(j = L -> length; j >= i; j--)
    {
        L -> data[j + 1] = L -> data[j];
    }
    L -> data[i] = x;
    L -> length++;

}
void DispSqList(SqList L)
{
    if(L -> length == 0)
    {
        cout<<"error"<<endl;
    }
    else
    {
        cout<<L -> data[0];
        for(int i = 1; i < L -> length; i++)
        {
            cout<<" "<<L -> data[i];
        }
        cout<<endl;
    }

}

编程题

4-2 jmu-ds-顺序表区间元素删除

若一个线性表L采用顺序存储结构存储,其中所有的元素为整数。设计一个算法,删除元素值在[x,y]之间的所有元素,要求算法的时间复杂度为O(n),空间复杂度为O(1)。

输入格式:

三行数据,第一行是顺序表的元素个数,第二行是顺序表的元素,第三行是x和y。

输出格式:

删除元素值在[x,y]之间的所有元素后的顺序表。

输入样例:

10
5 1 9 10 67 12 8 33 6 2
3 10

输出样例:
1 67 12 33 2

参考代码

#include<bits/stdc++.h>
using namespace std;

#define MaxSize 100
#define OK 1
#define ERROR 0
#define OVERFLOW -2

typedef int ElemType;
typedef struct {
    ElemType *elem;
    int length;
}SqList;

int main()
{
    SqList L, L2;
    int x, y;
    int i;
    cin>> L.length;
    L.elem = (int*)malloc(sizeof(int)*L.length);
    if(L.elem == NULL)
        return OVERFLOW;
    for(i = 0; i < L.length; i++)
    {
        cin>> L.elem[i];
    }
    cin>>x>>y;
    L2.elem=(int*)malloc(sizeof(int)*L.length);
    L2.length=0;
    int j = 0;
    for(i = 0; i < L.length; i++)
    {
        if(L.elem[i] < x || L.elem[i] > y)
        {
            L2.elem[j++] = L.elem[i];
            L2.length++;
        }
    }

    cout<<L2.elem[0];
    for(i = 1; i < L2.length; i++)
        cout<<" "<<L2.elem[i];
    cout<<endl;
}

}

7-2 最长连续递增子序列

给定一个顺序存储的线性表,请设计一个算法查找该线性表中最长的连续递增子序列。例如,(1,9,2,5,7,3,4,6,8,0)中最长的递增子序列为(3,4,6,8)。

输入格式:

输入第1行给出正整数n(≤10​5​​ );第2行给出n个整数,其间以空格分隔。

输出格式:

在一行中输出第一次出现的最长连续递增子序列,数字之间用空格分隔,序列结尾不能有多余空格。

输入样例:

15
1 9 2 5 7 3 4 6 8 0 11 15 17 17 10

输出样例:
3 4 6 8

参考代码

#include<bits/stdc++.h>
using namespace std;

#define MaxSize 100050

int main()
{
    int n, i, j;
    int elem[MaxSize], a[MaxSize];
    cin>>n;
    for(i = 0; i < n; i++)
    {
        cin>>elem[i];
        a[i] = 1;
    }
    for(i = 0; i < n - 1; i++)
    {
        for(j = i + 1; j < n; j++)
        {
            if(elem[j] > elem[j - 1])
                a[i]++;
            else
                break;
        }
    }
    int _max = 0, t;
    for(i = 0; i < n; i++)
    {
        if(a[i] > _max)
        {
            _max = a[i];
            t = i;
        }
    }
    for(i = t; i < t + _max - 1; i++)
        cout<<elem[i]<<" ";
    cout<<elem[t + _max - 1]<<endl;;
}

发布了145 篇原创文章 · 获赞 8 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/weixin_43476037/article/details/100526519