[关闭]
@yudesong 2017-06-24T14:51:58.000000Z 字数 4465 阅读 612

yudesong 2017/6/24


栈是一种后进先出(Last In First Out)的线性表。

栈的基本操作只有两个:
1. 入栈(push):即将数据保存在栈顶。进行该操作前,先修改栈顶指针,使其向上移动一个元素位置,然后将数据保存在栈顶指针所指向的位置。
2. 出栈(pop):即将栈顶的数据弹出,然后修改栈顶指针,使其指向栈中的下一个元素。
在栈中,只有栈顶元素是可以访问的。

  1. #define MAX_SIZE 100
  2. typedef char ElemType;
  3. typedef struct
  4. {
  5. ElemType data[MAX_SIZE];
  6. int top;
  7. }Stack;
  8. //初始化栈
  9. void initStack(Stack *&S)
  10. {
  11. S=(Stack *)malloc(sizeof(Stack));
  12. S->top=-1;
  13. }
  14. //元素进栈
  15. int push(Stack *&S,ElemType e)
  16. {
  17. if(S->top==MAX_SIZE-1) return 0;
  18. S->top++;
  19. S->data[S->top]=e;
  20. return 1;
  21. }
  22. //出栈
  23. int pop(Stack *&S)
  24. {
  25. if(S->top==-1) return 0;
  26. ElemType e=S->data[S->top];
  27. S->top--;
  28. return e;
  29. }
  30. //输出栈表
  31. void disPlay(Stack *S)
  32. {
  33. int i;
  34. for(i=S->top;i>=0;i--)
  35. printf("%c ",S->data[i]);
  36. }
面试题1:用两个栈实现队列和用两个队列实现一个栈

题目:用两个栈实现一个队列。队列的生命如下,请实现它的两个函数appendTail和deleteHead,分别完成在队列尾部插入结点和在队列头部删除结点的功能。

参考代码

  1. #include<iostream>
  2. #include<stdlib.h>
  3. #include<stack>
  4. using namespace std;
  5. template <typename T>class CQueue
  6. {
  7. public:
  8. CQueue(void);
  9. ~CQueue(void);
  10. void appendtail(const T& node);
  11. T deleteHead();
  12. private:
  13. stack<T> stack1;
  14. stack<T> stack2;
  15. };
  16. //构造函数
  17. template <typename T> CQueue<T>::CQueue(void)
  18. {
  19. }
  20. //析构函数
  21. template <typename T> CQueue<T>::~CQueue(void)
  22. {
  23. }
  24. //插入元素
  25. template <typename T> void CQueue<T>::appendtail(const T& node)
  26. {
  27. stack1.push(node);
  28. }
  29. //删除元素并返回
  30. template <typename T> T CQueue<T>::deleteHead()
  31. {
  32. if(stack2.size()<=0)
  33. {
  34. while(stack1.size()>0)
  35. {
  36. stack2.push(stack1.top());
  37. stack1.pop();
  38. }
  39. }
  40. if(stack2.size()==0)
  41. throw new exception("队列为空");
  42. T head=stack2.top();
  43. stack2.pop();
  44. return head;
  45. }
  46. void main()
  47. {
  48. CQueue<int> queue;
  49. queue.appendtail(1);
  50. queue.appendtail(2);
  51. queue.appendtail(3);
  52. queue.appendtail(4);
  53. int len=4;
  54. while(len>0)
  55. {
  56. cout<<queue.deleteHead()<<endl;
  57. --len;
  58. }
  59. system("pause");
  60. }
2. 使用两个队列实现一个栈

参考代码

  1. #include<iostream>
  2. #include<stdlib.h>
  3. #include<stack>
  4. #include<queue>
  5. using namespace std;
  6. template <typename T>class CStack
  7. {
  8. public:
  9. CStack(void){};
  10. ~CStack(void){};
  11. void push(const T& node);
  12. T pop();
  13. private:
  14. queue<T> queue1;
  15. queue<T> queue2;
  16. };
  17. //插入元素
  18. template <typename T> void CStack<T>::push(const T& element)
  19. {
  20. if(queue1.size()>0)//如果queue1不为空则往queue1中插入元素
  21. queue1.push(element);
  22. else if(queue2.size()>0)//如果queue2不为空则往queue2中插入元素
  23. queue2.push(element);
  24. else//如果两个队列都为空,则往queue1中插入元素
  25. queue1.push(element);
  26. }
  27. //删除元素
  28. template <typename T> T CStack<T>::pop()
  29. {
  30. if(queue1.size()==0)//如果queue1为空
  31. {
  32. //保证queue2中有一个元素,将其余元素保存到queue1中
  33. while(queue2.size()>1)
  34. {
  35. queue1.push(queue2.front());
  36. queue2.pop();
  37. }
  38. T& data=queue2.front();
  39. queue2.pop();
  40. return data;
  41. }
  42. else//如果queue2为空
  43. {
  44. //保证queue2中有一个元素,将其余元素保存到queue1中
  45. while(queue1.size()>1)
  46. {
  47. queue2.push(queue1.front());
  48. queue1.pop();
  49. }
  50. T& data=queue1.front();
  51. queue1.pop();
  52. return data;
  53. }
  54. }
  55. void main()
  56. {
  57. CStack<int> stack;
  58. stack.push(1);
  59. stack.push(2);
  60. stack.push(3);
  61. stack.push(4);
  62. int len=4;
  63. while(len>0)
  64. {
  65. cout<<stack.pop()<<" ";
  66. --len;
  67. }
  68. system("pause");
  69. }
