栈和队列 相关 面试题

发布时间:2020-04-14 05:51:39 作者:alick97
阅读:662
开发者专用服务器限时活动,0元免费领! 查看>>

栈和队列 相关 面试题

// 1、实现一个栈,要求实现Push(出栈)、 Pop(入栈)、Min(返回最小值的操作) 的时间复杂度为O(1)

// 【剑指offer 面试题21】

template<class T>

class StackWithMin

{

public:

protected:

};

template<class T>

void StackWithMin<T>::push(const Tvalue)

{

    _data.push(value);

    {

        _min.push(value);

    }

    {

        _min.push(_min.top());

    }

}

template<class T>

 {

     assert(_data.size() > 0 && _min.size() > 0);

     _data.pop();

     _min.pop();

 }

const TStackWithMin<T>::min() const

 {

     assert(_data.size() > 0 && _min.size() > 0);

     return _min.top();

 }

 {

     StackWithMin<int> st;

     st.push(8);

     st.push(5);

     st.push(3);

     st.push(2);

     st.push(2);

     int min = st.min();

     st.pop();

     st.pop();

     st.pop();

     min = st.min();

 }

//   2、使用两个栈实现一个队列

 {

     void appendTail(const T& node);

     T deleteHead();

     stack<T> _stack1;

     stack<T> _stack2;

 };

 {

     _stack1.push(node);

 }

 {

     if (_stack2.size() <= 0)

     {

         while (_stack1.size() > 0)

         {

             T& data = _stack1.top();

             _stack2.push(data);

             _stack1.pop();

         }

     }

     if (_stack2.size() == 0)

     {

         throw exception("queue is empty");

     }

     T head = _stack2.top();

     _stack2.pop();

     return head;

 }

 {

     CQueue<int> q;

     q.appendTail(1);

     q.appendTail(2);

     q.appendTail(3);

     q.deleteHead();

     q.deleteHead();

     q.deleteHead();

     try

     {

         q.deleteHead();

     }

     catch (const exception& e)

     {

         cout<<e.what();

     }

 }

////3、使用两个队列实现一个栈

//思路:入栈动作时,如果内部两个队列都为空的话,将数据压入其中一个队列(代码中为m_queue1)。如果其中一个队列已经有数据了,则将数据压入已经有数据的那个队列。出栈动作时,先将有数据的那个队列,除了最后一个入队的数据之外的所有数据输出到另外一个空的队列,然后最后那个数据也出队。

#include <queue>

 {

     void push(const T& node);

     T pop();

     queue<T> _queue1;

     queue<T> _queue2;

 };

 {

     if ((_queue1.empty() && _queue2.empty()) || _queue1.size())

     {

         _queue1.push(node);

     }

     else

     {

         _queue2.push(node);

     }

 }

 {

     assert(!(_queue1.empty() && _queue2.empty()));

     T node;

     if (_queue1.size())

     {

         while (_queue1.size() > 1)

         {

             node = _queue1.front();

             _queue1.pop();

             _queue2.push(node);

         }

         node = _queue1.front();

         _queue1.pop();

     }

     else // _queue2 有数据 _queue1 空

     {

         while (_queue2.size() > 1)

         {

             node = _queue2.front();

             _queue2.pop();

             _queue1.push(node);

         }

         node = _queue2.front();

         _queue2.pop();

     }

     return node;

 }

 {

     CStack<char> testStack ;  

    testStack.push('a')  ;  

    testStack.push('b') ;  

    testStack.push('c') ;  

  

    printf("%c\n",ch) ;  

  

    ch = testStack.pop() ;  

    printf("%c\n",ch) ;  

  

    testStack.push('d') ;  

    ch = testStack.pop() ;  

    printf("%c\n",ch) ;  

  

 }

// 4、元素出栈、入栈顺序的合法性。如入栈的序列(1,2,3,4,5),出栈序列为(4,5,3,2,1)

 {

     bool bPossible = false;

     if (pPush != NULL && pPop != NULL && nLength > 0)

     {

         const int* pNextPush = pPush;

         const int* pNextPop = pPop;

         stack<int> stackData;

         while (pNextPop - pPop < nLength)

         {

             while (stackData.empty() || stackData.top() != *pNextPop)

             {

                 if (pNextPush - pPush == nLength)

                 {

                     break;

                 }

                 stackData.push(*pNextPush);

                 pNextPush++;

             }

             if (stackData.top() != *pNextPop)  //  if (pNextPush - pPush == nLength) 跳出的

             {

                 break//  不匹配

             }

             stackData.pop();

             pNextPop++;

         }

         if (stackData.empty() && pNextPop - pPop == nLength)

         {

             bPossible = true;

         }

     }

     return bPossible;

 }

 {

     int PushArry[] = {1,2,3,4,5};

     int PopArray1[] = {3,2,5,4,1};

     int PopArray2[] = {3,2,5,1,4};

     int PopArray3[] = {3,2,5,1,1};

     bool ret1 = IsPopOrder(PushArry, PopArray1,5);

     bool ret2 = IsPopOrder(PushArry, PopArray2,5);

     bool ret3 = IsPopOrder(PushArry, PopArray3,5);

 }

//    5、一个数组实现两个栈

 {

     arrayWithTwoStack(int size)

         :_top1(0)

         ,_top2(size - 1)

         ,_len(size)

     {

        _s = new int[size];

     }

     bool isEmpty(int index);// index指定哪个栈

     void push(int index, int data);

     int pop(int index);

     int _top1;

     int _top2;  // 两个栈顶坐标

     int _len ;  //  数组长度

     int* _s;    //

 };

 {

     if (index == 0 && _top1 == 0)

     {

         return true;

     }

     if (index == 1 && _top2 == _len - 1)

     {

         return true;

     }

     return false;

 }

 {

     // 已满

     if (_top1 >= _top2)

     {

         throw exception("error: overflow");

     }

     // 对栈1操作

     if (index == 0)

     {  

         _s[_top1] =data;

         _top1++;

     }

     else if (index == 1)

     {

         _s[_top2] = data;

                  _top2--;

     }

 }

 {

     int ret;

     if (index == 0)

     {

         //栈1空

         if (_top1 == 0)

         {

             throw exception("error: stack 0 is empty");

         }

         else

         {

             ret = _s[--_top1];

         }

     }

     else if (index == 1)

     {

         //栈 2 空

         if (_top2 == _len - 1)

         {

             throw exception("error: stack 1 is empty");

         }

         else

         {

             ret = _s[++_top2];

         }

     }

     return ret;

 }

 {

     arrayWithTwoStack S(6);

     // s0 123  54 s1 

     S.push(0,1);

     S.push(0,2);

     S.push(0,3);

     try{

     S.push(1,4);

     S.push(1,5);

     }

     catch(exception e)

     {

         cout<<e.what()<<endl;

     }

     S.pop(0);

     S.pop(1);

     S.pop(1);

     bool em = S.isEmpty(1);

 }

亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>

推荐阅读:
  1. C++中怎么用strcmp比较两个字符串
  2. 非程序员选择学习C++还是Python?

开发者交流群:

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

c++ c

上一篇:大数据学习系列之九---- Hive整合Spark和HBase以及相关测试

下一篇:Jquery通过Ajax方式来提交Form表单的具体实现

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》
开发者交流群×