天天看點

王道資料結構應用題1

#include<stdio.h>

#include<stdlib.h>

//靜态配置設定

#define MaxSize 50

typedef int ElemType;

typedef struct{

ElemType data[MaxSize];

int length;

}SqList;

bool Del_Min(SqList &L,ElemType &value)

{

if(L.length==0)

return false;

value=L.data[0];

int pos=0;

for(int i=1;i<L.length;i++)

if(L.data[i]<value)

{

value=L.data[i];

pos=i;

}

L.data[pos]=L.data[L.length-1];
L.length--;
return true;
           

}

void PrintList(SqList &L){

for(int i=0;i<L.length;i++){

printf("%4d",L.data[i]);

}

printf("\n");

}

int main(){

SqList L;

bool ret;

L.data[0]=3;

L.data[1]=2;

L.data[2]=1;

L.data[3]=4;

L.data[4]=5;

L.length=5;

ElemType minvalue;

PrintList(L);

ret=Del_Min(L,minvalue);

if(ret){

printf(“删除成功\n”);

printf(“删除元素值為%d\n”,minvalue);

PrintList(L);

}else{

printf(“删除失敗\n”);

}

return 0;

}

僅供自己學習記錄使用