Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
parantheses matching using stack in C++ - test
#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"); }
run
|
edit
|
history
|
help
0
problem_soultion2
not a parameter pack
Visual C++ template instantiation
Cannot allocate an array of constant size 0
SFINAE with std::enable_if
additional layer of indirection
return reference (msvc)
Hello World
Full System Specifications version 3
лаб1