顺序栈的基本操作及应用

该博客介绍了如何使用顺序栈实现顺序栈的基本操作,包括判断栈是否为空、获取栈顶元素、进栈和出栈等。此外,还展示了两个实际应用:一是利用顺序栈将十进制数转换为十六进制,二是通过栈判断字符串是否为回文。顺序栈在这些应用场景中表现出高效和简洁的特点。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

第1关:顺序栈的基本操作

任务描述

本关任务是实现顺序栈的基本操作函数,以实现判断栈是否为满、是否为空、求栈元素个数、进栈和出栈等功能。

测试说明

平台会对你编写的代码进行测试:

测试输入:
12
12 47 5 8 6 92 45 63 75 38 4 29

预期输出:
栈中元素依次为:12 47 5 8 6 92 45 63 75 38 4 29
弹出的栈顶元素 e=29
栈空否:0(1:空 0:否)
栈顶元素 e=4 栈的长度为11
清空栈后,栈空否:1(1:空 0:否)
销毁栈后,s.top=0 s.base=0 s.stacksize=0

输入说明
第一行输入顺序栈的数据元素的个数n;
第二行输入顺序栈的n个整数。

输出说明
第一行输出顺序栈的所有数据元素;
第二行输出出栈的数据元素;
第三行判断栈是否为空;
第四行输出当前的栈顶元素及栈的长度;
第五行清空栈后,判断栈是否为空;
第六行销毁栈后输出栈顶、栈底及栈的容量。

代码如下

#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  STACK_INCREMENT    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*sizeof(SElemType));
    if(!S.base) exit(OVERFLOW);
    S.top=S.base;
    S.stacksize=STACK_INIT_SIZE;
    /********** End **********/  
}

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

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

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


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


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

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


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


void Push(SqStack &S,SElemType e)
{     
	// 插入元素e为新的栈顶元素
    /********** Begin **********/ 
    if((S.top-S.base)>=S.stacksize){
        S.base=(SElemType *)realloc(S.base,(S.stacksize+STACK_INCREMENT)*sizeof(SElemType));
        if(!S.base)
            exit(OVERFLOW);
        S.top=S.base+S.stacksize;
        S.stacksize+=STACK_INCREMENT;
    }
    *(S.top)++=e;
    /********** End **********/	
}

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

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

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

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

任务描述

本关任务:利用顺序栈将一个非负的十进制整数N转换为对应的十六进制数。非负的十进制整数N从键盘输入,转换结果从屏幕输出。

测试说明

平台会对你编写的代码进行测试:

测试输入:10
预期输出:A

测试输入:100
预期输出:64

代码如下

#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)
         putchar('0'+n%16);
      else
         putchar('A'+n%16-10);
   }
   
	/********** End **********/	
}

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

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

任务描述

本关任务:若一个字符串的正序与倒序相同,则称其为回文字符串。现在输入一个字符串,使用栈判断它是否是回文字符串。

测试说明

平台会对你编写的代码进行测试:

测试输入:
thisistrueurtsisiht
预期输出:
thisistrueurtsisiht
YES

测试输入:
thisisnottrue
预期输出:
thisisnottrue
NO

输入格式:
在一行中给出一个不超过80个字符长度的、以回车结束的非空字符串。

输出格式:
在第1行中输出字符串。如果它是回文字符串,在第2行中输出YES,否则输出NO。

代码如下

#include<stdio.h>
#include<string.h>
// 添加以下两行
#include <iostream>
using namespace std;

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

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

# define MAXSIZE  80

int isHuiWei(char *s){
	int i,j,m1,m2;
	m1=strlen(s)/2;
	m2=strlen(s)%2==0?m1:m1+1;
	for(i=0,j=strlen(s)-1;i<m1,j>=m2;i++,j--){
		if(s[i]!=s[j]) return 0;
	}
	return 1;
}

int main()
{
	/********** Begin **********/ 
	char s[80];
	cin >> s;
	int i;
	for(i=0;i<strlen(s);i++)
		putchar(s[i]);
	if(isHuiWei(s)) printf("\n%s","YES");
	else printf("\n%s","NO");
	/********** End **********/	
	return 0;
}

辅助文件

sqstack.h

#ifndef   __SQSTACK_H__
#define   __SQSTACK_H__
// 函数结果状态代码
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define OVERFLOW -1

#define  STACK_INIT_SIZE   100  //存储空间初始分配量 
#define  STACKINCREMENT    10   //存储空间分配增量  

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

typedef   struct
{          
    SElemType     *base;    //栈的基址即栈底指针          
    SElemType     *top;     //栈顶指针          
   int      stacksize;       //当前分配的空间 
}SqStack; 
void InitStack(SqStack &S);
void DestroyStack(SqStack &S);
void ClearStack(SqStack &S);
int StackEmpty(SqStack S);
int StackLength(SqStack S);
int GetTop(SqStack S,SElemType &e);
void Push(SqStack &S,SElemType e);
int Pop(SqStack &S,SElemType &e);
void StackTraverse(SqStack S,void(*visit)(SElemType));
#endif

sqstack.cpp

#include<stdio.h>
#include<stdlib.h>
#include"sqstack.h" 
typedef char SElemType; // 定义栈元素类型为整型
void InitStack(SqStack &S)
{ 
	if(!(S.base=(SElemType *)malloc(STACK_INIT_SIZE*sizeof(SElemType))))
     exit(OVERFLOW); // 存储分配失败
    S.top=S.base;
	S.stacksize=STACK_INIT_SIZE; 
}

void DestroyStack(SqStack &S)
{ 
	free(S.base);
	S.base=NULL;
	S.top=NULL;
	S.stacksize=0; 
}

void ClearStack(SqStack &S)
{ 
	S.top=S.base;
}

int StackEmpty(SqStack S)
{
	if(S.top==S.base)
     return TRUE;
	else
     return FALSE; 	
 }

int StackLength(SqStack S)
{ 
	return S.top-S.base;
}

int GetTop(SqStack S,SElemType &e)
{ 
	if(S.top>S.base)
   {
     e=*(S.top-1);
     return OK;
   }
   else
     return ERROR;
 }

void Push(SqStack &S,SElemType e)
{ 
	if(S.top-S.base>=S.stacksize) // 栈满,追加存储空间
	{
     S.base=(SElemType *)realloc(S.base,(S.stacksize+STACKINCREMENT)*sizeof(SElemType));
     if(!S.base)
       exit(OVERFLOW); // 存储分配失败
     S.top=S.base+S.stacksize;
     S.stacksize+=STACKINCREMENT;
	}
   *(S.top)++=e; 
}

int Pop(SqStack &S,SElemType &e)
{ 
	if(S.top==S.base)
     return ERROR;
	e=*--S.top;
	return OK;
}

void StackTraverse(SqStack S,void(*visit)(SElemType))
{
	while(S.top>S.base)
		visit(*S.base++);
	printf("\n");
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值