/*
试设计一个算法,将单链表中值最大的那个结点删除
*/
# include <iostream>
# include <stdlib.h>
# include <stdio.h>
using namespace std;
typedef struct Node
{
int elem;
struct Node* next;
}*linklist;
void inlinklist(linklist &l, int n)
{
l = (linklist) malloc (sizeof(Node));
l->next = NULL;
linklist p, end;
end = l;
printf("请输入链表中的元素:\n");
for(int i= 0 ; i<n; i++)
{
p = (linklist) malloc (sizeof(Node));
scanf("%d", &p->elem);
end->next = p;
p->next = NULL;
end = p;
}
}
void outlinklist(linklist l)
{
printf("链表中的元素为:\n");
linklist p;
p = l->next;
while(p)
{
printf("%d ", p->elem);
p = p->next;
}
printf("\n");
}
void deletelinklist(linklist &l)
{
linklist cur, precur, max, premax;
cur = max = precur = premax = l;
cur = l->next;
max = l->next;
while(cur)
{
if(cur->elem > max->elem)
{
premax = precur;
max = cur;
}
precur = cur;
cur = cur->next;
}
linklist p;
p = max;
premax->next = max->next;
free(p);
}
int main()
{
linklist l;
int n;
printf("链表中元素个数为:\n");
scanf("%d", &n);
inlinklist(l, n);
deletelinklist(l);
outlinklist(l);
return 0;
}