關於棧的實現及相關應用:
首先, 用vector容器實現了一個簡單的stack結構,因爲stack 遵循LIFO(last in first out)的原理,因此,我們在建立stack的時候,直接在vector中插入元素(使用insert而不是push_back)。
雖然在建立的時候,時間複雜度會高,因爲每次建立都會存在vector元素的移動,時間複雜度爲 O(n^2),pop()和top()的時間複雜度爲O(n),因此不是一個很好的構造棧的方法,但是這裏先了解一下stack的結構以及今後要用到的stack的問題,以後再改進stack的結構。
(注:在以後用stack求解問題時候,我直接使用STL庫中自帶的stack)
以下是,my_stack 用容器vector的實現,main函數中包含了測試程序:
(注:這裏有一個print()函數,當stack的元素是結構體的時候,print函數失效,可以重新寫一個打印函數,專門打印相應的結構體)
#include <iostream>
#include<stack>
#include<vector>
#include<cstdlib>
using namespace std;
/*using stack to implement some problem*/
/*first implement our stack*/
template<class T>
class my_stack
{
private:
vector<T> array;
public:
my_stack();
~my_stack();
bool isempty();
int size();
T top();
void push(T t);
void pop();
void print();
};
template<class T>
my_stack<T>::my_stack()
{
}
template<class T>
my_stack<T>::~my_stack()
{
array.clear();
}
template<class T>
bool my_stack<T>::isempty()
{
return (array.size() < 1);
}
template<class T>
int my_stack<T>::size()
{
return array.size();
}
template<class T>
T my_stack<T>::top()
{
if(this->isempty())
{
cout<<"there is no element in the stack!!"<<endl;
exit(-1);
}
else
{
return array[0];
}
}
template<class T>
void my_stack<T>::pop()
{
if(this->isempty())
{
cout<<"there is no element in the stack!!"<<endl;
exit(-1);
}
else
{
vector<T> newarray;
for(int i = 1; i < array.size(); i++)
newarray.push_back(array[i]);
array.clear();
array = newarray;
}
}
template<class T>
void my_stack<T>::push(T t)
{
array.insert(array.begin(),t);
}
template<class T>
void my_stack<T>::print()
{
for(int i = 0; i < array.size(); i++)
cout<<array[i] << " ";
cout << endl;
}
int main (int argc, char *argv[])
{
my_stack<int> ms;
if(!ms.isempty())
cout << "success intialized!!"<<endl;
for(int i = 0; i < 10 ; i++)
ms.push(i+10);
cout<< "first initilaize...." << endl;
ms.print();
for(int i = 0; i < 4; i++)
ms.pop();
cout << "after pop out 4 elements..." <<endl;
ms.print();
cout <<"the top element is:.... " << ms.top() << endl;
cout << "the size of array is... " << ms.size()<<endl;
cout << "Press ENTER to continue..." << endl;
cin.get();
return 0;
}
代碼2:
利用C++ 中動態數組寫了一個stack類,跟用vector寫的類似。
// OOD.cpp : 定義控制檯應用程序的入口點。
//
#include "stdafx.h"
#include<iostream> // this is system header file, so use <>
using namespace std;
template<class T>
class mystack
{
public:
mystack();
~mystack();
void push(T value);
void pop();
T top();
int size();
bool isempty();
private:
int capacity;
T* arr;
int used;
};
template<class T>
mystack<T>::mystack()
{
capacity = 4; // maximun number for storage
arr = new T[capacity];
used = 0;
}
template<class T>
mystack<T>::~mystack()
{
delete[] arr;
}
template<class T>
int mystack<T>::size()
{
return used;
}
template<class T>
bool mystack<T>::isempty()
{
return (used < 1);
}
template<class T>
void mystack<T>::pop()
{
if(isempty())
{
cout << "the stack is empty!" << endl;
return;
}
else
{
used--;
}
}
template<class T>
void mystack<T>::push(T value)
{
if(used >= capacity-1)
{
capacity *= 2;
T* newarr = new T[capacity];
for(int i = 0; i < used; i++)
newarr[i] = arr[i];
arr = newarr;
}
arr[used] = value;
used++;
}
template<class T>
T mystack<T>::top()
{
if(isempty())
{
cout<<"the stack is empty!"<<endl;
exit(-1);
}
else
{
return arr[used-1];
}
}
int main()
{
mystack<int> ms;
for(int i = 0; i < 5; i++)
ms.push(i);
int size = ms.size();
for(int i = 0; i < size; i++)
{
cout << ms.top() << " ";
ms.pop();
}
system("pause");
return 0;
}
代碼3:利用鏈表實現一個stack, 待添加。。。