| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 570 人关注过本帖
标题:2个堆栈共用一条内存的问题
只看楼主 加入收藏
C_snow
Rank: 1
等 级:新手上路
帖 子:34
专家分:3
注 册:2010-2-18
结帖率:100%
收藏
已结贴  问题点数:16 回复次数:2 
2个堆栈共用一条内存的问题
#include <stdio.h>
#include <stdlib.h>
#define MAXNUM 20
#define elemtype int

typedef struct
{
 elemtype Stack[MAXNUM];
 int  LeftTop;
 int RightTop;
}qstype;

void InitiateQStack(qstype *s)//初始化
{
 s->LeftTop=-1;
 s->RightTop=MAXNUM;     
}

int PushQStack(qstype *s,char WhichStack,elemtype x)//进栈
{
 if(s->LeftTop>=s->RightTop-1)
 {
  printf("堆栈已满\n");                             
  return 0;
 }
 if(WhichStack!='L'&&WhichStack!='R')
 {
  printf("参数错误\n");
  return 0;                                    
 }
 if(WhichStack=='L') s->Stack[++s->LeftTop]==x;
 else
 s->Stack[--(s->RightTop)]==x;
 return 1;
}

elemtype PopQStack(qstype *s,char WhichStack)//出栈
{
 if(WhichStack!='L'&&WhichStack!='R')
 {
  printf("参数错误\n");
  return 0;                                    
 }
 if(WhichStack=='L')
 {
  if(s->LeftTop<0)
  {
   printf("左栈已空\n");
   return 0;               
  }         
  return s->Stack[(s->LeftTop)--];
 }
 if(WhichStack=='R')
 {
  if(s->RightTop>MAXNUM)
  {
   printf("右栈已空\n");
   return 0;               
  }         
  return s->Stack[(s->RightTop)++];         
 }                  
}

int main()
{
 char WhichStack;
 qstype *ss=(qstype *)malloc(sizeof(qstype));
 elemtype x,y;
 InitiateQStack(ss);
 printf("请选择插入左表还是右表: 'L'or 'R':\n");
 scanf("%c",&WhichStack);
 while(scanf("%d",&x)!=EOF)
 {
  scanf("%d",&x);
  PushQStack(ss,WhichStack,x);   
 }                       
 printf("请选择出栈的表:'L' or 'R':\n");
 scanf("%c",&WhichStack);
 while(PopQStack(ss,WhichStack))
 {
  y=PopQStack(ss,WhichStack);
  printf("%d\n",y);                                
 }
 getchar();getchar();
 return 0;
}


结果运行错误  怎么回事~~~~~~~
搜索更多相关主题的帖子: 堆栈 内存 
2010-12-12 17:30
寒风中的细雨
Rank: 17Rank: 17Rank: 17Rank: 17Rank: 17
等 级:贵宾
威 望:66
帖 子:1710
专家分:8645
注 册:2009-9-15
收藏
得分:16 
程序代码:
//2个堆栈共用一条内存的问题
#include <stdio.h>
#include <stdlib.h>
#define MAXNUM 20
#define elemtype int 

typedef struct
{
    elemtype Stack[MAXNUM];
    int  LeftTop;
    int RightTop;
}qstype;

void InitiateQStack(qstype *s)//初始化
{
    s->LeftTop=-1;
    s->RightTop=MAXNUM;    
}

int IsEmpty(qstype s, char WhichStack)
{
    if( WhichStack == 'L' )
    {
        if( s.LeftTop < 0 )
            return 1;
        return 0;
    }
    else if ( WhichStack == 'R' )
    {
        if( s.RightTop >= MAXNUM )
            return 1;
        return 0;
    }
    else
    {
        exit(0);
    }
}
int PushQStack(qstype *s,char WhichStack,elemtype x)//进栈
{
    //if(s->LeftTop>=s->RightTop-1)
    if( s->LeftTop == s->RightTop-1 )
    {
          printf("堆栈已满\n");                            
          return 0;
    }
    if(WhichStack!='L' && WhichStack!='R')
    {
          printf("参数错误\n");
          return 0;                                   
    }
    if(WhichStack == 'L')
        //s->Stack[++s->LeftTop]==x;
        s->Stack[++s->LeftTop] = x;//栈顶不为空
    else
        //s->Stack[--(s->RightTop)]==x;
        s->Stack[--s->RightTop] = x;

    return 1;
}

elemtype PopQStack(qstype *s,char WhichStack)//出栈
{
    if(WhichStack!='L' && WhichStack!='R')
    {
          printf("参数错误\n");
          return 0;                                   
    }
    if(WhichStack=='L')
    {
          if(s->LeftTop<0)
          {
               printf("左栈已空\n");
               return 0;               
          }        
          return s->Stack[(s->LeftTop)--];
    }
    if(WhichStack=='R')
    {
          if(s->RightTop>MAXNUM)
          {
               printf("右栈已空\n");
               return 0;              
          }         
          return s->Stack[(s->RightTop)++];        
    }
}

int main()
{
    char WhichStack;
    qstype *ss = (qstype *)malloc(sizeof(qstype));
    elemtype x,y;

    InitiateQStack(ss);

    printf("请选择插入左表还是右表: 'L'or 'R':\n");
    scanf("%c", &WhichStack);
    while( scanf("%d",&x)!=EOF )
    {
          //scanf("%d",&x);
          PushQStack(ss,WhichStack,x);   
    }
    printf("请选择出栈的表:'L' or 'R':\n");
    scanf("%c",&WhichStack);
    while( !IsEmpty(*ss,WhichStack) )
    {
          y=PopQStack(ss,WhichStack);
          printf("%d\n",y);                               
    }
    getchar();
    getchar();

    return 0;
}

/*
结果运行错误  怎么回事~~~~~~~
*/
2010-12-18 12:24
胡燕
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2017-9-25
收藏
得分:0 
求算法
2017-09-26 00:24
快速回复:2个堆栈共用一条内存的问题
数据加载中...
 
   



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

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