| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1177 人关注过本帖
标题:栈的操作(有些小问题,帮忙看看)
只看楼主 加入收藏
crazywen2009
Rank: 1
等 级:新手上路
帖 子:16
专家分:5
注 册:2009-10-16
结帖率:100%
收藏
已结贴  问题点数:20 回复次数:10 
栈的操作(有些小问题,帮忙看看)
#include<malloc.h> /* malloc()等 */
#include<stdio.h> /* EOF(=^Z或F6),NULL */
#include<stdlib.h> /* atoi() */
#include<process.h> /* exit() */
struct StackRecord;
typedef struct StackRecord *Stack;
typedef   char ElementType;
#define EmptyTOS -1
#define MinStackSize 5

struct StackRecord
{
int Capacity;
int TopOfStack;
ElementType *Array;
};

void MakeEmpty(Stack S)
{
S->TopOfStack=EmptyTOS;
}
int IsFull(Stack S)
 { return S->TopOfStack== S->Capacity;
 }
int IsEmpty(Stack S)
 { return S->TopOfStack==EmptyTOS;
 }
   




Stack   CreateStack(int MaxElements )
 {
             Stack S;

/* 1 */        if(MaxElements<MinStackSize)
/* 2 */           {printf("Stack size is too small");
                 exit(0);
                 }
/* 3 */           S=(Stack)malloc(sizeof(struct StackRecord ) );
/* 4 */           if(S==NULL)
/* 5 */             { printf("Out of space!!l");
                        exit(0);
                 }
/* 6 */           S->Array=(ElementType *)malloc(sizeof(ElementType)*MaxElements);
/* 7 */           if(S->Array==NULL)
/* 8 */               { printf("Out of space!!l");
                           exit(0);
                 }
/* 9 */           S->Capacity=MaxElements;
/*10*/           MakeEmpty(S);
/*11*/           return S;
}

 /*void   DisposeStack(Stack S)
   {
      if(S!=NULL)
     {
         free(S->Array);
         free(S);
}
}
*/
void Push(ElementType X, Stack S)
{
     if( IsFull(S) )
{ printf("Full stack");
exit(0);  }
     else
             S->Array[++S->TopOfStack]=X;//将元素X放入栈中
}

ElementType Top(Stack S)
{
     if(!IsEmpty(S) )
       return   S->Array[S->TopOfStack]   ;//返回栈顶元素
    /* Return value used to avoid warning */
}

void Pop(Stack S)
{
if(IsEmpty(S) )
  { printf("Empty stack");
exit(0);
                }
else
    S->TopOfStack-- ;
}


ElementType TopAndPop(Stack S )
{
if(!IsEmpty(S) )
   return S->Array[S->TopOfStack--];
 
}

 void main(void)
{
     char c1,c2;
     Stack S;
     int i,MAX;
     scanf("%d",&MAX);getchar();
     S=CreateStack(MAX );//建立一个大小MAX的栈
for(i=1;i<=MAX;i++)
{
    scanf("%c\n",&c1);//通过键盘输入为变量c1赋值
    Push(c1, S);//将c1压入栈中
}
 //输出栈顶元素
/*printf("栈顶元素为c2=%c\n",Top(S));
Pop(S); //栈顶元素出栈
*/for( i=0; i<=MAX; i++)
printf("%c ",TopAndPop( S ));// 输出栈序列
}
搜索更多相关主题的帖子: return 
2009-10-30 09:38
crazywen2009
Rank: 1
等 级:新手上路
帖 子:16
专家分:5
注 册:2009-10-16
收藏
得分:0 
就是要输入a,b,c,d,e时,输出的时候就少了a!
2009-10-30 10:01
玩出来的代码
Rank: 11Rank: 11Rank: 11Rank: 11
来 自:河南新乡
等 级:贵宾
威 望:11
帖 子:742
专家分:2989
注 册:2009-10-12
收藏
得分:20 
问题有点严重让我看了好大会儿!

#include<malloc.h> /* malloc()等 */
#include<stdio.h> /* EOF(=^Z或F6),NULL */
#include<stdlib.h> /* atoi() */
#include<process.h> /* exit() */
struct StackRecord;
typedef struct StackRecord *Stack;
typedef   char ElementType;
#define EmptyTOS -1
#define MinStackSize
 
