2025年SCAU数据结构——8578顺序表逆置

SCAU数据结构——8578顺序表逆置题目 Description 顺序表的基本操作代码如下 include stdio h include malloc h define OK 1 define ERROR 0 define LIST INIT SIZE 100 define LISTINCREMEN 10 define malloc h stdio h

大家好,我是讯享网,很高兴认识大家。

题目

Description
顺序表的基本操作代码如下:

#include<stdio.h> #include<malloc.h> #define OK 1 #define ERROR 0 #define LIST_INIT_SIZE 100 #define LISTINCREMENT 10 #define ElemType int typedef int Status; typedef struct { 
    int *elem; int length; int listsize; }SqList; Status InitList_Sq(SqList &L) { 
    // 算法2.3 // 构造一个空的线性表L。 L.elem = (ElemType *)malloc(LIST_INIT_SIZE*sizeof(ElemType)); if (!L.elem) return OK; // 存储分配失败 L.length = 0; // 空表长度为0 L.listsize = LIST_INIT_SIZE; // 初始存储容量 return OK; } // InitList_Sq Status ListInsert_Sq(SqList &L, int i, ElemType e) { 
    // 算法2.4 // 在顺序线性表L的第i个元素之前插入新的元素e, // i的合法值为1≤i≤ListLength_Sq(L)+1 ElemType *p; if (i < 1 || i > L.length+1) return ERROR; // i值不合法 if (L.length >= L.listsize) { 
    // 当前存储空间已满,增加容量 ElemType *newbase = (ElemType *)realloc(L.elem, (L.listsize+LISTINCREMENT)*sizeof (ElemType)); if (!newbase) return ERROR; // 存储分配失败 L.elem = newbase; // 新基址 L.listsize += LISTINCREMENT; // 增加存储容量 } ElemType *q = &(L.elem[i-1]); // q为插入位置 for (p = &(L.elem[L.length-1]); p>=q; --p) *(p+1) = *p; // 插入位置及之后的元素右移 *q = e; // 插入e ++L.length; // 表长增1 return OK; } // ListInsert_Sq Status ListDelete_Sq(SqList &L, int i, ElemType &e) { 
    // 算法2.5 // 在顺序线性表L中删除第i个元素,并用e返回其值。 // i的合法值为1≤i≤ListLength_Sq(L)。 ElemType *p, *q; if (i<1 || i>L.length) return ERROR; // i值不合法 p = &(L.elem[i-1]); // p为被删除元素的位置 e = *p; // 被删除元素的值赋给e q = L.elem+L.length-1; // 表尾元素的位置 for (++p; p<=q; ++p) *(p-1) = *p; // 被删除元素之后的元素左移 --L.length; // 表长减1 return OK; } // ListDelete_Sq 

讯享网

设有一顺序表A=(a0,a1,…, ai,…an-1),其逆顺序表定义为A’=( an-1,…, ai,…,a1, a0)。设计一个算法,将顺序表逆置,要求顺序表仍占用原顺序表的空间。

输入格式
第一行:输入顺序表的元素个数
第二行:输入顺序表的各元素,用空格分开

输出格式
第一行:逆置前的顺序表元素列表
第二行:逆置后的顺序表元素列表


讯享网

输入样例
10
1 2 3 4 5 6 7 8 9 10

输出样例
The List is:1 2 3 4 5 6 7 8 9 10
The turned List is:10 9 8 7 6 5 4 3 2 1

答案

方法1(常规)

讯享网include <iostream> #include<stdio.h> #include<malloc.h> #define OK 1 #define ERROR 0 #define LIST_INIT_SIZE 100 #define LISTINCREMENT 10 #define ElemType int using namespace std; typedef int Status; typedef struct { 
    int *elem; int length; int listsize; }SqList; Status InitList_Sq(SqList &L,int n) { 
    // 算法2.3 // 构造一个空的线性表L。 L.elem = (ElemType *)malloc(LIST_INIT_SIZE*sizeof(ElemType)); if (!L.elem) return ERROR; // 存储分配失败 L.listsize = LIST_INIT_SIZE; for(int i=0;i<n;i++)//这里要记,初始输入 cin>>L.elem[i]; L.length = n; // 初始存储容量 return OK; } // InitList_Sq Status invert(SqList &L,int n) { 
    int i,temp; for(i=0;i<n/2;i++) { 
    temp=L.elem[i]; L.elem[i]=L.elem[n-1-i]; L.elem[n-1-i]=temp; } return OK; } int main() { 
    int num,i; cin>>num; SqList X; if(InitList_Sq(X,num)) { 
    cout<<"The List is:"; for(i=0;i<num;i++) cout<<X.elem[i]<<' '; cout<<endl; } if(invert(X,num)) { 
    cout<<"The turned List is:"; for(i=0;i<num;i++) cout<<X.elem[i]<<' '; cout<<endl; } return 0; } 

方法2(投机取巧,也可用于线性链表逆置)

#include <stdio.h> #include <stdlib.h> int main() { 
    int i,t,n; scanf("%d",&n); int a[n]; for(i=0;i<n;i++) { 
    scanf("%d",&a[i]); } printf("The List is:"); for(i=0;i<n;i++) { 
    printf("%d ",a[i]); } printf("\n"); for(i=0;i<=(n-1)/2;i++) { 
    t=a[i]; a[i]=a[n-1-i]; a[n-1-i]=t; } printf("The turned List is:"); for(i=0;i<n;i++) { 
    printf("%d ",a[i]); } return 0; } 
小讯
上一篇 2025-02-14 07:19
下一篇 2025-04-06 11:29

相关推荐

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容,请联系我们,一经查实,本站将立刻删除。
如需转载请保留出处:https://51itzy.com/kjqy/46044.html