动态栈的实现(CPP)

#include<iostream>
#define INCREMENTSIZE 10 
#define INITIALSIZE 20
using namespace std;


typedef struct{
	int *data;
	int top,stackSize;	
}SqStack;



bool initStack(SqStack & S){
	int *a=(int *)malloc(INITIALSIZE * sizeof(int));
	S.data=a;
	S.top=-1;
	S.stackSize=INITIALSIZE;
	return true;	
}
bool isFull(SqStack &S){
	if(S.top == S.stackSize-1) return true;
	else return false;	
}

bool increStack(SqStack &S){
	int *a=(int *)malloc(S.stackSize* sizeof(int));
	a=S.data;
	int *b=(int *)malloc((S.stackSize+INCREMENTSIZE)* sizeof(int));
	for(int i=0;i<=S.top;i++)
	  b[i]=a[i];
    S.data=b;
    S.stackSize+= INCREMENTSIZE;
    free(a); 

	return false;
	
}

bool push(SqStack &S,int x){
	if(isFull(S)) increStack(S);
	S.data[++ S.top]=x;
	return true;	
}

bool pop(SqStack &S,int &x){
	if(S.top==-1) return false;
	x=S.data[S.top --];	
	return true;	
}

bool getTop(SqStack S,int &x){
	if(S.top== -1) return false;
	x=S.data[S.top];
	return true;	
}

bool isEmpty(SqStack S){
	if(S.top == -1) return true;
	else return false;	
}
bool free(SqStack &S){
	S.top=-1;
	S.stackSize= 0;
	free(S.data);
	return true;	
}
int main(){
	int x,i;
	SqStack S;
	initStack(S);
	for(i=0;i<40;i++)
       push(S,i);
    while(! isEmpty(S)){
    	pop(S,x);
    	cout<<x<<"  ";    	
    }
    
    cout<<endl;
	free(S);
}

转载于:https://my.oschina.net/liangxiao/blog/122168

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 下面是一个简单的手打实现括号配对的cpp代码: ```cpp #include <iostream> #include <stack> #include <string> using namespace std; bool isMatchingPair(char character1, char character2) { if (character1 == '(' && character2 == ')') return true; else if (character1 == '{' && character2 == '}') return true; else if (character1 == '[' && character2 == ']') return true; else return false; } bool areParenthesesBalanced(string expr) { stack<char> s; for (int i = 0; i < expr.length(); i++) { if (expr[i] == '(' || expr[i] == '{' || expr[i] == '[') s.push(expr[i]); else if (expr[i] == ')' || expr[i] == '}' || expr[i] == ']') { if (s.empty() || !isMatchingPair(s.top(), expr[i])) return false; else s.pop(); } } return s.empty(); } int main() { string expr = "{()}[]"; if (areParenthesesBalanced(expr)) cout << "Balanced" << endl; else cout << "Not Balanced" << endl; return 0; } ``` 该程序首先定义了一个 `isMatchingPair` 函数,用于检查两个字符是否匹配。然后定义了一个 `areParenthesesBalanced` 函数,该函数使用来检查表达式中的括号是否匹配。最后在主函数中调用 `areParenthesesBalanced` 函数来检查表达式是否平衡,并输出结果。 ### 回答2: 下面是一个用C++编写的手打来配对括号的示例程序: ```cpp #include <iostream> #include <stack> #include <string> bool checkBrackets(const std::string& expression) { std::stack<char> bracketStack; for (char ch : expression) { if (ch == '(' || ch == '[' || ch == '{') { bracketStack.push(ch); } else if (ch == ')' || ch == ']' || ch == '}') { if (bracketStack.empty()) { return false; // 括号为空,说明前面没有相应的左括号 } // 检查当前字符是否和顶元素对应 char top = bracketStack.top(); if ((ch == ')' && top == '(') || (ch == ']' && top == '[') || (ch == '}' && top == '{')) { bracketStack.pop(); } else { return false; // 括号不匹配 } } } return bracketStack.empty(); // 如果最后括号为空,说明所有括号都配对成功 } int main() { std::string expression; std::cout << "请输入一个表达式:"; std::cin >> expression; if (checkBrackets(expression)) { std::cout << "括号配对正确!" << std::endl; } else { std::cout << "括号配对错误!" << std::endl; } return 0; } ``` 这个程序中,我们使用了一个字符类型的 `bracketStack` 来存储左括号,并在遇到右括号时进行匹配。如果一个右括号出现时,为空,或者该右括号与顶元素不匹配,则表达式中的括号配对错误。最后,我们检查是否为空,以确保所有左括号都能被成功配对。 ### 回答3: 手打括号配对的C++实现如下: ```cpp #include <iostream> #include <stack> #include <string> using namespace std; bool isParenthesesMatched(string str) { stack<char> parenthesesStack; for (int i = 0; i < str.length(); i++) { char currentChar = str[i]; // 如果字符是左括号,则将其压入中 if (currentChar == '(' || currentChar == '[' || currentChar == '{') { parenthesesStack.push(currentChar); } // 如果字符是右括号,则检查顶元素是否与之匹配 else if (currentChar == ')' || currentChar == ']' || currentChar == '}') { // 如果为空或顶元素与当前括号类型不匹配,则表明括号未配对 if (parenthesesStack.empty() || ((!parenthesesStack.empty() && parenthesesStack.top() != '(') && currentChar == ')') || ((!parenthesesStack.empty() && parenthesesStack.top() != '[') && currentChar == ']') || ((!parenthesesStack.empty() && parenthesesStack.top() != '{') && currentChar == '}')) { return false; } // 如果顶元素与当前括号类型匹配,则将其弹出 parenthesesStack.pop(); } } // 如果中还有剩余未匹配的左括号,则返回false if (!parenthesesStack.empty()) { return false; } return true; } int main() { string parenthesesExpression; cout << "请输入括号表达式:"; cin >> parenthesesExpression; if (isParenthesesMatched(parenthesesExpression)) { cout << "括号配对正确!" << endl; } else { cout << "括号配对错误!" << endl; } return 0; } ``` 以上程序实现了一个函数`isParenthesesMatched`,用于判断给定的括号表达式是否括号配对正确。通过使用来检查左右括号是否配对。程序首先遍历每个字符,如果字符是左括号,则将其压入中;如果是右括号,则检查顶元素是否与之匹配,若匹配,则将其弹出。最后,如果为空,表示所有的括号都配对成功,返回true;否则,表示还有未匹配的括号,返回false。在main函数中,用户可以输入一个括号表达式,然后调用`isParenthesesMatched`函数判断括号配对是否正确,并输出结果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值