| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1211 人关注过本帖
标题:求助:顺序栈的建立和插入元素出现问题
只看楼主 加入收藏
守望之殇
Rank: 1
来 自:福建福州
等 级:新手上路
帖 子:43
专家分:9
注 册:2010-12-2
结帖率:100%
收藏
已结贴  问题点数:20 回复次数:17 
求助:顺序栈的建立和插入元素出现问题
这是basic.h文件

#include"stdio.h"
#define StackSize 100

typedef int ElemType;
typedef struct
{
  ElemType *top;
  ElemType *base;
  int stacksize;
}SeqStack;

void InitStack(SeqStack *S)
{
  S->base=(ElemType *)malloc(sizeof(ElemType)*StackSize);
  if(S->base==NULL)
    exit(1);
  S->top=S->base;
  S->stacksize=StackSize;
}

int EmptyStack(SeqStack S)
{
  if(S.top==S.base)
    return 1;
  else
    return -1;
}

int FullStack(SeqStack S)
{
  if(S.top-S.base==StackSize)
    return 1;
  return -1;
}

int InsertElem(SeqStack *S,ElemType x)
{
  if(FullStack(*S)==1)
    return -1;
  *(S->top++)=x;
  return 1;
}

int GetTop(SeqStack S,ElemType *x)
{
  if(EmptyStack(S)==1)
    return -1;
  x=S.top-1;
  return 1;
}

int DeleteTop(SeqStack *S,ElemType *x)
{
  if(EmptyStack(*S)==1)
    return -1;
  x=--S->top;
  return 1;
}

void PrintStack(SeqStack S)
{
  int i;
  ElemType *p;
  if(EmptyStack(S)==1)
    printf("The Stack is Empty!\n");
  else
    {
      printf("The Stack is:");
      for(p=S.base,i=0;i<S.top-S.base;i++,p++)
        printf("%d,",*p);
    }


下面是insert.c文件
#include"stdio.h"
#include"basic.h"
main()
{
  int i=1;
  ElemType x;
  SeqStack *S;
  InitStack(S);
  printf("Input elem%d:",i++);
  scanf("%d",&x);
  while(x!=0)
    {
      InsertElem(S,x);
      printf("Input elem%d:",i++);
      scanf("%d",&x);
    }
  PrintStack(*S);
  printf("Input what to insert:");
  scanf("%d",&x);
  if(x!=0)
    InsertElem(S,x);
  PrintStack(*S);
  getch();
}
为什么编译的时候没有反应,原先是可以的,后来加了这一段就不行了
printf("Input what to insert:");
  scanf("%d",&x);
  if(x!=0)
    InsertElem(S,x);
  PrintStack(*S);
搜索更多相关主题的帖子: 元素 
2011-03-31 21:41
outsider_scu
Rank: 9Rank: 9Rank: 9
等 级:蜘蛛侠
威 望:3
帖 子:430
专家分:1333
注 册:2010-10-21
收藏
得分:0 
我眼拙,没看出你这栈是什么实现 的?
数组?可是我们看见数组操作。

编程的道路上何其孤独!
2011-03-31 22:07
守望之殇
Rank: 1
来 自:福建福州
等 级:新手上路
帖 子:43
专家分:9
注 册:2010-12-2
收藏
得分:0 
是用数组实现的

博观而约取
2011-03-31 22:11
outsider_scu
Rank: 9Rank: 9Rank: 9
等 级:蜘蛛侠
威 望:3
帖 子:430
专家分:1333
注 册:2010-10-21
收藏
得分:0 
typedef struct
{
  ElemType *top;
  ElemType *base;
  int stacksize;
}SeqStack;

数组在哪里?只是两个指针?
还是你这是什么高级方法?

编程的道路上何其孤独!
2011-03-31 22:13
守望之殇
Rank: 1
来 自:福建福州
等 级:新手上路
帖 子:43
专家分:9
注 册:2010-12-2
收藏
得分:0 
是有个调用函数
void InitStack(SeqStack *S)
{
  S->base=(ElemType *)malloc(sizeof(ElemType)*StackSize);
  if(S->base==NULL)
    exit(1);
  S->top=S->base;
  S->stacksize=StackSize;
}
然后在插入元素
int InsertElem(SeqStack *S,ElemType x)
{
  if(FullStack(*S)==1)
    return -1;
  *(S->top++)=x;
  return 1;
}

博观而约取
2011-03-31 22:15
outsider_scu
Rank: 9Rank: 9Rank: 9
等 级:蜘蛛侠
威 望:3
帖 子:430
专家分:1333
注 册:2010-10-21
收藏
得分:0 
哎,我无能啊,看了这么久也没弄明白。

编程的道路上何其孤独!
2011-03-31 22:35
守望之殇
Rank: 1
来 自:福建福州
等 级:新手上路
帖 子:43
专家分:9
注 册:2010-12-2
收藏
得分:0 
回复 5楼 守望之殇
还是谢谢了
我只知道我的PrintStack写错了,没有符合栈的规律

博观而约取
2011-03-31 22:38
寒风中的细雨
Rank: 17Rank: 17Rank: 17Rank: 17Rank: 17
等 级:贵宾
威 望:66
帖 子:1710
专家分:8645
注 册:2009-9-15
收藏
得分:20 
程序代码:
/*求助:顺序栈的建立和插入元素出现问题
这是basic.h文件*/

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

#define StackSize 100

typedef int ElemType;
typedef struct
{
  ElemType *top;
  ElemType *base;
  int stacksize;
}SeqStack;

SeqStack * InitStack(SeqStack *S)
{
    S = (SeqStack *) malloc (sizeof(SeqStack));
    if( NULL == S )
    {
        printf("\t malloc failed!\n");
        exit( -1 );
    }
    S->base = (ElemType *)malloc(sizeof(ElemType)*StackSize);
    if(S->base==NULL)
    {
        exit(1);
    }
    S->top = S->base;
    S->stacksize = StackSize;

    return S;
}

int EmptyStack(SeqStack S)
{
  if( S.top == S.base )
    return 1;
  else
    return -1;
}

int FullStack(SeqStack S)
{
  if(S.top-S.base==S.stacksize)
    return 1;
  return -1;
}

int InsertElem(SeqStack *S,ElemType x)
{
  if(FullStack(*S)==1)
    return -1;
  //*(S->top++)=x;
  *S->top++ = x;
  return 1;
}

int GetTop(SeqStack S,ElemType *x)
{
  if(EmptyStack(S)==1)
    return -1;
  *x=*S.top-1;

  return 1;
}

int DeleteTop(SeqStack *S,ElemType *x)
{
  if(EmptyStack(*S)==1)
    return -1;
  *x = *--S->top;
  return 1;
}

void PrintStack(SeqStack S)
{
  if(EmptyStack(S)==1)
    printf("The Stack is Empty!\n");
  else
  {
      printf("\tThe Stack is\n");
      while( S.top != S.base )
      {
          printf("%d ", *--S.top );
      }

      printf("\n");
  }
}
2011-04-01 09:57
寒风中的细雨
Rank: 17Rank: 17Rank: 17Rank: 17Rank: 17
等 级:贵宾
威 望:66
帖 子:1710
专家分:8645
注 册:2009-9-15
收藏
得分:0 
程序代码:
/*下面是insert.c文件*/
#include"basic.h"
#include <conio.h>

int main()
{
    int i=1;
    ElemType x;
    SeqStack *S = NULL;
    S = InitStack(S);
    printf("Input elem%d:",i++);
    scanf("%d",&x);
    while(x!=0)
    {
        InsertElem(S,x);
        printf("Input elem%d:",i++);
        scanf("%d",&x);
    }
    PrintStack(*S);
    printf("Input what to insert:");
    scanf("%d",&x);
    if(x!=0)
        InsertElem(S,x);
    PrintStack(*S);
    getch();

    return 0;
}
/*为什么编译的时候没有反应,原先是可以的,后来加了这一段就不行了
printf("Input what to insert:");
  scanf("%d",&x);
  if(x!=0)
    InsertElem(S,x);
  PrintStack(*S);
*/
2011-04-01 09:57
寒风中的细雨
Rank: 17Rank: 17Rank: 17Rank: 17Rank: 17
等 级:贵宾
威 望:66
帖 子:1710
专家分:8645
注 册:2009-9-15
收藏
得分:0 
继续努力 很不错
2011-04-01 09:58
快速回复:求助:顺序栈的建立和插入元素出现问题
数据加载中...
 
   



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

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