请写一算法,从顺序表中删除具有最小值的元素并由函数返回被删元素的值。
发布网友
发布时间:2022-05-23 22:57
我来回答
共1个回答
热心网友
时间:2024-03-09 21:15
我给你一个吧,我运行了的,没有问题。
#define _CRT_SECURE_NO_WARNINGS
#include<<a href="https://www.baidu.com/s?wd=stdio.h&tn=44039180_cpr&fenlei=mv6quAkxTZn0IZRqIHckPjm4nH00T1Y3rADvnHRYm1T4nvD3mhcz0ZwV5Hcvrjm3rH6sPfKWUMw85HfYnjn4nH6sgvPsT6K1TL0qnfK1TL0z5HD0IgF_5y9YIZ0lQzqlpA-bmyt8mh7GuZR8mvqVQL7gPYpyq8Q1cvPjRYPWmznjDsPH0kP1bvPf" target="_blank" class="-highlight">stdio.h</a>>
#include<<a href="https://www.baidu.com/s?wd=stdlib.h&tn=44039180_cpr&fenlei=mv6quAkxTZn0IZRqIHckPjm4nH00T1Y3rADvnHRYm1T4nvD3mhcz0ZwV5Hcvrjm3rH6sPfKWUMw85HfYnjn4nH6sgvPsT6K1TL0qnfK1TL0z5HD0IgF_5y9YIZ0lQzqlpA-bmyt8mh7GuZR8mvqVQL7gPYpyq8Q1cvPjRYPWmznjDsPH0kP1bvPf" target="_blank" class="-highlight">stdlib.h</a>>
#define initsize 100
typedef int Datatype;
typedef struct {
Datatype *data;
int n; //当前长度;
int maxsize; //最大容量;
}seqlist;
void Init(seqlist *L)
{
L->data = (Datatype *)malloc(sizeof(Datatype)*initsize); //动态分配;
L->n = 0;
L->maxsize = initsize;
}
void creat(seqlist *L, int n)
{
for (int i = 0; i < n; i++)
{
scanf("%d", &(L->data[i]));
L->n=n;
}
}
void removemin(seqlist *L,int*count)
{
int i, pos = 0;
for (i = 2; i <=L->n; i++)
{
if (L->data[i - 1]<L->data[pos])
pos = i - 1;
}
*count = L->data[pos];
L->n--;
L->data[pos] = L->data[L->n];
}
void show(seqlist L)
{
for (int i = 0; i < L.n; i++)
{
printf("%-3d", L.data[i]);
}
}
void main()
{
int n,back;
seqlist L ;
Init(&L);
printf("请输入你想创建的长度:");
scanf("%d", &n);
printf("请输入初始值:");
creat(&L,n);
printf("你创建的顺序表是:\n");
show(L);
removemin(&L, &back);
printf("\n最小值为:%d", back);
printf("\n删除后的顺序表是:\n");
show(L);
system("pause");
}