/*给定一个带表头结点的单链表,设head为头指针,结点结构为(data,next),data为整型元素,next为指针,
试写出算法:按递增次序输出单链表中各结点的数据元素,并释放结点所占的存储空间(要求:不允许使用数组作为辅助空间)*/
#include<stdio.h>
#include<stdlib.h>
typedef int ElemType;
typedef struct LNode
{
ElemType data;
struct LNode *next;
}LNode,*LinkList;
LinkList TailCreateList(LinkList &L)
{
L = (LinkList)malloc(sizeof(LNode));
ElemType x;
LNode *r = L;
scanf("%d",&x);
while(x!=9999)
{
LNode *s = (LNode*)malloc(sizeof(LNode));
s->data = x;
r->next = s;
r = s;
scanf("%d",&x);
}
r->next = NULL;
return L;
}
LinkList delPrinMinList(LinkList &head)
{
while(head->next!=NULL)
{
LNode *pre = head;
LNode *p = pre->next;
while(p->next!=NULL)
{
if(pre->next->data>p->next->data)
pre = p;
p = p->next;
}
printf("%d ",pre->next->data);
pre->next = p->next;
free(p);
}
printf("\n");
return head;
}
bool printList(LinkList L)
{
LNode *p = L->next;
while(p!=NULL)
{
printf("%d ",p->data);
p = p->next;
}
printf("\n");
return true;
}
void main()
{
LinkList L;
TailCreateList(L);
printList(L);
delPrinMinList(L);
}