3.26 Show how to implement three stacks in one array.
3.28 A deque is a data structrue consisting of a list of items, on which the following operations are possible:
push(x): Insert item x on the front end of the deque.
pop(): Remove the front item from the deque and return it.
inject(x): Insert item x on the rear end of the deque.
eject(): Remove the rear item from the deque and return it.
//3.26 tstack.hpp
#ifndef TSTACK_HPP__
#define TSTACK_HPP__
#include <iostream>
template< typename Object >
class TStack {
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:
TStack(int capacity = 0) : theSize(0),
theCapacity(capacity + SPARE_CAPACITY),
aSize(0),bSize(0),cSize(0)
{
items = new Item[theCapacity];
}
~TStack()
{
delete items;
}
public:
const int size(const int first) const { return first == 1 ? aSize : (first == 2 ? bSize : cSize); }
const int capacity() const { return theCapacity; }
bool empty(const int first) const { return first == 1 ? aSize == 0 : (first == 2 ? bSize == 0 : cSize == 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 : (first == 2 ? ++bSize : ++cSize);
}
}
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 : (first == 2 ? --bSize : --cSize);
--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:
TStack(const TStack&);
TStack& operator=(const TStack&);
Item* items;
int theSize;
int theCapacity;
int aSize;
int bSize;
int cSize;
enum { SPARE_CAPACITY = 16 };
};
#endif
//main.cpp
#include "tstack.hpp"
using namespace std;
int main()
{
TStack<int> t;
t.push(3,50);
t.push(2,3);
t.push(1,1);
t.push(1,2);
t.push(2,1);
t.push(2,2);
t.push(3,100);
t.push(3,200);
t.push(1,4);
cout << "A size: " << t.size(1);
cout << "/nB size: " << t.size(2);
cout << "/nC size: " << t.size(3);
cout << "/nA elems: /n";
while(!t.empty(1))
{
cout << t.top(1) << "/n";
t.pop(1);
}
t.push(3,500);
t.push(3,1000);
cout << "/nC size: " << t.size(3);
cout << "/nB elems: /n";
while(!t.empty(2))
{
cout << t.top(2) << "/n";
t.pop(2);
}
cout << "/nC elems: /n";
while(!t.empty(3))
{
cout << t.top(3) << "/n";
t.pop(3);
}
return 0;
}
//3.28 deque.hpp
#ifndef DEQUE_HPP__
#define DEQUE_HPP__
template< typename Object >
class Deque {
private:
class Node {
public:
Node(const Object& val = Object(), Node* n = 0, Node* p = 0) :
data(val), next(n), prev(n) {}
public:
Node* next;
Node* prev;
Object data;
};
public:
Deque() : head(new Node()), rear(new Node()), theSize(0)
{
head->next = rear;
rear->next = head;
}
~Deque()
{
while(!empty())
pop();
delete head;
delete rear;
}
bool empty() const { return theSize == 0; }
const int size() const { return theSize; }
void push(const Object& val)
{
Node* newnode = new Node(val, head->next, head);
head->next->prev = newnode;
head->next = newnode;
++theSize;
}
const Object pop()
{
Node* old = head->next;
Object retVal = old->data;
head->next = old->next;
old->prev = head;
delete old;
--theSize;
return retVal;
}
void inject(const Object& val)
{
Node* newnode = new Node(val, rear, rear->prev);
rear->prev->next = newnode;
rear->prev = newnode;
++theSize;
}
const Object eject()
{
Node* old = rear->prev;
Object retVal = old->data;
rear->prev = old->prev;
old->prev->next = rear;
delete old;
--theSize;
return retVal;
}
private:
Node* head;
Node* rear;
int theSize;
};
#endif