3. 利用一个栈倒序另外一个栈中的数

参考代码

  1. #include<iostream>
  2. #include<stdlib.h>
  3. #include<stack>
  4. using namespace std;
  5. template <typename T> void ReverseOrder(stack<T> &s1,stack<T> &s2)
  6. {
  7. s1.push(5);
  8. s1.push(4);
  9. s1.push(3);
  10. s1.push(2);
  11. s1.push(1);
  12. int sortNum=0;
  13. int oriStackSize=s1.size();
  14. while(sortNum<oriStackSize)
  15. {
  16. int temp=s1.top();
  17. s1.pop();
  18. while(s1.size()-sortNum>0)
  19. {
  20. s2.push(s1.top());
  21. s1.pop();
  22. }
  23. //首元素存入s1
  24. s1.push(temp);
  25. ++sortNum;
  26. while(!s2.empty())
  27. {
  28. s1.push(s2.top());
  29. s2.pop();
  30. }
  31. }
  32. cout<<"逆序栈输出:"<<endl;
  33. while(!s1.empty())
  34. {
  35. cout<<s1.top()<<endl;
  36. s1.pop();
  37. }
  38. }
  39. void main()
  40. {
  41. stack<int> s1;
  42. stack<int> s2;
  43. ReverseOrder(s1,s2);
  44. system("pause");
  45. }
4. 铁轨

某城市有一个火车站,铁轨铺设如图所示。有n节车厢从A方向驶入车站,按进站顺序编号1~n。现让这些火车按照某种特定的顺序进入B方向的铁轨并驶出车站。 为了重组车厢,可以借助中转站C。C是一个可以停放任意多节车厢的车站,但由于末端封顶,驶入C的车厢必须按照相反的顺序驶出C。对于每个车厢,一旦从A移入C,就不能再回到A了;一旦从C移入B,就不能回到C了。换句话说,在任意时刻,只有两种选择:A→C和C→B。请编程判断判断:按给定的出站顺序,火车能否出站。

参考代码

  1. #include <iostream>
  2. #include <stack>
  3. using namespace std;
  4. const int N=100;
  5. int main()
  6. {
  7. int n,i;
  8. stack<char > stack;
  9. cin>>n;
  10. int num[N];
  11. for(i=1;i<=n;i++)
  12. cin>>num[i];
  13. int A=1,B=1,ok=1;
  14. while(B<=n)
  15. {
  16. if(A==num[B])
  17. {
  18. A++;
  19. B++;
  20. }
  21. else if(!stack.empty() && stack.top()==num[B])
  22. {
  23. stack.pop();
  24. B++;
  25. }else if(A<=n)
  26. {
  27. stack.push(A);
  28. A++;
  29. }else
  30. {
  31. ok=0;
  32. break;
  33. }
  34. }
  35. if(ok==1) cout<<"Yes"<<endl;
  36. else cout<<"No"<<endl;
  37. return 0;
  38. }
5. 进制转换

参考代码

  1. void conversion(int N,int d)
  2. {
  3. stack<int > stack;
  4. while(N)
  5. {
  6. stack.push(N%d);
  7. N=N/d;
  8. }
  9. while(!stack.empty())
  10. {
  11. cout<<stack.top()<<" ";
  12. stack.pop();
  13. }
  14. }
6. 括号匹配 (栈)

参考代码

  1. #include <iostream>
  2. #include <string>
  3. #include <stack>
  4. using namespace std;
  5. bool judge_bracket(string str)
  6. {
  7. int i;
  8. char ch;
  9. stack<char > stack;
  10. for(i=0;i<str.length();i++)
  11. {
  12. switch(ch=str[i])
  13. {
  14. case '(': stack.push(ch);
  15. break;
  16. case ')': if(stack.top()=='(')
  17. stack.pop();
  18. else
  19. return false;
  20. break;
  21. case '[': stack.push(ch);
  22. break;
  23. case ']': if(stack.top()=='[')
  24. stack.pop();
  25. else
  26. return false;
  27. break;
  28. case '{': stack.push(ch);
  29. break;
  30. case '}': if(stack.top()=='{')
  31. stack.pop();
  32. else
  33. return false;
  34. break;
  35. default : break;
  36. }
  37. }
  38. return true;
  39. }
  40. int main()
  41. {
  42. string str;
  43. cin>>str;
  44. if(judge_bracket(str)) cout<<"匹配成功!"<<endl;
  45. else cout<<"匹配不成功!"<<endl;
  46. return 0;
  47. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注