6-1 顺序表创建和就地逆置

 本题要求实现顺序表的创建和就地逆置操作函数。L是一个顺序表,函数ListCreate_Sq(SqList &L)用于创建一个顺序表,函数ListReverse_Sq(SqList &L)是在不引入辅助数组的前提下将顺序表中的元素进行逆置,如原顺序表元素依次为1,2,3,4,则逆置后为4,3,2,1

裁判测试程序样例: 

//库函数头文件包含
#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>

//函数状态码定义
#define TRUE        1
#define FALSE       0
#define OK          1
#define ERROR       0
#define INFEASIBLE -1
#define OVERFLOW   -2

typedef int  Status;

//顺序表的存储结构定义
#define LIST_INIT_SIZE  100
#define LISTINCREMENT   10
typedef int ElemType;  //假设线性表中的元素均为整型
typedef struct{
    ElemType* elem;   //存储空间基地址
    int length;       //表中元素的个数
    int listsize;     //表容量大小
}SqList;    //顺序表类型定义

Status ListCreate_Sq(SqList &L);
void ListReverse_Sq(SqList &L);

int main() {
    SqList L;
    ElemType *p;

    if(ListCreate_Sq(L)!= OK) {
        printf("ListCreate_Sq: 创建失败!!!\n");
        return -1;
    }
    
    ListReverse_Sq(L);

    if(L.length){
    for(p=L.elem;p<L.elem+L.length-1;++p){
        printf("%d ",*p);
    }
    printf("%d",*p); 
    }
    return 0;
}
/* 请在这里填写答案 */
 

 

 

数据结构中最基础的题目。

首先是顺序表的创建,根据所给的主函数,要考虑创建失败的情景,所以在分配空间时如果分配失败要返回error。另外如果输入的数据大于了一开始分配的空间,要使用realloc函数重新分配空间。

malloc函数的用法:

指针  = (返回给指针的数据类型)malloc(需要的空间大小)

realloc函数的用法:

指针 = (返回给指针的数据类型)realloc(接收地址的指针名字,重新分配的空间大小)。

分配完空间以后就是常规的输入数据。创建顺序表就完成了。

然后是顺序表的逆置,不论输入的元素个数是奇数个还是偶数个,我们进行操作的对象都只有前1/2,因为逆置是前面和后面对应位置两两呼唤。所以创建一个临时空间进行互换就可以了。

Status ListCreate_Sq(SqList& L)
    {
        scanf("%d", &L.length);
        int a;
        L.elem = (ElemType*)malloc(sizeof(ElemType) * LIST_INIT_SIZE);
        if (!L.elem) exit(ERROR);
        L.listsize = LIST_INIT_SIZE;
        if (L.length == L.listsize)
        {
            L.elem = (ElemType*)realloc(L.elem, sizeof(ElemType) * (10 + LIST_INIT_SIZE));
            L.listsize += 10;
        }
        for (ElemType i = 0; i < L.length; i++)
        {
            scanf("%d", &a);
            L.elem[i] = a;
        }
        return OK;
    }
    void ListReverse_Sq(SqList& L)
    {
        ElemType vec;
        for (ElemType i = 0; i < L.length / 2; i++)
        {
            vec = L.elem[i];
            L.elem[i] = L.elem[L.length - i - 1];
            L.elem[L.length - i - 1] = vec;
        }
    }

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是Java顺序表就地逆置的示例代码: ```java public class SeqListReverse { public static void main(String[] args) { SeqList list = new SeqList(); list.insert(1); list.insert(2); list.insert(3); list.insert(4); list.insert(5); System.out.println("原始顺序表:" + list.toString()); reverse(list); System.out.println("逆置后顺序表:" + list.toString()); } public static void reverse(SeqList list) { int length = list.size(); for (int i = 0; i < length / 2; i++) { int temp = list.get(i); list.set(i, list.get(length - i - 1)); list.set(length - i - 1, temp); } } } class SeqList { private int[] array; private int size; public SeqList() { array = new int[10]; size = 0; } public void insert(int value) { if (size >= array.length) { int[] newArray = new int[array.length * 2]; System.arraycopy(array, 0, newArray, 0, array.length); array = newArray; } array[size++] = value; } public int get(int index) { if (index < 0 || index >= size) { throw new IndexOutOfBoundsException(); } return array[index]; } public void set(int index, int value) { if (index < 0 || index >= size) { throw new IndexOutOfBoundsException(); } array[index] = value; } public int size() { return size; } @Override public String toString() { StringBuilder sb = new StringBuilder(); sb.append("["); for (int i = 0; i < size; i++) { sb.append(array[i]); if (i != size - 1) { sb.append(", "); } } sb.append("]"); return sb.toString(); } } ``` 运行结果: ``` 原始顺序表:[1, 2, 3, 4, 5] 逆置后顺序表:[5, 4, 3, 2, 1] ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值