gb t50358 2017全文:求数据结构栈简单程序,送300积分
来源:百度文库 编辑:中科新闻网 时间:2024/05/05 22:07:18
可以参考我的算法
方法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);
}
void push_seq(PSeqStack pastack,int x)
{
if(pastack->t >=pastack->MAXNUM-1)
printf("Overflow! \n");
else
{
pastack->t=pastack->t+1;
pastack->s[pastack->t]=x;
}
}
void pop_seq(PSeqStack pastack)
{
if(pastack->t==-1)
printf("Underflow! \n");
else
pastack->t=pastack->t-1;
}
int top_seq(PSeqStack pastack)
{
if(pastack->t==-1)
printf("It is empty!\n");
else
return(pastack->s[pastack->t]);
}
struct SeqQueue
{
int MAXNUM;
int f,r;
int *q;
};
typedef struct SeqQueue * PSeqQueue;
PSeqQueue createEmptyQueue_seq(int m)
{
PSeqQueue paqu =(PSeqQueue)malloc(sizeof(struct SeqQueue));
if(paqu!=NULL)
{
paqu->q=(int *)malloc(sizeof(int)*m);
paqu->f=0;
paqu->r=0;
paqu->MAXNUM=m;
return(paqu);
}
else
printf("Overflow!\n");
return(NULL);
}
void enQueue_seq(PSeqQueue paqu,int temp)
{
if((paqu->r+1)%(paqu->MAXNUM) ==paqu->f)
printf("Full queue.\n");
else
{
paqu->q[paqu->r]=temp;
paqu->r=(paqu->r+1)%(paqu->MAXNUM);
}
}
void dispaly(PSeqStack pastack)
{
int i;
for(i=0;i<=pastack->t;i++)
{
cout<<pastack->s[i]<<" ";
}
cout<<endl;
}
void dispaly1(PSeqQueue paqu)
{
int r;
r=paqu->f;
while(r!=paqu->r)
{
cout<<paqu->q[r]<<" ";
r=(r+1)%(paqu->MAXNUM);
}
cout<<endl;
}
void main()
{
PSeqQueue paqu;
int temp,j;
PSeqStack temp1;
temp1=createEmptyStack_seq(8);
push_seq(temp1,10);
push_seq(temp1,20);
push_seq(temp1,30);
push_seq(temp1,40);
dispaly(temp1);
paqu=createEmptyQueue_seq(9);
while(temp1->t!=-1)
{
temp=top_seq(temp1);
pop_seq(temp1);
enQueue_seq(paqu,temp);
}
dispaly1(paqu);
}
哇,不是吧,这样子就有300分了?楼主是不是在骗人?