3.24 Write routines to implement two stacks using only one array. Your stack routines should not declare an overflow unless every slot in the array is used
3.25 Propose a data structure that supports the stack push and pop operations and a third operation findMin, which returns the smallest element int the data structure, all in O(1) worst case time.(这个题不算做出来,现在还没想到O(1)时间的算法,现在的是O(N)的)
//3.24 dstack.hpp
#ifndef DSTACK_HPP__
#define DSTACK_HPP__
#include <iostream>
template< typename Object >
class DStack {
private:
class Item {
public:
Item(const int f = 0, Object d = Object(), int n = 0) :
data(d), firstS(f), num(n){}
public:
Object data;
int firstS;
int num;
};
public:
DStack(int capacity = 0) : theSize(0),
theCapacity(capacity + SPARE_CAPACITY),
aSize(0),bSize(0)
{
items = new Item[theCapacity];
}
~DStack()
{
delete items;
}
public:
const int size(const int first) const { return first == 1 ? aSize : bSize; }
const int capacity() const { return theCapacity; }
bool empty(const int first) const { return first == 1 ? aSize == 0 : bSize == 0; }
void push(const int first, const Object& val)
{
if(theSize == theCapacity)
{
std::cerr << "Over flow/n";
return;
}
else
{
int s = 0;
int num = size(first) + 1;
while(items[s].firstS != 0 && s < theCapacity)
++s;
items[s] = Item(first, val, num);
++theSize;
first == 1 ? ++aSize : ++bSize;
}
}
void pop(const int first)
{
int s = theCapacity - 1;
while(!empty(first))
{
if(items[s].num == size(first) && items[s].firstS == first)
{ break; }
else
{ --s; }
}
items[s].firstS = 0;
first == 1 ? --aSize : --bSize;
--theSize;
return;
}
Object top(const int first) const
{
int s = theCapacity - 1;
while(!empty(first))
{
if(items[s].num == size(first) && items[s].firstS == first)
{ break; }
else
{ --s; }
}
return items[s].data;
}
private:
DStack(const DStack&);
DStack& operator=(const DStack&);
Item* items;
int theSize;
int theCapacity;
int aSize;
int bSize;
enum { SPARE_CAPACITY = 16 };
};
#endif
//main.cpp
#include "dstack.hpp"
using namespace std;
int main()
{
DStack<int> stack;
stack.push(2, 1);
stack.push(2, 2);
stack.push(1, 1);
stack.push(1, 2);
stack.push(2, 3);
stack.push(2, 4);
stack.push(2, 5);
stack.push(2, 6);
stack.push(2, 7);
stack.push(2, 8);
stack.push(2, 9);
stack.push(2, 10);
stack.push(2, 11);
stack.push(2, 12);
stack.push(2, 13);
stack.push(2, 14);
cout << stack.size(2) << endl;
while(!stack.empty(2))
{
cout << stack.top(2) << "/n";
stack.pop(2);
}
cout << stack.size(2) << endl;
stack.push(1,3);
cout << stack.size(1) << endl;
while(!stack.empty(1))
{
cout << stack.top(1) << endl;
stack.pop(1);
}
cout << stack.size(1) << endl;
return 0;
}
//3.25 mstack.hpp
#ifndef MinStack_HPP__
#define MinStack_HPP__
#include <deque>
template< typename Object >
class MinStack {
public:
MinStack(const int size = 0)
{ init(size); }
~MinStack()
{
delete[] objects;
}
const int size() const { return theSize; }
bool empty() const { return theSize == 0; }
const int capacity() const { return theCapacity; }
const Object findMin() //this is not a O(1) solution
{
Object min = top();
while(!empty())
{
if(min > top())
min = top();
pop();
}
return min;
}
void push(const Object& val)
{
if(theSize == theCapacity)
reserve(2 * theCapacity + 1);
objects[theSize++] = val;
}
void pop()
{
if(theSize > 0)
--theSize;
}
Object top()
{
return objects[size() - 1];
}
private:
void resize(const int newSize)
{
if(newSize > theCapacity)
reserve(newSize * 2 + 1);
theSize = newSize;
}
void reserve(const int newCapacity)
{
if(newCapacity < theSize)
return;
Object* oldO = objects;
objects = new Object[newCapacity];
for(int i = 0; i < theSize; ++i)
{
objects[i] = oldO[i];
}
theCapacity = newCapacity;
delete[] oldO;
}
void init(const int size)
{
objects = new Object[size + SPARE_CAPACITY];
theSize = 0;
theCapacity = size + SPARE_CAPACITY;
}
MinStack(const MinStack&);
MinStack& operator=(const MinStack&);
//Object min;
Object* objects;
int theSize;
int theCapacity;
enum { SPARE_CAPACITY = 16 };
};
#endif