您的位置:首页 > 理论基础 > 数据结构算法

数据结构与面向对象学习4 栈实现

2014-05-29 14:29 357 查看
关于栈的实现及相关应用:

首先, 用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, 待添加。。。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