《数据结构教程》(第5版)学习笔记(一)

这是实现顺序栈的各种基本运算的算法:

#include <iostream>
#include<cstdio>
#include<cstdlib>
using namespace std;

typedef int ElemType;
const int MaxSize=100;

//声明顺序栈的类型 
typedef struct{
	ElemType data[MaxSize];
	int top;
}SqStack;

 //栈的初始化 
void InitStack(SqStack *&s){
	s=(SqStack*)malloc(sizeof(SqStack));
	s->top=-1;
} 

//栈的销毁 
void Destory(SqStack *&s){
	free(s);
}

//判断栈是否为空 
bool StackEmpty(SqStack *s){
	return s->top==-1;
}

//元素进栈 
bool Push(SqStack *&s,ElemType e){
	if(s->top==MaxSize-1)return false;
	s->data[++s->top]=e;
	return true;
}

//元素出栈 
bool Pop(SqStack *&s,ElemType &e){
	if(s->top==-1)return false;
	e=s->data[s->top--];
	return true;
}

//获取栈顶元素 
bool GetTop(SqStack *&s,ElemType &e){
	if(s->top==-1)return false;
	e=s->data[s->top];
}

int main() {
 /*一些简单的操作:
        string str;
	cin>>str;
	int len=str.length();
	SqStack* s;
	InitStack(s);
	for(int i=0;i<len;i++)	Push(s,str[i]);
	
	ElemType e;
	for(int i=0;i<len/2;i++){
		Pop(s,e);
		if(str[i]!=e){
			cout<<"no";
		return 0;
		}
	}
	cout<<"yes";
*/
	ElemType a[]={1,3,5,7,9,0,8,6,4,2};
	SqStack* s;
	InitStack(s);
	for(int i=0;i<10;i++)Push(s,a[i]);
	for(int i=0;i<10;i++)Pop(s,a[i]);	
	for(int i=0;i<10;i++)cout<<a[i];
    Destory(s);
	cout<<endl<<StackEmpty(s)<<endl;
	return 0;
}

有不对的地方,请多多指教,谢谢。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值