c++函數模板二棧實現

來源:互聯網
上載者:User

標籤:blog   public   amp   logs   pac   sem   type   std   實現   

1 沒有使用模板的棧實現

 1 #include <iostream> 2 #include <string> 3 using namespace std; 4  5  6 class Stack 7 { 8 public: 9     Stack(int size = 1024);10     ~Stack();11     bool isEmpty();12     bool isFull();13     void push(int data);14     int pop();15 private:16     int* space;17     int top;18 19 };20 21 Stack::Stack(int size)22 {23     space = new int(size);24     top = 0;25 }26 Stack::~Stack()27 {28     delete[]space;29 }30 bool Stack::isEmpty()31 {32     return top == 0;33 }34 bool Stack::isFull()35 {36     return top == 1024;37 }38 void Stack::push(int data)39 {40     space[top++] = data;41 }42 int Stack::pop()43 {44     return space[--top];45 }46 47 int main()48 {49     Stack s(100);50     int i = 0;51     /*if (!s.isFull())52     {53         s.push(10);54     }*/55     while (!s.isFull()&&i<1024)56     {57         s.push(i);58         i+=10;59     }60     i = 0;61     while (!s.isEmpty()&&i<1024)62     {63         cout << s.pop() << endl;64         i += 10;65     }66     cin.get();67     return 1;68 }

2 使用模板以後的棧實現

 1 #include <iostream> 2 #include <string> 3 using namespace std; 4  5 template<typename T> 6 class Stack 7 { 8 public: 9     Stack(int size)10     {11         space = new T[size];12         top = 0;13     }14     ~Stack();15     bool isEmpty();16     bool isFull();17     void push(T data);18     T pop();19 private:20     T* space;21     int top;22 23 };24 25 template<typename T>26 Stack<T>::~Stack()27 {28     delete[]space;29 }30 template<typename T>31 bool Stack<T>::isEmpty()32 {33     return top == 0;34 }35 template<typename T>36 bool Stack<T>::isFull()37 {38     return top == 1024;39 }40 template<typename T>41 void Stack<T>::push(T data)42 {43     space[top++] = data;44 }45 template<typename T>46 T Stack<T>::pop()47 {48     return space[--top];49 }50 51 int main()52 {53     Stack<double> s(100);54     if (!s.isFull())55         s.push(10.4);56     if (!s.isFull())57         s.push(10.5);58     if (!s.isFull())59         s.push(10.6);60     if (!s.isFull())61         s.push(10.7);62     if (!s.isFull())63         s.push(10.8);64     if (!s.isFull())65         s.push(10.9);66 67     Stack<string> s1(100);68     if (!s1.isFull())69         s1.push("ni");70     if (!s1.isFull())71         s1.push("hao");72     73     while (!s1.isEmpty())74     {75         cout << s1.pop() << endl;76     77     }78     cin.get();79     return 1;80 }

可以其他類型可以自己多敲敲哦

c++函數模板二棧實現

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.