• 2023年9月13日


    1> 将之前定义的栈类和队列类都实现成模板类

    1. #include
    2. using namespace std;
    3. template <class T>
    4. class Stack
    5. {
    6. private:
    7. T data[8];
    8. int top;
    9. public:
    10. //构造函数
    11. Stack()
    12. {
    13. top=-1;
    14. }
    15. //构析函数
    16. ~Stack()
    17. {
    18. }
    19. //判空
    20. bool stack_empty(Stack *p)
    21. {
    22. if(p->top==-1)
    23. {
    24. return 1;
    25. }
    26. return 0;
    27. }
    28. //判满
    29. bool stack_full(Stack *p)
    30. {
    31. if(p->top==7)
    32. {
    33. return 1;
    34. }
    35. return 0;
    36. }
    37. //入栈
    38. void stack_push(Stack *p,T e)
    39. {
    40. if(NULL==p||stack_full(p))
    41. {
    42. return;
    43. }
    44. p->top++;
    45. p->data[p->top]=e;
    46. cout<
    47. cout<<"入栈成功"<
    48. return ;
    49. }
    50. //出栈
    51. void stack_pop(Stack *p)
    52. {
    53. if(NULL==p||stack_empty(p))
    54. {
    55. return ;
    56. }
    57. T e=p->data[p->top];
    58. cout<"出栈成功"<
    59. p->top--;
    60. return;
    61. }
    62. //销毁栈
    63. void stack_free(Stack *p)
    64. {
    65. if(NULL==p)
    66. {
    67. return ;
    68. }
    69. delete p;
    70. p=nullptr;
    71. cout<<"销毁成功"<
    72. return ;
    73. }
    74. //获取栈顶元素
    75. void stack_gettop(Stack *p)
    76. {
    77. if(NULL==p)
    78. {
    79. return ;
    80. }
    81. T e = p->data[p->top];
    82. cout<<"栈顶元素为"<
    83. return ;
    84. }
    85. //求栈的大小
    86. void stack_size(Stack *p)
    87. {
    88. if(NULL==p)
    89. {
    90. return ;
    91. }
    92. cout<<"栈的大小为"<top+1<
    93. }
    94. void stack_show(Stack *p)
    95. {
    96. for(int i=0;i<=p->top;i++)
    97. {
    98. cout<<" "<data[i];
    99. }
    100. cout<
    101. return ;
    102. }
    103. };
    104. int main()
    105. {
    106. //初始化
    107. Stack<double>zhan;
    108. //定义一个
    109. Stack<double> *p=new Stack<double>(zhan);
    110. //入栈
    111. zhan.stack_push(p,8.12);
    112. zhan.stack_push(p,6.27);
    113. zhan.stack_push(p,9.08);
    114. zhan.stack_push(p,9.29);
    115. zhan.stack_push(p,1.99);
    116. zhan.stack_show(p);
    117. cout<<"*******************************"<
    118. //出栈
    119. zhan.stack_pop(p);
    120. zhan.stack_pop(p);
    121. zhan.stack_pop(p);
    122. zhan.stack_show(p);
    123. //获取栈顶元素
    124. zhan.stack_gettop(p);
    125. //求栈大小
    126. zhan.stack_size(p);
    127. //销毁栈
    128. zhan.stack_free(p);
    129. return 0;
    130. }

    队列

    1. #include
    2. using namespace std;
    3. template<class T>
    4. class Queue
    5. {
    6. private:
    7. T data[8];
    8. int front;
    9. int tail;
    10. public:
    11. //构造函数
    12. Queue()
    13. {
    14. cout<<"构造函数"<
    15. front=0;
    16. tail=0;
    17. }
    18. //构析函数
    19. ~Queue()
    20. {
    21. }
    22. //判空
    23. int queue_empty(Queue *p)
    24. {
    25. if(NULL==p)
    26. {
    27. cout<<"队列不合法"<
    28. return -1;
    29. }
    30. return 0;
    31. }
    32. //判满
    33. int queue_full(Queue *p)
    34. {
    35. if(NULL==p)
    36. {
    37. cout<<"所给队列不和发"<
    38. return -1;
    39. }
    40. return 0;
    41. }
    42. //入队
    43. void queue_push(Queue *p,T e)
    44. {
    45. if(NULL==p||queue_full(p))
    46. {
    47. cout<<"入队失败"<
    48. return ;
    49. }
    50. p->data[p->tail]=e;
    51. p->tail=(p->tail+1)%8;
    52. cout<<"入队成功"<
    53. return ;
    54. }
    55. //出队
    56. void queue_pop(Queue *p)
    57. {
    58. if(NULL==p||queue_empty(p))
    59. {
    60. cout<<"出队失败"<
    61. return ;
    62. }
    63. cout<data[p->front]<<"出队成功"<
    64. p->front=(p->front+1)%8;
    65. return ;
    66. }
    67. //求队列长度
    68. void queue_size(Queue *p)
    69. {
    70. if(NULL==p)
    71. {
    72. cout<<"所给队列不合法"<
    73. return ;
    74. }
    75. cout<<"队列长度为"<<(p->tail+8-p->front)%8<
    76. return ;
    77. }
    78. //销毁队列
    79. void queue_free(Queue *p)
    80. {
    81. if(NULL!=p)
    82. {
    83. free(p);
    84. p=NULL;
    85. cout<<"释放成功"<
    86. return ;
    87. }
    88. cout<<"所给队列不合法"<
    89. return ;
    90. }
    91. //遍历
    92. void queue_show(Queue *p)
    93. {
    94. for(int i=p->front;i!=p->tail;i=(i+1)%8)
    95. {
    96. cout<data[i]<<" ";
    97. }
    98. cout<
    99. }
    100. };
    101. int main()
    102. {
    103. //定义一个队列
    104. Queue<int> dl;
    105. //创建指针 申请空间
    106. Queue<int> *p=new Queue<int>(dl);
    107. //入队
    108. dl.queue_push(p,8);
    109. dl.queue_push(p,60);
    110. dl.queue_push(p,19);
    111. dl.queue_push(p,627);
    112. dl.queue_push(p,78);
    113. dl.queue_push(p,1);
    114. dl.queue_push(p,2);
    115. dl.queue_show(p);
    116. cout<<"**********************"<
    117. //出队
    118. dl.queue_pop(p);
    119. dl.queue_pop(p);
    120. dl.queue_pop(p);
    121. dl.queue_show(p);
    122. cout<<"**********************"<
    123. //求队列长度
    124. dl.queue_size(p);
    125. //销毁队列
    126. dl.queue_free(p);
    127. return 0;
    128. }

  • 相关阅读:
    Flask 学习-48.Flask-RESTX 使用api.model() 模型工厂
    MySQL性能分析
    数字孪生是什么?【深度解析】
    通达信接口的定义和实现
    网卡和智能网卡
    leetcode刷题 (6.1) 字符串
    燕千云知识库,解决你的知识沉淀烦恼
    代码随想录笔记_动态规划_139单词拆分
    基于ssm008医院门诊挂号系统+jsp【附PPT|开题|任务书|万字文档(LW)和搭建文档】
    Pulsar Meetup 深圳 2024 大咖推荐
  • 原文地址:https://blog.csdn.net/2201_75732711/article/details/132862722