struct StackRecord
{
int Capacity;
int TopOfStack;
ElementType *Array;
};
 
void MakeEmpty(Stack S)
{
S->TopOfStack=EmptyTOS;
}
int IsFull(Stack S)
{ return S->TopOfStack== S->Capacity;
}
int IsEmpty(Stack S)
{ return S->TopOfStack==EmptyTOS;
}
   
 
 
 
 
Stack   CreateStack(int MaxElements )
{
             Stack S;
 
/* 1 */        if(MaxElements>MinStackSize)                   ///// //这里应该是>
/* 2 */           {printf("Stack size is too small");
                 exit(0);
                 }
/* 3 */           S=(Stack)malloc(sizeof(struct StackRecord ) );
/* 4 */           if(S==NULL)
/* 5 */             { printf("Out of space!!l");
                        exit(0);
                 }
/* 6 */           S->Array=(ElementType *)malloc(sizeof(ElementType)*MaxElements);
/* 7 */           if(S->Array==NULL)
/* 8 */               { printf("Out of space!!l");
                           exit(0);
                 }
/* 9 */           S->Capacity=MaxElements;
/*10*/           MakeEmpty(S);
/*11*/           return S;
}
 
/*void   DisposeStack(Stack S)
   {
      if(S!=NULL)
     {
         free(S->Array);
         free(S);
}
}
*/
void Push(ElementType X, Stack S)
{
     if( IsFull(S) )  
{ printf("Full stack");
exit(0);  }
     else
             S->Array[S->TopOfStack]=X;      //应该从0开始,因为你让头指针=-1了
 
            S->TopOfStack=S->TopOfStack+1;
}
ElementType Top(Stack S)
{
     if(!IsEmpty(S) )
       return   S->Array[--S->TopOfStack]   ;
    /* Return value used to avoid warning */  
}
 
void Pop(Stack S)
{
if(IsEmpty(S) )
  { printf("Empty stack");
exit(0);
                }
else
    S->TopOfStack-- ;
}
 
 
ElementType TopAndPop(Stack S )
{
if(!IsEmpty(S) )
   return S->Array[S->TopOfStack--];
 
}
 
void main(void)
{
     int c1,c2;
     Stack S;
     int i,MAX;
     scanf("%d",&MAX);
     S=CreateStack(MAX );
for(i=1;i<=MAX;i++)
{  
    scanf("%d",&c1);
    Push(c1, S);
}
 printf("%d", S->TopOfStack);        
 
printf("栈顶元素为c2=%d\n",Top(S));      ///////////这些数据明明是整型的你要让他试字符型输入输出
 
 
Pop(S);         
for( i=1; i<=MAX-1; i++)               //pop后删除了一个元素,让MAX-1,输出4个数就行了!
printf("%d ",TopAndPop( S ));
}
你这种写的方式很难调试的!,太麻烦,主调函数和被调函数连起来了不好,最好让被调函数可以独立运行!

离恨恰如春草,更行更远还生。
2009-10-30 11:54
crazywen2009
Rank: 1
等 级:新手上路
帖 子:16
专家分:5
注 册:2009-10-16
收藏
得分:0 
调试下好像还不行哦··
2009-11-03 22:39
玩出来的代码
Rank: 11Rank: 11Rank: 11Rank: 11
来 自:河南新乡
等 级:贵宾
威 望:11
帖 子:742
专家分:2989
注 册:2009-10-12
收藏
得分:0 
typedef struct StackRecord *Stack;
typedef   char ElementType;
#define EmptyTOS -1
#define MinStackSize  6         //当时调试时明明是调好的,奇怪!这里少个6

