考研 线性表 删除线性表的最小值 王道
程序员文章站
2022-05-16 22:06:52
...
##include <bits/stdc++.h>
#include<stdio.h>
#define MaxSize 1000
using namespace std;
typedef int ElemType;
typedef struct {
ElemType data[MaxSize];
ElemType length;
}SqList;
//初始化线性表
void InitList(SqList *&L){
L = (SqList*)malloc(sizeof(SqList)*MaxSize);
L->length = 10;
}
void PrintList(SqList* L){
for(int i = 0; i < L->length; i++)
printf("%d ",L->data[i]);
}
//删除顺序表的最小元素 返回最小值
bool deleteMin(SqList* &L, int &Min){
int index = 0;
Min = L->data[0];
if(L->length == 0){
printf("表是空表");
return false;
}
else{
for(int i = 0; i < L->length;i++){
if(Min > L->data[i]){
Min = L->data[i];
index = i;
}
}
printf("删除前:\n");
PrintList(L);
printf("\n");
L->data[index] = L->data[L->length-1];
printf("删除后:\n");
PrintList(L);
}
return true;
}
int main(){
SqList* L;
int Min = 0;
InitList(L);
printf("输入十个元素:\n");
for(int i = 0; i < 10 ; i++)
scanf("%d",&L->data[i]);
deleteMin(L, Min);
printf("\n");
printf("%d",Min);
}