顺序栈的基本操作及应用

第1关 顺序栈的基本操作

#include <stdio.h>
#include<stdlib.h>
#include <iostream>
using namespace std;

 // 函数结果状态代码
 #define TRUE 1
 #define FALSE 0
 #define OK 1
 #define ERROR 0
 #define OVERFLOW -1
 
#define  STACK_INIT_SIZE   100  //存储空间初始分配量 
#define  STACKINCREMENT    10   //存储空间分配增量  
typedef int SElemType; // 定义栈元素类型为整型
/* 顺序栈类型定义 */
typedef   struct
{          
    SElemType     *base;    //栈的基址即栈底指针          
    SElemType     *top;     //栈顶指针          
   int      stacksize;       //当前分配的空间 
}SqStack; 

void input(SElemType &s);
void output(SElemType s);

void InitStack(SqStack &S);// 构造一个空栈S
void DestroyStack(SqStack &S); // 销毁栈S,S不再存在
void ClearStack(SqStack &S); // 把S置为空栈
int StackEmpty(SqStack S); // 若栈S为空栈,则返回TRUE,否则返回FALSE
int StackLength(SqStack S); // 返回S的元素个数,即栈的长度
int GetTop(SqStack S,SElemType &e);  // 若栈不空,则用e返回S的栈顶元素,并返回OK;否则返回ERROR
void Push(SqStack &S,SElemType e);    // 插入元素e为新的栈顶元素
int Pop(SqStack &S,SElemType &e);   // 若栈不空,则删除S的栈顶元素,用e返回其值,并返回OK;否则返回ERROR
void StackTraverse(SqStack S,void(*visit)(SElemType)); // 从栈底到栈顶依次对栈中每个元素调用函数visit()


int main()
 {
   int j;
   SqStack s;
   SElemType e;
   InitStack(s);
   int i;
   cin>>i;
   for(j=0;j<i;j++)  
   {  
       input(e);  
       Push(s,e);  
   }  
   printf("栈中元素依次为:");
   StackTraverse(s,output);
   Pop(s,e);
   printf("弹出的栈顶元素 e=%d\n",e);
   printf("栈空否:%d(1:空 0:否)\n",StackEmpty(s));
   GetTop(s,e);
   printf("栈顶元素 e=%d 栈的长度为%d\n",e,StackLength(s));
   ClearStack(s);
   printf("清空栈后,栈空否:%d(1:空 0:否)\n",StackEmpty(s));
   DestroyStack(s);
   printf("销毁栈后,s.top=%u s.base=%u s.stacksize=%d\n",s.top,s.base, s.stacksize);
 }
/*****SElemType类型元素的基本操作*****/
void input(SElemType &s)
{
    cin>>s;
}
void output(SElemType s)
{
    cout<<s<<" ";
}

/*****顺序栈的基本操作*****/
void InitStack(SqStack &S)
{ 
	// 构造一个空栈S
    /********** Begin **********/ 
    S.base=(SElemType*)malloc(STACK_INIT_SIZE);
    S.stacksize=STACK_INIT_SIZE;
    S.top=S.base;



    /********** End **********/  
}

void DestroyStack(SqStack &S)
{ 
	// 销毁栈S,S不再存在
    /********** Begin **********/ 
    // free(S.base);
    // free(S.top);
    S.base=S.top=NULL;
    S.stacksize=0;

    /********** End **********/ 	
}

void ClearStack(SqStack &S)
{ 
 	// 把S置为空栈
    /********** Begin **********/ 
    S.base=S.top=NULL;
    S.stacksize=0;


    /********** End **********/ 
}


int StackEmpty(SqStack S)
{
 	// 若栈S为空栈,则返回TRUE,否则返回FALSE
    /********** Begin **********/ 
    if(S.top==S.base)
    return 1;
    else
    return 0;
    /********** End **********/	
 }


int StackLength(SqStack S)
{ 
	// 返回S的元素个数,即栈的长度
    /********** Begin **********/ 
    int m;
    m=S.top-S.base;
    return m;

    /********** End **********/  
}


int GetTop(SqStack S,SElemType &e)
{ 
    // 若栈不空,则用e返回S的栈顶元素,并返回OK;否则返回ERROR
    /********** Begin **********/ 

    if(S.top!=S.base)
    {
        e=*(S.top-1);
        return 1;
    }
    else
    return 0;
    /********** End **********/	
}


void Push(SqStack &S,SElemType e)
{     
	// 插入元素e为新的栈顶元素
    /********** Begin **********/ 
    *(S.top)++=e;

    /********** End **********/	
}

int Pop(SqStack &S,SElemType &e)
{   
	// 若栈不空,则删除S的栈顶元素,用e返回其值,并返回OK;否则返回ERROR
    /********** Begin **********/ 
    if(S.base!=S.top)
    {
        e=*--S.top;
        return 1;
    }
    else
    return 0;
    

    /********** End **********/ 	
}

void StackTraverse(SqStack S,void(*visit)(SElemType))
{ 
	// 从栈底到栈顶依次对栈中每个元素调用函数visit()
    /********** Begin **********/ 
    while(S.top!=S.base)
    {
        visit(*S.base++);
    }
    printf("\n");

    /********** End **********/	
}

第2关 栈的应用-进制转换

#include <stdio.h>
#include<stdlib.h>
#include <iostream>
using namespace std;

typedef int SElemType; // 定义栈元素类型为整型

#include "sqstack.h"  // 顺序栈的类型定义

void conversion(unsigned n)
{ 
	// 对于输入的任意一个非负10进制整数,打印输出与其等值的16进制数
   /********** Begin **********/ 
   if(n>0)
   {
      conversion(n/16);
      if(n%16<10)
      printf("%c",'0'+n%16);
      else
      printf("%c",'A'+n%16-10);
   }
	
	/********** End **********/	
}

int main()
{  
    unsigned n; // 非负整数
    scanf("%u",&n); // 输入非负十进制整数n
    conversion(n);
 }

第3关 栈的应用-回文的判断

#include<stdio.h>
#include<string.h>

#include"sqstack.h"    //头文件sqstack.h为顺序栈的定义和基本操作的实现

typedef char SElemType;// 定义栈元素类型为字符型

# define MAXSIZE  80

int main()
{
	/********** Begin **********/ 
	char a[80];
    scanf("%s",a);
    int i=0,length;
    length=strlen(a);
    printf("%s\n",a);

    int m=0,n=0,j=0,t;
    m=length/2;
    if(length%2)
    n=m+1;
    else 
    n=m;
    for(i=0,j=length-1;i<m,j>=n;i++,j--)
    {
        if(a[i]==a[j])
        t=0;
        else
        t=1;
    }
    if(t==0)
    printf("YES");
    else
    printf("NO");

	/********** End **********/	
	return 0;
}

  • 5
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ଲଇଉକ ଲ ̊ଳ

多谢大哥赏赐

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值