Run Code  | API  | Code Wall  | Misc  | Feedback  | Login  | Theme  | Privacy  | Patreon 

parantheses matching test 1

#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());
    }
    
    void Print() {
        l.Print();
    }
};

bool match(string str) {
    bool result=false;
    char d;
    Stack s;
    for (int i=0;i<str.size();i++) {
        if (str[i]=='(' || str[i]=='[' || str[i]=='{') {
            s.Push(str[i]);
        }
        else {
            if (s.Top()=='(' && str[i]==')') {
                s.Pop(d);
            }
            else if (s.Top()=='[' && str[i]==']') {
                s.Pop(d);
            }
            else if (s.Top()=='{' && str[i]=='}') {
                s.Pop(d);
            }
        }
    }
    if (!s.IsEmpty()) {
        result=false;
    }
    return result;
}

void main() {
    /*
    char str[] = "Hello World!";
    Stack s;
    cout << str << endl;
    for (int i = 0; i < sizeof(str); i++) {
        s.Push(str[i]);
    }

    cout << endl;
    cout << s.IsEmpty() << endl;
    cout << s.Top() << endl;
    */
    //system("pause");
    cout << match("()") << endl;
}
 run  | edit  | history  | help 0