栈的基本实现(入栈,出栈,清空,判断是否为空)

#include #include typedef struct Node { int data; struct Node *pNext;}NODE, *PNODE;typedef struct Stack { PNODE pTop; PNODE pBottom;}STACK, *PSTACK;void init(PSTACK pS) { pS->pTop = (PNO
摘要由CSDN通过智能技术生成
#include <stdio.h>
#include <stdlib.h>

typedef struct Node {
	int data;
	struct Node *pNext;
}NODE, *PNODE;

typedef struct Stack {
	PNODE pTop;
	PNODE pBottom;
}STACK, *PSTACK;

void init(PSTACK pS) {
	pS->pTop = (PNODE)malloc(sizeof(NODE));
	if(NULL == pS->pTop) {
		printf("动态内存分配失败!\n");
		exit(-1);
	}
	else {
		pS->pBottom = pS->pTop;
		pS->pTop->pNext = NULL;
	}
}

void push(PSTACK pS, int val){
	PNODE pNew = (PNODE)malloc(sizeof(NODE));
	pNew->data = val;
	pNew->pNext = pS->pTop;
	pS->pTop = pNew;
}

bool empty(PSTACK pS) {
	if(pS->pTop == pS->pBottom)
		return true;
	else
		return false;
}

bool pop(PSTACK pS, int* pVal) {
	if(empty(pS)) {
		return false;
	}
	else {
		PNODE r = pS->pTop;
		*pVal = r->data;
		pS->pTop = r->pNext;
		free(r);
		r = NULL;
		return true;
	}
}

void traverse(PSTACK pS) {
	PNODE p = pS->
  • 1
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
好的,下面是C++实现: ``` #include <iostream> #include <cstring> using namespace std; struct Node { char data; Node *next; }; class Stack { private: Node *top; int length; public: Stack() { top = NULL; length = 0; } ~Stack() { clear(); } void clear() { while (top != NULL) { Node *temp = top; top = top->next; delete temp; } length = 0; } bool isEmpty() { return (top == NULL); } int getLength() { return length; } char getTop() { if (isEmpty()) { return '\0'; } return top->data; } bool push(char data) { Node *node = new Node; node->data = data; node->next = top; top = node; length++; return true; } bool pop() { if (isEmpty()) { return false; } Node *temp = top; top = top->next; delete temp; length--; return true; } }; int priority(char op) { if (op == '+' || op == '-') { return 1; } if (op == '*' || op == '/') { return 2; } return 0; } bool isOperator(char c) { return (c == '+' || c == '-' || c == '*' || c == '/'); } void infixToPostfix(char infix[], char postfix[]) { Stack stack; int len = strlen(infix); int j = 0; for (int i = 0; i < len; i++) { if (infix[i] == '\b') { if (j > 0) { j--; } continue; } if (isdigit(infix[i])) { postfix[j++] = infix[i]; } else if (isOperator(infix[i])) { while (!stack.isEmpty() && priority(stack.getTop()) >= priority(infix[i])) { postfix[j++] = stack.getTop(); stack.pop(); } stack.push(infix[i]); } else if (infix[i] == '(') { stack.push(infix[i]); } else if (infix[i] == ')') { while (!stack.isEmpty() && stack.getTop() != '(') { postfix[j++] = stack.getTop(); stack.pop(); } if (!stack.isEmpty()) { stack.pop(); } } } while (!stack.isEmpty()) { postfix[j++] = stack.getTop(); stack.pop(); } postfix[j] = '\0'; } int calculate(char op, int num1, int num2) { if (op == '+') { return num1 + num2; } if (op == '-') { return num1 - num2; } if (op == '*') { return num1 * num2; } if (op == '/') { return num1 / num2; } return 0; } int evaluatePostfix(char postfix[]) { Stack stack; int len = strlen(postfix); for (int i = 0; i < len; i++) { if (isdigit(postfix[i])) { stack.push(postfix[i] - '0'); } else if (isOperator(postfix[i])) { int num2 = stack.getTop(); stack.pop(); int num1 = stack.getTop(); stack.pop(); stack.push(calculate(postfix[i], num1, num2)); } } return stack.getTop(); } int main() { char infix[100]; char postfix[100]; cout << "请输入一个中序表达式(允许使用退格键):" << endl; cin.getline(infix, 100); infixToPostfix(infix, postfix); cout << "转换后的后序表达式为:" << postfix << endl; cout << "计算结果为:" << evaluatePostfix(postfix) << endl; return 0; } ``` 在这个程序中,我们使用了链表来实现具有的基本操作都被实现了,并且我们还实现了中序表达式到后序表达式的转换,以及后序表达式的计算。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值