栈和队列

总览

  • 栈和队列的基本概念
  • 栈和队列的顺序存储结构
  • 栈和队列的链式存储结构
  • 栈和队列的应用
  • 特殊矩阵的压缩存储

 

  1. 栈的基本概念
    1. 特点:先进后出,栈顶进栈顶出
  2. 队列的基本概念
    1. 特点:先进先出,队首进,队尾出
  3. 顺序栈
  4. #include <iostream>
    using namespace std;
    #define MaxSize 1000
    struct
    {
        int data[MaxSize];
        int top;
    };
    class SeqStack
    {
      public:
        SeqStack() { top = -1; }
        ~SeqStack() {}
        void Push(int x);
        int Pop();
        int GetTop()
        {
            if (top != -1)
                return data[top];
        }
        int Empty() { return top == -1 ? 1 : 0; }
    
      private:
        int data[MaxSize];
        int top;
    };
    void SeqStack::Push(int x)
    {
        if (top == MaxSize - 1)
            throw "上溢";
        data[++top] = x;
    }
    int SeqStack::Pop()
    {
        if (top == -1)
            throw "下溢";
        return data[top--];
    }
    

      

  5. 链栈
  6. #include <iostream>
    using namespace std;
    struct Node
    {
        Node *next;
        int data;
    };
    class LinkStack
    {
      public:
        LinkStack() { top = NULL; }
        ~LinkStack();
        void Push(int x);
        int Pop();
        int GetTop()
        {
            if (top != NULL)
                return top->data;
        }
        int Empty()
        {
            return top == NULL ? 1 : 0;
        }
    
      private:
        Node *top;
    };
    void LinkStack::Push(int x)
    {
        Node *s = new Node;
        s->data = x;
        s->next = top;
        top = s;
    }
    int LinkStack::Pop()
    {
        if(top == NULL) throw "下溢";
        int x = top->data;
        Node *p = top;
        top = top->next;
        delete p;
        return x;
    }                                                                                                                                     
    

      

  7. 应用
  8. int match(char exp[], int n)
    {
        char stack[MaxSize];
        int top = -1;
    
        int i;
        for (i = 0; i < n; i++)
        {
            if (exp[i] == '(')
                stack[++top] = '(';
            if (exp[i] == ')')
            {
                if (top == -1)
                    return 0;
                else
                    --top;
            }
        }
        if (top == -1)
            return 1;
        else
            return 0;
    }
    int op(int a, char Op, int b)
    {
        if (Op == '+')
            return a + b;
        if(Op == '-')
            return a - b;
        if(Op == '*')
            return a * b;
        if(Op == '/')
        {
            if(b == 0)
            {
                cout << "ERROR" << endl;
                return 0;
            }
        }else {
            return a / b;
        }
    }
    int com(char exp[])
    {
        int i,a,b,c;
        char Op;
        int stack[MaxSize];
        int top = -1;
        for(i = 0;exp[i] != '\0'; i ++)
        {
            if(exp[i] >= '0' && exp[i] <= '9')
                stack[++top] = exp[i] - '0';
            else
            {
                Op = exp[i];
                b = stack[top --];
                a = stack[top --];
                c = op(a,Op,b);
                stack[++top] = c;
            }
        }
        return stack[top];
    }
    

      

  9. 顺序队列
  10. #include <iostream>
    using namespace std;
    #define QueueSize 1000
    class CirQueue
    {
      public:
        CirQueue() { front = rear = QueueSize - 1; }
        ~CirQueue() {}
        void EnQueue(int x);
        int DeQueue();
        int GetQueue();
        int Empty() { return front == rear ? 1 : 0; }
    
      private:
        int data[QueueSize];
        int front, rear;
    };
    void CirQueue::EnQueue(int x)
    {
        if ((rear + 1) % QueueSize == front)
            throw "上溢";
        rear = (rear + 1) % QueueSize;
        data[rear] = x;
    }
    int CirQueue::DeQueue()
    {
        if(rear == front) throw "下溢";
        front = (front + 1) % QueueSize;
        return data[front];
    }
    

      

  11. 链队列
  12. #include<iostream>
    using namespace std;
    struct Node
    {
        Node *next;
        int data;
    };
    class LinkQueue
    {
        public:
        LinkQueue();
        ~LinkQueue();
        void EnQueue(int x);
        int DeQueue();
        int GetQueue();
        int Empty(){return front == rear ? 1 : 0;}
        private:
        Node *front,*rear;
    };
    LinkQueue::LinkQueue()
    {
        Node *s = new Node;
        s->next = NULL;
        front = rear = s;
    }
    void LinkQueue::EnQueue(int x)
    {
        Node *s = new Node;
        s->data = x;
        s->next = NULL;
        rear->next = s;
        rear = s;
    }
    int LinkQueue::DeQueue()
    {
        if(rear == front) throw "下溢";
        Node *p = front->next;
        int x = p->data;
        front->next = p->next;
        if(p->next == NULL) rear = front;
        delete p;
        return x;
    }
posted @ 2018-08-05 19:07  StormAX  阅读(118)  评论(0编辑  收藏  举报