第一章 函数题


第一章函数题

6-2 顺序表基本操作 (10 分)

//库函数头文件包含
#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 ListInsert_Sq(SqList &L, int pos, ElemType e);
Status ListDelete_Sq(SqList &L, int pos, ElemType &e);
int ListLocate_Sq(SqList L, ElemType e);
void ListPrint_Sq(SqList L);

//结构初始化与销毁操作
Status InitList_Sq(SqList &L){
//初始化L为一个空的有序顺序表
L.elem=(ElemType )malloc(LIST_INIT_SIZEsizeof(ElemType));
if(!L.elem)exit(OVERFLOW);
L.listsize=LIST_INIT_SIZE;
L.length=0;
return OK;
}

int main() {
SqList L;

if(InitList_Sq(L)!= OK) {
    printf("InitList_Sq: 初始化失败!!!\n");
    return -1;
}

for(int i = 1; i <= 10; ++ i)
    ListInsert_Sq(L, i, i);

int operationNumber;  //操作次数
scanf("%d", &operationNumber);

while(operationNumber != 0) {
    int operationType;  //操作种类
    scanf("%d", & operationType);

    if(operationType == 1) {  //增加操作
        int pos, elem;
        scanf("%d%d", &pos, &elem);
        ListInsert_Sq(L, pos, elem);
    } else if(operationType == 2) {  //删除操作
         int pos; ElemType elem;
         scanf("%d", &pos);
         ListDelete_Sq(L, pos, elem);
         printf("%d\n", elem);
    } else if(operationType == 3) {  //查找定位操作
        ElemType elem;
        scanf("%d", &elem);
        int pos = ListLocate_Sq(L, elem);
        if(pos >= 1 && pos <= L.length)
            printf("%d\n", pos);
        else
            printf("NOT FIND!\n");
    } else if(operationType == 4) {  //输出操作
        ListPrint_Sq(L);
    }
   operationNumber--;
}
return 0;

}

/* 请在这里填写答案 /
typedef int ElemType;
typedef struct{
ElemType elem;
int length;//表示元素的个数
int listsize;//表示容量的大小
}SqList;
Status InitList_Sq(SqList &L)
{
L.elem=(ElemType
)malloc(LIST_INIT_SIZE
sizeof(ElemType));
if(!L.elem)exit(OVERFLOW);
L.listsize=LIST_INIT_SIZE;
L.length=0;
return OK;
}
Status ListInsert_Sq(SqList &L,int pos,ElemType e)
{
ElemType newbase;
if(pos>=1&&pos<L.length+1){
if(L.length>=L.listsize){
newbase=(ElemType
)realloc(L.elem,(L.listsize+LISTINCREMENT)*sizeof(ElemType));
if(newbase==NULL)
return ERROR;
L.elem=newbase;
L.listsize+=LISTINCREMENT;
}
ElemType *p,*q;
p=&(L.elem[pos-1]);
for(q=&(L.elem[L.length-1]);q>=p;q–){
*(q+1)=*q;//指针管用技俩
}
*p=e;
L.length++;
return OK;
}
else
return ERROR;

}
Status ListDelete_Sq(SqList &L,int pos,ElemType &e)
{
if(pos<1&&pos>L.length)
return ERROR;
else{
ElemType *p;
e=L.elem[pos-1];
for(p=&(L.elem[pos-1]);p<=&(L.elem[L.length-1]);p++){
p=(p+1);//
}
L.length–;
return Ok;
}
}
int ListLocate_Sq(SqList L, ElemType e)
{
int j;
for(j=0;j<L.length;j++)
{
if(L.elem[j]e)
break;
}
if(j
L.length)
return 0;
else
return j+1;
}
void ListPrint_Sq(SqList L)
{
for(int i=0;i<L.length;i++){
if(i==0)
printf("%d",L.elem[i]);
else
printf("%d",L.elem[i]);
}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值