#include <iostream>
using namespace std;
// クラス宣言部
#define SIZE 10
// 文字を保存するstackクラスを宣言する
template <class T> class stack {
T stck[SIZE]; // スタック領域を確保する
int tos; // スタック先頭の索引
public:
stack(); // コンストラクタ (※1)
~stack(); // デストラクタ (※2)
void init();
void push(T ch); // スタックに文字をプッシュする
T pop(); // スタックから文字をポップする
};
// クラス実現部
// コンストラクタ
template <class T> stack<T>::stack()
{
cout << "スタックを生成する\n";
tos = 0;
}
// デストラクタ
template <class T> stack<T>::~stack()
{
cout << "スタックを破棄する\n";
}
// スタックを初期化する
template <class T> void stack<T>::init()
{
tos = 0;
}
// 文字をプッシュする
template <class T> void stack<T>::push(T ch)
{
if(tos==SIZE) {
cout << "スタックは一杯です";
return;
}
stck[tos] = ch;
tos++;
}
// 文字をポップする
template <class T> T stack<T>::pop()
{
if(tos==0) {
cout << "スタックは空です";
return 0; // スタックが空の場合はヌルを返す
}
tos--;
return stck[tos];
}
|
#include <iostream>
using namespace std;
class node{
node* left;
node* right;
int data;
public:
node(){ left=right=0; } // デフォルトコンストラクタ
node (node* l, node* r, int d){ left=l; right=r; data=d;}; // 引数つき
void printData(){ cout << data << "\n"; };
friend class list;
};
class list{
node* root;
node* last;
public:
list(); // デフォルトコンストラクタ
~list(); // デストラクタ
void insertRoot(int d);
void insertLast(int d);
int getLastValue();
void printAll();
void deleteLast(); //(※)
};
// デフォルトコンストラクタ
list::list(){
root = 0;
last = 0; // ヌルポインタで初期化
}
// デストラクタ
list::~list(){
node *nl;
node *nr;
nl=root;
while(nl!=0){
nr = nl->right;
delete nl;
nl = nr;
}
}
void list::insertRoot(int d){
if(last==0){ // root に node がない (同時に last にも node がない)
root = new node(0,0,d);
last = root;
}else{
root->left = new node(0,root,d); // 新たな node を root の左側に
root = root->left; // root ポインタのつけ変え
}
}
void list::insertLast(int d){
if(last==0){ // last に node がない (同時に root にも node がない)
last = new node(0,0,d);
root = last;
}else{
last->right = new node(last,0,d); // 新たな node を last の右側に
last = last->right; // last ポインタのつけ変え
}
}
int list::getLastValue(){
if(last != 0){ // ヌルポインタでなかったら
return(last->data);
}else{
return(0);
}
}
void list::printAll(){
node *n;
n=root;
while(n!=0){
n->printData();
n=n->right;
}
}
void list::deleteLast(){ //(※)
if(last !=0){ //(※)
node *n; //(※)
n = last->left; //(※)
delete last; //(※)
if(n!=0){ //(※)
n->right = 0; //(※)
} //(※)
last = n; //(※)
} //(※)
} //(※)
class queue{
list l;
public:
void push(int i);
int pop();
};
void queue::push(int i){
l.insertRoot(i); //(※)
}
int queue::pop(){
int val; //(※)
val = l.getLastValue(); //(※)
l.deleteLast(); //(※)
return(val); //(※)
}
int main(){
queue q;
for(int i=0 ; i<10 ; i++){
q.push(i);
}
for(int i=0 ; i<10 ; i++){
cout << q.pop() << "\n";
}
return(0);
}
|
//一つ目の※
}else{ // k >= key なら
bool result; // 挿入されたかどうかの結果を格納する変数
if(rightNode != 0){ // 右の枝に BNode が存在すれば
result = rightNode->insert(k,d);
}else{ // 右の枝に BNode が存在しなければ
rightNode = new BNode(k,d); //新たな BNode を追加
rightNode->parentNode = this; //自分が新たな BNode の上位に
result = true; // 挿入されたので true
}
return result; // 挿入されたかどうかを return
}
//二つ目の※
}else{ // この Node より右の Node が求める Node なら
return(rightNode->find(k));
}
|