堆栈

自家写的栈,目前只有初始化,出栈,入栈操作,因为结构知道,栈顶元素可以自己看

一:顺序栈

#include <bits/stdc++.h>
#define ref1(i,s,e,c) for(int i=s;i<e;i=i+c)
#define ref2(i,s,e,c) for(int i=s;i<=e;i=i+c)
#define ref3(i,e,s,c) for(int i=e;i>=s;i=i-c)
using namespace std;
typedef long long ll;
const int MAXSIZE=1000010;
typedef int ElemType;
typedef struct {
	ElemType elem[MAXSIZE];
	int top;
}stack;
void initStack(stack *st){
	st->top=-1;
} 
void push(stack *st,ElemType x){
	if(st->top<MAXSIZE-1){
		st->top++;
		st->elem[st->top]=x;	
	}
	else{
		cout<<"Overflow!\n";
	}
}
ElemType pop(stack *st){
	if(st->top==-1){
		cout<<"underflow!\n";
		return -1;
	}
	else{
		ElemType x=st->elem[st->top];
		st->top--;
		return x;
	}
}
int main()
{
	
	return 0;
}

二、两个栈共用一向量

特点:栈底在两端,栈顶浮动,俩栈顶相遇时栈满

#include <bits/stdc++.h>
#define ref1(i,s,e,c) for(int i=s;i<e;i=i+c)
#define ref2(i,s,e,c) for(int i=s;i<=e;i=i+c)
#define ref3(i,e,s,c) for(int i=e;i>=s;i=i-c)
using namespace std;
typedef long long ll;
const int MAXSIZE=1000010;
typedef int ElemType;
typedef struct {
	ElemType elem[MAXSIZE];
	int top[2];
}DupStack;
int d[2]={-1,MAXSIZE};//左右栈判断栈空的条件 
int z[2]={1,-1};//左右栈进栈时栈顶指针的增量 
void initStack(DupStack *st,int i){
	st->top[i]=d[i];
} 
void push(DupStack *st,ElemType x,int i){
	if(st->top[1]!=st->top[0]+1){
		st->top[i]+=z[i];
		st->elem[st->top[i]]=x;	
	}
	else{
		cout<<"Overflow!\n";
	}
}
ElemType pop(DupStack *st,int i){
	if(st->top[i]==d[i]){
		cout<<"underflow!\n";
		return -1;
	}
	else{
		ElemType x=st->elem[st->top[i]];
		st->top[i]-=z[i];
		return x;
	}
}
int main()
{
	
	return 0;
}

三、链表存储栈

#include <bits/stdc++.h>
#define ref1(i,s,e,c) for(int i=s;i<e;i=i+c)
#define ref2(i,s,e,c) for(int i=s;i<=e;i=i+c)
#define ref3(i,e,s,c) for(int i=e;i>=s;i=i-c)
using namespace std;
typedef long long ll;
const int MAXSIZE=1000010;
typedef int ElemType;
typedef struct Lsnode{
	ElemType elem;
	Lsnode *next;
}Lsnode;
Lsnode *top;
void initStack(Lsnode *top){
	top->next=NULL;
} 
void push(Lsnode *top,ElemType x){
	Lsnode *p=(Lsnode*)malloc(sizeof(Lsnode));
	p->elem=x;
	p->next=top->next;
	top->next=p;
}
ElemType pop(Lsnode *top){
	if(top->next==NULL){
		cout<<"Underflow!\n";
		return -1;
	}
	else{
		Lsnode *p=top->next;
		top->next=p->next;
		ElemType x=p->elem;
		free(p);
		return x;
	}
}
int main()
{
	top=(Lsnode*)malloc(sizeof(Lsnode));
	initStack(top);
	return 0;
}

四、多链表栈

特点:将多个链表的指针放入一个一维数组之中

#include <bits/stdc++.h>
#define ref1(i,s,e,c) for(int i=s;i<e;i=i+c)
#define ref2(i,s,e,c) for(int i=s;i<=e;i=i+c)
#define ref3(i,e,s,c) for(int i=e;i>=s;i=i-c)
using namespace std;
typedef long long ll;
const int MAXSIZE=1000010;
typedef int ElemType;
typedef struct Lsnode{
	ElemType elem;
	Lsnode *next;
}Lsnode;
Lsnode *top[MAXSIZE];//指针数组 
void initStack(Lsnode *top[]){
	ref1(i,0,MAXSIZE,1)
		top[i]->next=NULL;
} 
void push(Lsnode *top[],ElemType x,int i){
	Lsnode *p=(Lsnode*)malloc(sizeof(Lsnode));
	p->elem=x;
	p->next=top[i]->next;
	top[i]->next=p;
}
ElemType pop(Lsnode *top[],int i){
	if(top[i]->next==NULL){
		cout<<"Underflow!\n";
		return -1;
	}
	else{
		Lsnode *p=top[i]->next;
		top[i]->next=p->next;
		ElemType x=p->elem;
		free(p);
		return x;
	}
}
int main()
{
	initStack(top);
	return 0;
}

