#include <iostream.h>
#include <stdlib.h>
#define LIST_INIT_SIZE 100
#define OK 1
#define OVERFLOW -2
#define ERROR 0
typedef int Status;
typedef int ElemType;
typedef struct LNode{
ElemType data;
struct LNode *next;
} LNode,*LinkList;
Status InitList_L(LinkList &L)
{
L=new LNode;
if(!L) exit(OVERFLOW);
L->next=NULL;
return OK;
}
void ListCreat_L(LinkList &L,int n)
{
ElemType x;
LNode *p,*q;
q=L;
cout<<"input x(n)="<<endl;
for(int i=n;i>0;i--)
{
p=new LNode;
if(!p) exit(OVERFLOW);
cin>>x;
p->data=x;
p->next=NULL;
q->next=p;
q=q->next;
}
}
Status ListDelete_L(LinkList &L,int i,ElemType &e)
{
LNode *p,*q;
int j;
p=L;
j=0;
while(p->next&&j<i-1){ p=p->next;++j;}
if(!p->next|| j>i-1)
return ERROR;
q=p->next;p->next=q->next;
e=q->data;
delete q;
return OK;
}
void FindMin(LinkList &L)
{
ElemType result;
int i=1;
int minnum=1;//这里设置了一个记录最小位置的变量,这是关键
LNode *p;
p=L->next->next;
ElemType min;
if(!p) exit(ERROR);
min=L->next->data;
while (p)
{
i++;
if(p->data<=min)
{
min=p->data;
minnum=i;
}
p=p->next;
}
ListDelete_L(L,minnum,result);
cout<<"你删除的元素为"<<result<<endl;
}
void vist_L(LinkList L)
{
LNode *p=L->next;
while(p)
{
cout<<p->data<<"";
p=p->next;
}
cout<<endl;
}
void main()
{
LinkList L;
InitList_L(L);
ListCreat_L(L,5);
FindMin(L);
vist_L(L);
}