标题:这两个程序都是顺序栈,有什么什么错误?
只看楼主
cwl168
Rank: 1
等 级:新手上路
帖 子:48
专家分:0
注 册:2012-12-14
结帖率:8.33%
 问题点数:0 回复次数:1 
这两个程序都是顺序栈,有什么什么错误?
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
#define MAX 100
typedef struct
{
    int data[20];
    int top;
}sqstack;
void inti(sqstack *s)
{
      s->top=0;
      printf("初始化队列成功!\n");
}
int emptystack(sqstack *s)
{
  if(s->top==0)
      return (1);
  else
      return (0);
}
int Fullstack(sqstack *s)
{
    if(s->top==20)
    {
        return (1);
   
    }
    else
    {
        return (0);
    }
}
void pushstack(sqstack *s,int x)
{
      if(Fullstack(s))
      {
          printf("顺序栈为满!\n");
      }
      else
      {
          s->data[s->top]=x;
          s->top++;
          printf("压栈成功!\n");

      }
}
void print(sqstack *s)
{
     
    int p;
    p=s->top-1;
    printf("%d\n",p);
    if(s->top==0)
    printf("stack is NULL!\n");
  else
      while(p>=0)
    {
        printf("%d ",s->data[p]);
        p=p--;
    }
     printf("\n");

     

}
int popstack(sqstack *s)
{
    int e;
    if(emptystack(s))
    {
        printf("顺序栈为空!\n");
    }
    else
    {
         s->top--;
         e=s->data[s->top];
         return e;
    }
}
int main()
{   
    sqstack *s;int x;
    s=(sqstack *)malloc(sizeof(sqstack));
    inti(s);
    pushstack(s,1);
    pushstack(s,2);
    pushstack(s,3);
    pushstack(s,4);
     print(s);
    x=popstack(s);
    printf("弹栈的元素为%d\n",x);

    return 0;
}




#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
#define MAX 100
typedef struct
{
    int *base;
    int *top;
}stack;
void inti(stack *s)
{
    s->base=(int *)malloc(MAX*sizeof(int));
    s->top=s->base;
    printf(" 初始化顺序栈成功!\n");
}
void push(stack *s,int e)
{
    if(s->top-s->base>=MAX)
    {
           printf("顺序栈已满!\n");
    }
    else
    {   
         *s->top++=e;
         printf("入栈成功!\n");
    }
}
void pop(stack *s)
{
   int e;
   e=*--s->top;
   printf("弹栈的元素为%d\n",e);
   
}
void print(stack * s)
{  
    int *p;
   while(s->top!=s->base)
   {
       s->top--;
       printf("%d ",*(s->top));
      
   }
}

int main()
{   
     stack s;
     inti(&s);
     push(&s,5);
     push(&s,4);
     push(&s,3);
     pop(&s);
     print(&s);
     return 0;
}
搜索更多相关主题的帖子: top void 什么什么 include return 
2012-12-16 21:22
yuccn
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:何方
等 级:版主
威 望:167
帖 子:6809
专家分:42393
注 册:2010-12-16
得分:0 
int popstack(sqstack *s)
不是每个分支都有返回值

我行我乐
我的博客:
http://blog.yuccn. net
2012-12-17 10:19



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-391530-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.947198 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved