0% found this document useful (0 votes)
4 views10 pages

Stack

The document contains implementations of various stack operations and algorithms, including methods for pushing, popping, and checking the top element of a stack. It also includes functions for calculating baseball scores, finding the next greater element, removing duplicates from a string, validating parentheses, navigating a maze, calculating stock spans, and evaluating postfix expressions. Each function is designed to demonstrate the use of stacks and their applications in different scenarios.

Uploaded by

buihahaivas
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views10 pages

Stack

The document contains implementations of various stack operations and algorithms, including methods for pushing, popping, and checking the top element of a stack. It also includes functions for calculating baseball scores, finding the next greater element, removing duplicates from a string, validating parentheses, navigating a maze, calculating stock spans, and evaluating postfix expressions. Each function is designed to demonstrate the use of stacks and their applications in different scenarios.

Uploaded by

buihahaivas
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

void push(T item) {

// Push new element into the top of the stack

[Link](item);

T pop() {

// Remove an element on top of the stack

if ([Link]()) {

throw out_of_range("Stack is empty");

return [Link]([Link]() - 1);

T top() {

// Get value of the element on top of the stack

if ([Link]()) {

throw out_of_range("Stack is empty");

return [Link]([Link]() - 1);

}
bool empty() {

// Determine if the stack is empty

return [Link]();

int size() {

// Get the size of the stack

return [Link]();

void clear() {

// Clear all elements of the stack

[Link]();

int baseballScore(string ops) {

vector<int> record;

for (char op : ops) {

if (isdigit(op)) {

record.push_back(op - '0');

} else if (op == '+') {


int n = [Link]();

record.push_back(record[n - 1] + record[n - 2]);

} else if (op == 'D') {

record.push_back(2 * [Link]());

} else if (op == 'C') {

record.pop_back();

int sum = 0;

for (int score : record) {

sum += score;

return sum;

// iostream, stack and vector are included

vector<int> nextGreater(vector<int>& nums) {


int n = [Link]();

vector<int> result(n, -1);

stack<int> s;

for (int i = 0; i < n; ++i) {

while (![Link]() && nums[[Link]()] < nums[i]) {

result[[Link]()] = nums[i];

[Link]();

[Link](i);

return result;

#include<algorithm>

string removeDuplicates(string S) {

stack<char> st;

for (char c : S) {

if (![Link]() && [Link]() == c) {

[Link]();

} else {

[Link](c);

}
}

string result;

while (![Link]()) {

result += [Link]();

[Link]();

reverse([Link](), [Link]());

return result;

bool isValidParentheses(string s) {

stack<char> st;

for (char c : s) {

if (c == '(' || c == '[' || c == '{') {

[Link](c);

} else {

if ([Link]()) return false;

char top = [Link]();

[Link]();

if ((c == ')' && top != '(') ||


(c == ']' && top != '[') ||

(c == '}' && top != '{')) {

return false;

return [Link]();

bool isValidMove(int maze[5][5], int x, int y, bool visited[5][5]) {

return (x >= 0 && x < 5 && y >= 0 && y < 5 && maze[x][y] == 1 && !visited[x][y]);

bool canEatFood(int maze[5][5], int fx, int fy) {

if (maze[0][0] == 0 || maze[fx][fy] == 0) return false;

bool visited[5][5];

memset(visited, false, sizeof(visited));

stack<Node> st;

[Link](Node(0, 0));

visited[0][0] = true;
int row[] = {-1, 0, 1, 0}; // Up, Left, Down, Right

int col[] = {0, -1, 0, 1};

while (![Link]()) {

Node curr = [Link]();

[Link]();

int x = curr.x, y = curr.y, dir = [Link];

if (x == fx && y == fy) return true;

while (dir < 4) {

int newX = x + row[dir];

int newY = y + col[dir];

dir++;

if (isValidMove(maze, newX, newY, visited)) {

[Link](Node(x, y));

[Link]().dir = dir;

[Link](Node(newX, newY));

visited[newX][newY] = true;

break;

return false;

}
vector<int> stock_span(const vector<int> &prices){

vector<int> res([Link](), 0);

stack<int> st;

[Link](0);

res[0] = 1;

for(int i = 1; i < [Link](); i++){

while(![Link]() && prices[[Link]()] <= prices[i]){

[Link]();

res[i] = ([Link]()) ? (i + 1) : (i - [Link]());

[Link](i);

return res;

}
#include <sstream>

int evaluatePostfix(string expr) {

stack<int> st;

istringstream iss(expr);

string token;

while (iss >> token) {

if (isdigit(token[0]) || ([Link]() > 1 && isdigit(token[1]))) {

[Link](stoi(token));

} else {

int val2 = [Link]();

[Link]();

int val1 = [Link]();

[Link]();

switch (token[0]) {

case '+': [Link](val1 + val2); break;

case '-': [Link](val1 - val2); break;

case '*': [Link](val1 * val2); break;

case '/': [Link](val1 / val2); break;

return [Link]();
}

You might also like