Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
parantheses matching using stack in C++ - test
Language:
Ada
Assembly
Bash
C#
C++ (gcc)
C++ (clang)
C++ (vc++)
C (gcc)
C (clang)
C (vc)
Client Side
Clojure
Common Lisp
D
Elixir
Erlang
F#
Fortran
Go
Haskell
Java
Javascript
Kotlin
Lua
MySql
Node.js
Ocaml
Octave
Objective-C
Oracle
Pascal
Perl
Php
PostgreSQL
Prolog
Python
Python 3
R
Rust
Ruby
Scala
Scheme
Sql Server
Swift
Tcl
Visual Basic
Layout:
Vertical
Horizontal
#include <iostream> #include <string> using namespace std; class Node { public: char data; Node *next; Node() { data = 0; next = 0; } Node(char value, Node *nxt = 0) { data = value; next = nxt; } }; class List { private: Node *head, *tail; public: List() { head = tail = 0; } int Count() { int s = 0; Node *temp = head; while (temp != 0) { temp = temp->next; s++; } return s; } void Add2Head(char value) { if (head == 0) { head = tail = new Node(value, 0); } else { Node *temp = new Node(value, 0); temp->next = head; head = temp; } } void DeleteHead(char &c) { if (head == 0) { return; } else if (head == tail) { c = (head->data); head = tail = 0; } else { c = (head->data); Node *temp = head->next; delete head; head = temp; } } void DeleteHead() { if (head == 0) { return; } else if (head == tail) { head = tail = 0; } else { Node *temp = head->next; delete head; head = temp; } } char GetHeadValue() { return (head->data); } bool IsEmpty() { return (head == 0); } void Print() { cout << "****" << endl; Node *temp=head; while (temp!=0) { cout << temp->data << endl; temp=temp->next; } cout << "****" << endl; } }; class Stack { private: List l; public: void Push(char c) { l.Add2Head(c); } bool Pop(char &c) { if (l.IsEmpty()) { return false; } else { l.DeleteHead(c); return true; } } bool IsEmpty() { return l.IsEmpty(); } bool Flush() { if (l.IsEmpty()) { return false; } else { while (!l.IsEmpty()) { l.DeleteHead(); } return true; } } char Top() { return (l.GetHeadValue()); } int Size() { return l.Count(); } void Print() { l.Print(); } }; bool is_balanced(string str) { Stack s; s.Flush(); char d; bool pop; for (int i=0;i<str.length();i++) { switch (str[i]) { case '(': case '[': case '{': s.Push(str[i]); break; case ')': pop=s.Pop(d); if (d!='(' && pop) { return false; } break; case ']': pop=s.Pop(d); if (d!='[' && pop) { return false; } break; case '}': pop=s.Pop(d); if (d!='{' && pop) { return false; } break; } } if (!s.IsEmpty()) { return false; } s.Print(); return true; } void main() { /* char str[] = "Hello World!"; Stack s; char d; cout << str << endl; for (int i = 0; i < sizeof(str); i++) { s.Push(str[i]); } */ //s.Pop(d); //s.Pop(d); //cout << d << endl; //s.Print(); cout << is_balanced("())") << endl; //cout << endl; //cout << s.IsEmpty() << endl; //cout << s.Top() << endl; //system("pause"); }
cl.exe
Show compiler warnings
[
+
] Compiler args
[
+
]
Show input
Compilation time: 1,78 sec, absolute running time: 0,21 sec, absolute service time: 2,01 sec
edit mode
|
history
|
discussion
Hello World! **** **** 1