int IsEmpty(Stack S)
{ return S->TopOfStack+1==EmptyTOS;   //让S->TopOfStack+1

离恨恰如春草,更行更远还生。
2009-11-04 13:31
fenzhi4297
Rank: 2
等 级:论坛游民
帖 子:34
专家分:13
注 册:2009-8-31
收藏
得分:0 
我看看也是不行......要不把你 运行的结果 截图 截 出来看看啊
2009-11-08 20:34
crazywen2009
Rank: 1
等 级:新手上路
帖 子:16
专家分:5
注 册:2009-10-16
收藏
得分:0 
这个已经改好了!谢谢帮忙啦··
2009-11-09 18:06
crazywen2009
Rank: 1
等 级:新手上路
帖 子:16
专家分:5
注 册:2009-10-16
收藏
得分:0 
#include<malloc.h> /* malloc()等 */
#include<stdio.h> /* EOF(=^Z或F6),NULL */
#include<stdlib.h> /* atoi() */
#include<process.h> /* exit() */
struct StackRecord;
typedef struct StackRecord *Stack;
typedef   char ElementType;
#define EmptyTOS -1
#define MinStackSize 5
   
struct StackRecord
{
int Capacity;
int TopOfStack;
ElementType *Array;
};
void MakeEmpty(Stack S)
{
S->TopOfStack=EmptyTOS;
}
int IsFull(Stack S)
 { return S->TopOfStack== S->Capacity;
 }
int IsEmpty(Stack S)
 { return S->TopOfStack==EmptyTOS;
 }
Stack   CreateStack(int MaxElements )
 {
             Stack S;

/* 1 */        if(MaxElements<MinStackSize)
/* 2 */           {printf("Stack size is too small");
                 exit(0);
                 }
/* 3 */           S=(Stack)malloc(sizeof(struct StackRecord ) );
/* 4 */           if(S==NULL)
/* 5 */             { printf("Out of space!!l");
                        exit(0);
                 }
/* 6 */           S->Array=(ElementType *)malloc(sizeof(ElementType)*MaxElements);
/* 7 */           if(S->Array==NULL)
/* 8 */               { printf("Out of space!!l");
                           exit(0);
                 }
/* 9 */           S->Capacity=MaxElements;
/*10*/           MakeEmpty(S);
/*11*/           return S;
}
void   DisposeStack(Stack S)
{
      if(S!=NULL)
     {
         free(S->Array);
         free(S);
      }
}
void Push(ElementType X, Stack S)
{
     if( IsFull(S) )
{ printf("Full stack");
exit(0);  }
     else
  S ->Array[++S->TopOfStack]=X;//将元素X放入栈中
}
ElementType Top(Stack S)
{
     if(!IsEmpty(S) )
       return S->Array[S-> TopOfStack];//返回栈顶元素
    /* Return value used to avoid warning */
}
void Pop(Stack S)
{
if(IsEmpty(S) )
  { printf("Empty stack");
exit(0);
                }
else
  S->TopOfStack--;
}
ElementType TopAndPop(Stack S )
{
if(!IsEmpty(S) )
   return S->Array[S->TopOfStack--];
 
}
void main(void)
{   
    printf("先输入5,建立一个大小为5的栈;依次元素a,b,c,d,e进栈\n");
    char c1,c2;
    Stack S;
    int i,MAX;
    scanf("%d",&MAX);getchar( );
    S=CreateStack(MAX);//建立一个大小MAX的栈
    for(i=1;i<=MAX;i++)
    {
        scanf("%c",&c1);//通过键盘输入为变量c1赋值
        getchar();
        Push(c1,S);//将c1压入栈中
    }   
    printf("S->TopOfStack:%c\n ",Top(S));  //输出栈顶元素
    Pop(S);//栈顶元素出栈
    for( i=1;i<MAX;i++)
    printf("%c ",TopAndPop(S));// 输出栈序列
}
2009-11-09 18:07
benbenchina
Rank: 1
等 级:新手上路
帖 子:10
专家分:8
注 册:2009-11-14
收藏
得分:0 
你们编程是在哪里学的 ,我也想学哦   
2009-11-14 19:17
crazywen2009
Rank: 1
等 级:新手上路
帖 子:16
专家分:5
注 册:2009-10-16
收藏
得分:0 
上课哦··现在要忙数据结构!唉,C都还不是很行··
2009-12-17 13:30
快速回复:栈的操作(有些小问题,帮忙看看)
数据加载中...
 
   



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

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