首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

字符串的栈有关问题

2012-02-17 
字符串的栈问题本代码只能实现字符的出栈入栈把下代码改成能实现字符串的出栈入栈,(用C++)#include mallo

字符串的栈问题
本代码只能实现字符的出栈入栈
把下代码改成能实现字符串的出栈入栈,(用C++)

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

typedef struct astack *stack;
typedef struct astack
{
  int top,maxtop;
  char *word;
} Astack;

stack stackinit(int size)
{
  stack s=malloc(sizeof *s);
  s->word=malloc(size*sizeof(char));
  s->maxtop=size;
  s->top=-1;
  return s;
}

int stackempty(stack s)
{
 return s->top<0;
}

int stackfull(stack s)
{
 return s->top==s->maxtop;
}

void push(char x,stack s)
{
 if(stackfull(s)) printf("stack is full");
 else s->word[++s->top]=x;
}

char pop(stack s)
{
 if(stackempty(s)) printf ("stack is empty");
 else return s->word[s->top--];
}
void main()
{

 stack h;
 h=stackinit(100);
 push('k',h);
 push('b',h);
 push('c',h);
 pop(h);
 push('q',h);
 push('i',h);
 push('l',h);
 push('y',h);
 printf("printf stack list:");
 while(!stackempty(h))
{
 printf("\n%c\n",pop(h));
}






[解决办法]
因为你原来押栈的是一个值
现在是一个指针
而这个指针指向的空间你好像还没有分配
当然有错啊

热点排行