求数据结构栈简单程序,送300积分

来源:百度知道 编辑:UC知道 时间:2024/05/21 09:34:25
求C语言完整程序,题目:给定一个栈S,编写一个函数算法求此栈的元素个数(要求分别使用递归和非递归实现)
可以参考我的算法

方法1:非递归算法:

staitic noss(&s)

{ int c=0;

while(s.top!=s.base) { c++; pop(s);}

return c; }

方法2:递归算法:

static nos2(&s)

{ if (gettop(s)!=NULL) { pop(s); nos2=1+nos2(s); }

else nos2=0;

}

#include <stdio.h>
#include <malloc.h>
#include <iostream.h>

struct SeqStack
{
int MAXNUM;
int t;
int *s;
};

typedef struct SeqStack * PSeqStack;

PSeqStack createEmptyStack_seq(int m)
{
PSeqStack pastack =(PSeqStack)malloc(sizeof(struct SeqStack));
if(pastack!=NULL)
{
pastack->s=(int *)malloc(sizeof(int)*m);
pastack->t=-1;
pastack->MAXNUM=m;
return(pastack);
}
else
printf("Overflow!\n");
return(NULL);