基于链表栈的delimiterMatching(linux文件读写)
469
2023.03.09
发布于 未知归属地
#include <string.h>
#include <iostream>
#include <unistd.h>
#include <sys/types.h>
void delimiterMatching(std::string expr)
{
        linkedStack<char> st;
        for(auto c: expr)
        {
                if (c == '(') st.push(')');
                else if (c == '{') st.push('}');
                else if (c == '[') st.push(']');
                else if (c == ')' || c == ']' || c == '}')
                {
                        if(st.empty() || st.top() != c)
                        {
                                std::cout << " no matching" << std::endl;
                                return;
                        }
                        st.pop();
                }
                else continue;
        }
        if(!st.empty())
        {
                std::cout << " no matching" << std::endl;
                return;
        }
        std::cout << "matching" << std::endl;
}

int main(int argc, char *argv[])
{
        int fd;
        fd = open(argv[1], O_RDONLY);
        char buffer[128];
        int ret = read(fd, buffer, 128);
        buffer[ret-1] = '\0';
        std::string buf(std::begin(buffer), std::end(buffer));
        std::cout << buf << std::endl;
        delimiterMatching(buf);
        return 0;
}
#include<iostream>

template<class T>
struct chainNode
{
        T element;
        chainNode<T> *next;

        chainNode(){}
        chainNode(const T& element)
        {
                this->element = element;
        }
        chainNode(const T& element, chainNode<T>* next)
        {
                this->element = element;
                this->next = next;
        }
};

template<class T>
class stack
{
public:
        virtual ~stack() {}
        //virtual bool empty();
        virtual T& top() = 0;
        virtual void pop() = 0;
        virtual void push(const T& theElement) = 0;
};

template<class T>
class linkedStack : public stack<T>
{
public:
        linkedStack(int initialCapacity = 10)  { stackTop = nullptr;stackSize = 0; }
        ~linkedStack();
        bool empty() const { return stackSize == 0; }
        T& top();
        void pop();
        void push(const T& theElement);
        bool empty();
private:
        chainNode<T>* stackTop;
        int stackSize;
};

template<class T>
linkedStack<T>::~linkedStack()
{
        while(stackTop != nullptr)
        {
                chainNode<T>* nextNode = stackTop->next;
                delete stackTop;
                stackTop = nextNode;
        }
}

template<class T>
T& linkedStack<T>::top()
{
        if(stackSize == 0);
                std::cout << "stack is empty" << std::endl;
        return stackTop->element;
}

template<class T>
void linkedStack<T>::pop()
{
        if(stackSize == 0)
                std::cout << "stack is empty" << std::endl;
        chainNode<T>* nextNode = stackTop->next;
        std::cout << "pop: " << stackTop->element << std::endl;
        delete stackTop;
        stackTop = nextNode;
        stackSize--;
}

template<class T>
void linkedStack<T>::push(const T& theElement)
{
        stackTop = new chainNode<T>(theElement, stackTop);
        stackSize++;
        std::cout << "push: " << theElement << std::endl;
}

template<class T>
bool linkedStack<T>::empty()
{
        return stackSize == 0;
}
评论 (2)