第2章 线性表的链式表示
综合应用题 第9题
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/f1ccd6dddb76e70235fcae20f605b654.png)
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
using namespace std;
typedef int ElemType;
typedef struct node {
ElemType data;
int len;
struct node *next;
}LNode, *LinkList;
LinkList LinkListInit() {
LNode *L;
L = (LNode *)malloc(sizeof(LNode));
if (L == NULL) {
cout<<"申请内存空间失败\n";
exit(0);
}
L->next = NULL;
L->len=0;
return L;
}
LinkList LinkListCreatT(ElemType *a, int n) {
LNode *L;
L = LinkListInit();
LNode *r;
r = L;
LNode *p;
for (int i = 0; i < n; i++){
p = (LNode *)malloc(sizeof(LNode));
if (p == NULL) {
cout<<"申请内存空间失败\n";
exit(0);
}
p->data = a[i];
p->next = r->next;
r->next = p;
r = p;
L->len++;
}
return L;
}
void del_minx(LinkList &L)
{
LNode *p,*pre,*min,*prer_min;
while(L->next!=NULL)
{
pre=L;
prer_min=L;
p=L->next;
min=L->next;
while(p!=NULL)
{
if(min->data>p->data)
{
min=p;
prer_min=pre;
}
p=p->next;
pre=pre->next;
}
prer_min->next=min->next;
cout<<min->data<<" ";
delete min;
}
}
int main() {
LinkList list, start;
int array[8] = { 8, 2, 3, 4, 5, 6, 7, 1 };
cout<<"输出单链表的数据:";
list=LinkListCreatT(array,8);
for (start = list->next; start != NULL; start = start->next) {
cout<<start->data<<" ";
}
cout<<endl;
del_minx(list);
return 0;
}
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/dac4802068607ef4967594099d50a471.png)