求栈中的最大值
利用一个辅助的顺序表来
先定义栈和顺序表
typedef struct _Seqlist { DataType array[MaxNum]; int len; }Seqlist,*PSeqlist; typedef struct _Stack { DataType array[MaxNum]; int len; }Stack,*PStack;
入栈出栈时,都更新顺序表
int PushStack(PStack sta,DataType key,PSeqlist list) { if (sta->len > MaxNum) { printf("Out of Stack!\n"); return Empty; } else { sta->array[sta->len++] = key; if ((list->len > 0) && key > list->array[list->len-1]) { list->array[list->len++] = key; } else { list->array[list->len] = list->array[list->len-1]; list->len++; } } return list->array[list->len-1]; } int PopStack(PStack sta,PSeqlist list) { if (sta->len < 1) { printf("Lilk of Stack!\n"); return Empty; } else { int ret; sta->array[--sta->len] = 0; ret = list->array[--list->len-1]; return ret; } }