程序代码:
#include<stdio.h> #include<stdlib.h> #include<malloc.h> #include<limits.h> typedef struct node{ int value; struct node * next; } Node; typedef Node * List; void InitialList( List * list ) { if( *list == NULL ) { *list = (Node *)malloc(sizeof(Node)); (*list)->value = INT_MIN; (*list)->next = NULL; } return ; } void DestroyList(List list) { if ( list == NULL ) return ; Node * p = list; Node * q = NULL ; while( ! p->next ) { q = p; p = p->next; free(q); q = NULL; } free( p ); } void InsertValue(List list, int value) { if ( NULL == list ) { printf("UnInitialized List\n"); exit(1); } Node * tail = (Node *) malloc(sizeof(Node)); tail->value = value; tail->next = NULL; Node * point = list; while( point->next) { point = point->next; } point->next = tail; } void WalkList(List list) { if( list == NULL ) return ; else{ Node * p = list->next ; while( p ) { printf("%d\t",p->value); p = p->next; } printf("\n"); } return ; } void DeleteMax(List list, Node * pre = NULL ) { static int max = INT_MIN; if ( list ) { if( max < list->value) max = list->value; DeleteMax(list->next, list ); if( max == list->value) { pre->next = pre->next->next; printf("Delete %d\n",max); free( list ); } } return ; } int main() { List list = NULL; InitialList(&list); InsertValue(list,5); InsertValue(list,4); InsertValue(list,3); InsertValue(list,5); DeleteMax(list); WalkList(list); DestroyList(list); }
写了个递归的。