栈和队列面试题总结

假溢出是什么?怎么解决?

顺序队列因多次入队列和出队列操作后出现的尚有存储空间但不能再进行入队列操作的溢出称作为假
溢出。
顺序队列最大存储空间已经存满而又要求进行入队列操作所引起的溢出称为真溢出。

这里写图片描述
解决方法
这里写图片描述
在使用循环队列时判断队空和队满

1 少用一个存储单元
如果少用一个存储空间,则尾队尾指针加1等于队头指针为队列满的判断条件:
(rear+1)%MaxSize==front
队列空的条件仍旧为rear == front
2 设置一个标记为
设置标记为flag,初始时置flag = 0;每当入队列操作成功,就置flag=1;每当出队列成功就置
flag=0.此时判断队列空
的条件为:rear == front && flag == 0;判断队列满的条件为rear == front&& flag == 1
3 设置一个计数器
设置计数器count,初始时置count=0,每当入队列操作成功,就是count加1,每当出队列成
功,就使count减1。此时队列空的判断条件为:count==0;队列满的判断条件为count>0 && rear ==front 或者count == MaxSize

栈实现括号匹配操作

        bool IsMatchBla(const char* str)
 {
     assert(str);
     int sz=strlen(str);
      stack<char> sk;
     for(int i=0;i<sz;i++)
     {
         if(IsLeft(*str))
         {
             sk.push(*str);
            str++;
             continue;
          }
         else
         {
            if(!sk.empty())
              {
                char temp=sk.top();

                 if(temp=='('&&*str==')'||temp=='['&&*str==']'\
                         ||temp=='{'&&*str=='}')
                  {
                     sk.pop();
                     str++;
                      continue;
                 }
                  else
                 {
                     cout<<"left and right not match"<<endl;
                     return false;
                 }
             }
               else
              {
                cout<<"right more than left"<<endl;
                return false;
             }
         }
          str++;
     }
     if(!sk.empty())
      {
          cout<<"left more than right"<<endl;
          return false;
     }
      else
     {
          cout<<"match success"<<endl;
          return true;
     }

  }

利用栈计算表达式

bool IsOperatorSign(char str)
{
    if (str == '+' || str == '-' || str == '*' || str == '/')
    {
        return true;
    }
    return false;
}
int CalcRPN(char** ptr, int sz)
{
    stack<int>s;
    for (int i = 0; i < sz; i++)
    {
        if (!IsOperatorSign(*ptr[i]))
        {

            s.push(atoi(ptr[i]));
            continue;
        }
        else
        {
            if (s.empty())
                return 0;
            else
            {
                int left = s.top();
                s.pop();
                int right = s.top();
                s.pop();

                switch (*ptr[i])
                {

                case '+':
                {
                            s.push(right + left);
                            break;
                }
                case '-':
                {
                            s.push(right - left);
                            break;
                }
                case '*':
                {
                            s.push(right * left);
                            break;
                }
                case '/':
                {
                            if (left != 0)
                                s.push(right / left);
                            break;
                }

                default:cout << "参数错误!" << endl;
                    break;
                }
            }
        }
    }
    return s.top();
}

int main()
{
    char* str[] = { "12", "3", "4", "+", "*","6","-","8","0", "/","+" };
    int sz = sizeof(str) / sizeof(str[0]);
    cout << CalcRPN(str,sz) << endl;
    system("pause");
    return 0;
}

实现一个栈,要求实现push(入栈),pop(出栈),min(返回最小值)的时间复杂度为O(1)

template <class T>
 class StackwithMin{

     public:
        void push(const T&value)
         {
             data_stack.push(value);

             if(min_stack.size()==0||value<min_stack.top())
                 min_stack.push(value);
            else
                 min_stack.push(data_stack.top());
        }
        void pop()
        {
            if(data_stack.size()<=0||min_stack.size()<=0)
                return;

             data_stack.pop();
              min_stack.pop();
         }
          const T& min()const  
  {

            if(data_stack.size()<=0||min_stack.size()<=0)
                return;

              return min_stack.top();

          }
      private:
          stack<T> min_stack;
         stack<T> data_stack;
  };

使用两个栈实现一个队列

 template <class T>
 class myqueue
 {
     public:
         void push(const T& value)
         {
             s1.push(value);

         }
         const T& pop()
         {
             if(s2.size()<=0)
            {
                while(s1.size()>0)
                {
                    T&data=s1.top();
                    s1.pop();
                    s2.push(data);
                 }
            }
             if(s2.size()==0)  
   throw new exception("queue is empty");

            T head=s2.top();
             s2.pop();
             return head;
         }
     private:
         stack<T> s1;
         stack<T> s2;
 };

使用两个队列实现一个栈

 template <class T>
 class mystack
 {
     public:
         void push(const T& value)
        {
             q1.push(value);

         }
         const T& pop()
        {
            if(q2.size()<=0)
            {
                while(q1.size()!=1)
                {
                    q2.push(q1.front());
                    q1.pop();
                }
                T head=q1.front();
                q1.pop();  
                   while(q2.size()>0)
               {
                    q1.push(q2.front());
                    q1.pop();
                }
           }
            return head;
         }
     private:
         queue<T> q1;
         queue<T> q2;
 };

猜你喜欢

转载自blog.csdn.net/qq_37954088/article/details/81436609
今日推荐