0% found this document useful (0 votes)
21 views3 pages

225 BalancedParenthesisUsingLLBasedStackC++

The document describes a class called Stack that implements a stack data structure using a linked list. It includes methods to push, pop, peek, check if empty/full, and get the top element. It also shows how to use the Stack class to check if parentheses in an expression are balanced.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views3 pages

225 BalancedParenthesisUsingLLBasedStackC++

The document describes a class called Stack that implements a stack data structure using a linked list. It includes methods to push, pop, peek, check if empty/full, and get the top element. It also shows how to use the Stack class to check if parentheses in an expression are balanced.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

#include <iostream>

#include<cstring>
using namespace std;

class Node{
public:
char data;
Node* next;
};

class Stack{
private:
Node* top;
public:
Stack();
~Stack();
void push(char x);
char pop();
char peek(int index);
int isEmpty();
int isFull();
char stackTop();
};

Stack::Stack() {
top = nullptr;
}

Stack::~Stack() {
Node* p = top;
while (top){
top = top->next;
delete p;
p = top;
}
}

void Stack::push(char x) {
Node* t = new Node;
if (t == nullptr){
cout << "Stack Overflow!" << endl;
} else {
t->data = x;
t->next = top;
top = t;
}
}

char Stack::pop() {
Node* p;
char x = -1;
if (top == nullptr){
cout << "Stack Underflow!" << endl;
} else {
p = top;
x = p->data;
top = top->next;
delete p;
}
return x;
}

int Stack::isFull() {
Node* t = new Node;
int r = t ? 1 : 0;
delete t;
return r;
}

int Stack::isEmpty() {
return top ? 0 : 1;
}

char Stack::stackTop() {
if (top){
return top->data;
}
return -1;
}

char Stack::peek(int index) {


if (isEmpty()){
return -1;
} else {
Node* p = top;

for (int i=0; p != nullptr && i<index-1; i++){


p = p->next;
}

if (p != nullptr){
return p->data;
} else {
return -1;
}
}
}

int isBalanced(char* exp){


Stack stk;

for (int i=0; i<strlen(exp); i++){


if (exp[i] == '('){
stk.push(exp[i]);
} else if (exp[i] == ')'){
if (stk.isEmpty()){
return false;
} else {
stk.pop();
}
}
}
return stk.isEmpty() ? true : false;
}

int main() {
char E[] = "((a+b)*(c-d))";
cout << isBalanced(E) << endl;

char F[] = "((a+b)*(c-d)))";


cout << isBalanced(F) << endl;

char G[] = "(((a+b)*(c-d))";


cout << isBalanced(G) << endl;

return 0;

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy