C++栈实现

1、栈(Stack)是一种线性存储结构,它具有如下特点:

(1)栈中的数据元素遵守“先进后出"(First In Last Out)的原则,简称FILO结构。

(2)限定只能在栈顶进行插入和删除操作。

2、栈的相关概念:

(1)栈顶与栈底:允许元素插入与删除的一端称为栈顶,另一端称为栈底。

(2)压栈:栈的插入操作,叫做进栈,也称压栈、入栈。

(3)弹栈:栈的删除操作,也叫做出栈。

3、栈的常用操作为:

(1)弹栈,通常命名为pop

(2)压栈,通常命名为push

(3)求栈的大小

(4)判断栈是否为空

(5)获取栈顶元素的值

4、栈的常见分类:

(1)基于数组的栈——以数组为底层数据结构时,通常以数组头为栈底,数组头到数组尾为栈顶的生长方向

(2)基于单链表的栈——以链表为底层的数据结构时,以链表头为栈顶,便于节点的插入与删除,压栈产生的新节点将一直出现在链表的头部

5、实例分析

       使用标准库的栈时, 应包含相关头文件,在栈中应包含头文件: #include< stack > 。定义:stack< int > s;

s.empty();         //如果栈为空则返回true, 否则返回false;
s.size();          //返回栈中元素的个数
s.top();           //返回栈顶元素, 但不删除该元素
s.pop();           //弹出栈顶元素, 但不返回其值
s.push();          //将元素压入栈顶
(1)基于数组的栈

#include <stack>
#include <iostream>
using namespace std;
 
int main()
{
    stack<int> mystack;
    int sum = 0;
    for (int i = 0; i <= 10; i++){
        mystack.push(i);
    }
    cout << "size is " << mystack.size() << endl;
    while (!mystack.empty()){
        cout << " " << mystack.top();
        mystack.pop();
    }
    cout << endl;
    system("pause");
    return 0;
}
//size is 11
// 10 9 8 7 6 5 4 3 2 1 0

(2)基于单链表的栈

#include <iostream>
using namespace std;
template<class T>class Stack
{
private:
    struct Node
    {
        T data;
        Node *next;
    };
    Node *head;
    Node *p;
    int length;
 
public:
    Stack()
    {
        head = NULL;
        length = 0;
    }
    void push(T n)//入栈
    {
        Node *q = new Node;
        q->data = n;
        if (head == NULL)
        {
            q->next = head;
            head = q;
            p = q;
        }
        else
        {
            q->next = p;
            p = q;
        }
        length++;
    }
 
    T pop()//出栈并且将出栈的元素返回
    {
        if (length <= 0)
        {
            abort();
        }
        Node *q;
        int data;
        q = p;
        data = p->data;
        p = p->next;
        delete(q);
        length--;
        return data;
    }
    int size()//返回元素个数
    {
        return length;
    }
    T top()//返回栈顶元素
    {
        return p->data;
    }
    bool isEmpty()//判断栈是不是空的
    {
        if (length == 0)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    void clear()//清空栈中的所有元素
    {
        while (length > 0)
        {
            pop();
        }
    }
};
int main()
{
    Stack<char> s;
    s.push('a');
    s.push('b');
    s.push('c');
    while (!s.isEmpty())
    {
        cout << s.pop() << endl;
    }
    system("pause");
    return 0;
}

 

  • 0
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是C++实现简易计算器的代码: ```c++ #include <iostream> #include <stack> #include <string> using namespace std; int main() { stack<int> numStack; // 存储数字的 stack<char> opStack; // 存储运算符的 string expression; // 表达式字符串 cin >> expression; int len = expression.length(); for (int i = 0; i < len; i++) { char ch = expression[i]; if (ch >= '0' && ch <= '9') { // 如果是数字,入数字 numStack.push(ch - '0'); } else { // 如果是运算符 if (opStack.empty() || ch == '(') { // 如果运算符为空或者当前运算符是左括号,直接入 opStack.push(ch); } else if (ch == ')') { // 如果当前运算符是右括号,弹出运算符中的元素直到遇到左括号 while (opStack.top() != '(') { int num2 = numStack.top(); numStack.pop(); int num1 = numStack.top(); numStack.pop(); char op = opStack.top(); opStack.pop(); int result; if (op == '+') { result = num1 + num2; } else if (op == '-') { result = num1 - num2; } else if (op == '*') { result = num1 * num2; } else { result = num1 / num2; } numStack.push(result); } opStack.pop(); // 弹出左括号 } else { // 如果当前运算符是加减乘除 while (!opStack.empty() && opStack.top() != '(' && ((ch == '*' || ch == '/') || (opStack.top() == '+' || opStack.top() == '-'))) { int num2 = numStack.top(); numStack.pop(); int num1 = numStack.top(); numStack.pop(); char op = opStack.top(); opStack.pop(); int result; if (op == '+') { result = num1 + num2; } else if (op == '-') { result = num1 - num2; } else if (op == '*') { result = num1 * num2; } else { result = num1 / num2; } numStack.push(result); } opStack.push(ch); } } } while (!opStack.empty()) { // 处理剩余的运算符 int num2 = numStack.top(); numStack.pop(); int num1 = numStack.top(); numStack.pop(); char op = opStack.top(); opStack.pop(); int result; if (op == '+') { result = num1 + num2; } else if (op == '-') { result = num1 - num2; } else if (op == '*') { result = num1 * num2; } else { result = num1 / num2; } numStack.push(result); } cout << numStack.top() << endl; // 输出最终结果 return 0; } ``` 这个程序可以处理包含加减乘除和括号的表达式,但是没有考虑负数的情况。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值