栈的学习笔记

坚持就是胜利,加油吧自己

前期数据准备:

#define maxsize 100
typedef int elemtype;
typedef struct stack_type
{	
	elemtype stack[maxsize];
	int top;
}stacktype;

使用函数:

//initial
void initial(stacktype *p)
{
	p->top = -1;//Why is top equal to -1?
}

//Insert elem;
bool push(stacktype *p,elemtype a)
{
	if (p->top >= maxsize - 1)
	{
		printf("This stack IS FULL! Please find another stack to finish your option!!!\n");
		return false;
	}
	else
	{
		p->top++;
		p->stack[p->top] = a;
		return true;
	}
}

//Let the top elem get out of the stack
elemtype pop(stacktype *p)
{
	if (p->top < 0)
	{
		printf("This stack IS EMPTY!Please find another stack to finish your option!!!\n");
		return(NULL);
	}
	else
	{
		p->top--;
		return(p->stack[p->top + 1]);
	}
}

使用该栈解决的问题:简单的数字进制转换


我的代码:

#include <cstdio>
#include <iostream>
using namespace std;
#define maxsize 100
typedef int elemtype;
typedef struct stack_type
{
	elemtype stack[maxsize];
	int top;
}stacktype;
//
void initial(stacktype *p)
{
	p->top = -1;//Why is top equal to -1?
}
//Insert elem;
bool push(stacktype *p,elemtype a)
{
	if (p->top >= maxsize - 1)
	{
		printf("This stack IS FULL! Please find another stack to finish your option!!!\n");
		return false;
	}
	else
	{
		p->top++;
		p->stack[p->top] = a;
		return true;
	}
}
//Let the top elem get out of the stack
elemtype pop(stacktype *p)
{
	if (p->top < 0)
	{
		printf("This stack IS EMPTY!Please find another stack to finish your option!!!\n");
		return(NULL);
	}
	else
	{
		p->top--;
		return(p->stack[p->top + 1]);
	}
}
int main(void)
{
	stacktype s,*p;
	p = &s;
	//Now here is a simple question ;
	elemtype num = 12;
	int choice; bool flag;
	scanf_s("%d",&choice);
	initial(p);
	do
	{
		flag = push(p, (num%choice));
	} while ((num/=choice)&&flag);
	printf("\nNow the ans is ");
	while (p->top >= 0)
	{
		printf("%d",pop(p));
	}
	cout << endl;
	return 0;
}

输出结果




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值