栈的应用:

一、表达式的计算

我用C++自带的栈写

#include <bits/stdc++.h>
#define ref1(i,s,e,c) for(int i=s;i<e;i=i+c)
#define ref2(i,s,e,c) for(int i=s;i<=e;i=i+c)
#define ref3(i,e,s,c) for(int i=e;i>=s;i=i-c)
using namespace std;
typedef long long ll;
const int MAXSIZE=1000010;
map<char,int> Map;//将算符转化为数字 
stack<int> num;//存放数字的栈 
stack<char> ch;//存放算符的栈
/*
算符之间的优先关系 
---------------------------------------------------------------- 
x1 \ x2|	+  |	-  | 	*  | 	/  | 	(  | 	)  |	#  |  
----------------------------------------------------------------
	+  |	>  |	>  |	<  |	<  |	<  |	>  |	>  |
----------------------------------------------------------------
	-  |	>  |	>  |	<  |	<  |	<  |	>  |	>  |
----------------------------------------------------------------
	*  |	>  |	>  |	>  |	>  |	<  |	>  |	>  |
----------------------------------------------------------------
	/  |	>  |	>  |	>  |	>  |	<  |	>  |	>  |
----------------------------------------------------------------
	(  |	>  |	>  |	>  |	>  |	>  |	=  |	   |
----------------------------------------------------------------
	)  |	>  |	>  |	>  |	>  |	   |	>  |	>  |
----------------------------------------------------------------
	#  |	<  |	<  |	<  |	<  |	<  |	   |	=  |
----------------------------------------------------------------
*/
char ret[10][10]={
{'>','>','<','<','<','>','>'},{'>','>','<','<','<','>','>'},{'>','>','>','>','<','>','>'},
{'>','>','>','>','<','>','>'},{'<','<','<','<','<','=',' '},{'>','>','>','>',' ','>','>'},
{'<','<','<','<','<',' ','='}
};
void init(){
	Map.insert(pair<char,int>('+',0));
	Map.insert(pair<char,int>('-',1));
	Map.insert(pair<char,int>('*',2));
	Map.insert(pair<char,int>('/',3));
	Map.insert(pair<char,int>('(',4));
	Map.insert(pair<char,int>(')',5));
	Map.insert(pair<char,int>('#',6));
}
int operate(char x,int a,int b){
	if(x=='/'){
		return a/b;
	}
	else if(x=='*'){
		return a*b;
	}
	else if(x=='+'){
		return a+b;
	}
	else return a-b;
}
int evaluateExpression(){
	ch.push('#');
	char c,p=' ';
	c=getchar();
	while(c!='#'||ch.top()!='#'){
		if(c=='\n') c='#';//最后一个换行也会被读进来,变成'#'就行
		if(c<='9'&&c>='0'){
			int v;
			if(p<='9'&&p>='0'){
				v=num.top()*10+c-'0';
				num.pop();
			}
			else v=c-'0';
			num.push(v);
			p=c;
			c=getchar();
		}
		else{
			switch(ret[Map[ch.top()]][Map[c]]){
				case '<': ch.push(c); p=c; c=getchar(); break;
				case '=': ch.pop(); p=c; c=getchar() ; break;
				case '>': p=c;
						  char temp=ch.top(); ch.pop();
						  int b=num.top(); num.pop();
						  int a=num.top(); num.pop();
						  num.push(operate(temp,a,b));
						  break;
			}
		}
	}
	int x=num.top();
	num.pop();
	return x;
}
int main()
{
	init(); ;
	cout<<evaluateExpression();
	return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Freertos中的堆栈和启动文件中的堆栈是两个不同的概念。在Freertos中,堆栈是用来保存任务的上下文信息的内存区域,用于任务切换时保存和恢复任务的执行状态。而启动文件中的堆栈是用来为整个程序提供内存空间的,包括全局变量、静态变量等。 在Freertos中,堆的大小可以通过在FreeRTOSConfig.h文件中设置configTOTAL_HEAP_SIZE来指定。这个值决定了Freertos可以使用的堆的总大小。根据经验,堆的大小应该设置得足够大,以满足任务的内存需求。 而启动文件中的堆栈大小与Freertos中的堆大小没有直接关系。启动文件中的堆栈大小是用来为整个程序提供内存空间的,包括全局变量、静态变量等。在使用Freertos时,启动文件中的堆栈大小可以根据以下公式来设置:启动文件中的heap_size = mcu运行时的ram空间 - RW-Data - ZI-Data - Freertos中设置的堆大小。 总结起来,Freertos中的堆栈和启动文件中的堆栈是两个不同的概念,它们的大小设置是独立的。在使用Freertos时,需要根据任务的内存需求来设置Freertos中的堆大小,并根据公式来设置启动文件中的堆栈大小,以确保程序的正常运行。 #### 引用[.reference_title] - *1* *2* *3* [stm32以及freertos 堆栈解析](https://blog.csdn.net/sinat_36568888/article/details/124320985)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值