開発環境
- OS X Mavericks - Apple, ときどきWindows 8.1 + Cygwin64, MinGW (OS)
- Emacs (CUI)、BBEdit - Bare Bones Software, Inc. (GUI) (Text Editor)
- C++ (プログラミング言語)
- g++(コンパイラ)
C++実践プログラミング (スティーブ オウアルライン (著)、Steve Oualline (原著)、Steve Oualline(原著)、望月 康司(翻訳)、クイープ(翻訳) 、オライリー・ジャパン)のⅣ部(高度なプログラミング概念)の20章(高度なポインタ)、20.10(プログラミング実習)、実習 20-4.を解いてみる。
その他参考書籍
- C++プログラミング入門 (グレゴリー サティア (著)、ダウグ ブラウン (著)、Gregory Satir (原著)、Doug Brown (原著)、望月 康司 (翻訳)、谷口 功 (翻訳)、オライリージャパン)
実習 20-4.
コード(BBEdit, Emacs)
test_tree.cpp
#include <iostream> #include <cstdlib> #include <fstream> #include <string> #include "tree.h" tree words; void tree::remove_one(node *tree_node, node *parent_node, const std::string& word) { if (tree_node == NULL) return; if (tree_node->data == word) { if (tree_node->right == NULL) if (tree_node == root) root = tree_node->left; else if (word > parent_node->data) parent_node->right = tree_node->left; else parent_node->left = tree_node->left; else if (tree_node->left == NULL) if (tree_node == root) root = tree_node->right; else if (word > parent_node->data) parent_node->right = tree_node->right; else parent_node->left = tree_node->right; else { node *parent_node1; node *current_node; parent_node1 = tree_node; current_node = tree_node->left; while (current_node->right != NULL) { parent_node1 = current_node; current_node = current_node->right; } if (parent_node1 == tree_node) { if (tree_node == root) root = current_node; else if (word > parent_node->data) parent_node->right = current_node; else parent_node->left = current_node; current_node->right = tree_node->right; } else { if (current_node->left != NULL) parent_node1->right = current_node->left; current_node->left = tree_node->left; current_node->right = tree_node->right; current_node->left = tree_node->left; current_node->right = tree_node->right; if (tree_node == root) root = current_node; else if (word > parent_node->data) parent_node->right = current_node; else parent_node->left = current_node; } delete tree_node; tree_node = NULL; } } else if (tree_node->data < word) remove_one(tree_node->right, tree_node, word); else remove_one(tree_node->left, tree_node, word); } void tree::remove(std::string& word) { std::cout << word << "を削除----------\n"; node *parent_node; parent_node = NULL; remove_one(root, parent_node, word); } int main(int argc, char *argv[]) { int size = 6; std::string word_array[] = {"lemon", "apple", "pear", "grape", "plum", "orange", "strawberry"}; int i; words.print(); for (i = 0; i < size; ++i) { words.enter(word_array[i]); words.print(); } words.remove(word_array[2]); words.print(); words.remove(word_array[4]); words.print(); words.remove(word_array[1]); words.print(); words.remove(word_array[0]); words.print(); words.remove(word_array[6]); words.print(); words.remove(word_array[3]); words.print(); words.remove(word_array[5]); words.print(); words.remove(word_array[0]); words.print(); return (0); }
Makefile
CC=g++ CFLAGS=-g -Wall all: test_tree test_tree: test_tree.cpp tree.o ${CC} ${CFLAGS} -o test_tree test_tree.cpp tree.o tree.o: tree.cpp ${CC} -c -o tree.o tree.cpp clean: rm test_tree
入出力結果(Terminal)
$ make && ./test_tree g++ -g -Wall -o test_tree test_tree.cpp tree.o lemonを追加---------- lemon appleを追加---------- apple lemon pearを追加---------- apple lemon pear grapeを追加---------- apple grape lemon pear plumを追加---------- apple grape lemon pear plum orangeを追加---------- apple grape lemon orange pear plum pearを削除---------- apple grape lemon orange plum plumを削除---------- apple grape lemon orange appleを削除---------- grape lemon orange lemonを削除---------- grape orange strawberryを削除---------- grape orange grapeを削除---------- orange orangeを削除---------- lemonを削除----------
0 コメント:
コメントを投稿