#include <stdio.h>
#include <stdlib.h>
#define TRUE 1
#define FALSE 0
#define ERROR 0
#define OK 1
#define OVERFLOW -2
typedef int Status;
typedef int ElemType;
typedef struct StackNode{
ElemType data;
struct StackNode *next;
}StackNode,*LinkStack;
Status InitStack(LinkStack *s){
(*s) = NULL;
return OK;
}
Status Push(LinkStack *s,ElemType e){
LinkStack p = (LinkStack)malloc(sizeof(StackNode));
p->data = e;
p->next = (*s);
(*s) = p;
return OK;
}
Status Pop(LinkStack *s,ElemType *e){
if(!(*s))return ERROR;
*e = (*s)->data;
(*s) = (*s)->next;
return OK;
}
ElemType Max(LinkStack s){
if(s->next ==NULL){
return s->data;
}else{
int max;
max = Max(s->next);
return s->data>max?s->data:max;
}
}
int Length(LinkStack s) {
if (s == NULL) {
return 0;
} else {
return 1 + Length(
链表递归方法求平均值,最大值,表长
最新推荐文章于 2023-04-01 22:35:02 发布
本文介绍如何使用递归方法在C++中处理链表,包括计算链表元素的平均值、找出最大值以及确定链表的长度。通过对链表节点的递归遍历,实现高效且简洁的算法。
摘要由CSDN通过智能技术生成