• 周二. 7 月 16th, 2024

5G编程聚合网

5G时代下一个聚合的编程学习网

热门标签

test

admin

11 月 28, 2021
  1 /*
  2  * mystl.h
  3  *
  4  * Copyright (C) 2021 - lsc
  5  *
  6  * debug.h is free; you can redistribute it and/or modify
  7  * it under the terms of the GNU General Public License as published by
  8  * the Free Software Foundation; either version 2 of the License, or
  9  * (at your option) any later version.
 10  *
 11  * debug.h is distributed in the hope that it will be useful,
 12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 14  * GNU General Public License for more details.
 15  *
 16  * You should have received a copy of the GNU General Public License
 17  * along with . If not, see <http://www.gnu.org/licenses/>.
 18  
 19 ----------------------------------------------------------------------
 20  
 21 >Before you use it ,you need to know...
 22  
 23  * Name:mystl.h
 24  * Author: lsc
 25  * Update Date:14/08/21
 26 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
 27 #ifndef _MYSTL_H
 28 #define _MYSTL_H
 29 
 30 #include<iostream>
 31 using namespace std;
 32 /*
 33     This stl is not the best one.When you use it ,maybe it will run slower then the ture STL;
 34     But you can just to use it ,because the author has tested it many times;
 35 */
 36 
 37 template<class Type>
 38 class Node{
 39     public:
 40         Type da;
 41         Node *nxt;    //&
 42 };
 43 /*identify the data's apperance;*/
 44 template<class Type>
 45 class Stack{
 46     public:
 47         Stack();
 48         ~Stack();
 49 
 50         void push(Type N);
 51         void pop();
 52         bool empty();
 53         int size();
 54         Type top();
 55     private:
 56         Node<Type> *m_top;
 57         Node<Type> *m_bottom;
 58 };
 59 /*identify the Stack's appearance*/
 60 
 61 template<class Type>
 62 Stack<Type>::Stack(){
 63     Node<Type> *p=new Node<Type>;
 64     if(p==NULL){
 65         exit(0);
 66     }   
 67     else 
 68     {
 69         m_bottom=p;
 70         m_top=m_bottom;
 71     }
 72 }
 73 
 74 template<class Type>
 75 Stack<Type>::~Stack(){
 76     if(m_top!=m_bottom){
 77         while(m_bottom!=m_top)
 78         {
 79             Node<Type> *p;
 80             p=m_bottom;
 81             delete m_bottom;
 82             m_bottom=&(*p->nxt);
 83             delete p;
 84         }
 85     }
 86 }
 87 template<class Type>
 88 void Stack<Type>::push(Type N){
 89     Node<Type> *p;
 90     m_top->nxt=p;
 91     m_top=p;
 92     m_top->da=N;
 93     m_top=NULL;
 94 }
 95 
 96 template<class Type>
 97 bool Stack<Type>::empty(){
 98     return m_top==m_bottom;
 99 }
100 
101 template<class Type>
102 Type Stack<Type>::top(){
103     return *m_top->da;
104 }
105 #endif

test.cpp

 1 #include<cstdio>
 2 #include"mystl.h"
 3 using namespace std;
 4 int main()
 5 {
 6     Stack<int>s;
 7     if(s.empty())printf("000
");
 8     s.push(1);
 9     s.push(2);
10     printf("%d
",s.size());
11     s.pop();
12     if(!s.empty())printf("1111
");
13     printf("%d
",s.size());
14     return 0;
15 }

发表回复