開発環境
- 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-3.を解いてみる。
その他参考書籍
- C++プログラミング入門 (グレゴリー サティア (著)、ダウグ ブラウン (著)、Gregory Satir (原著)、Doug Brown (原著)、望月 康司 (翻訳)、谷口 功 (翻訳)、オライリージャパン)
実習 20-3.
コード(BBEdit, Emacs)
double_linked.h
#include <iostream>
class double_list {
private:
class double_list_element {
public:
int data;
private:
double_list_element *next_ptr;
double_list_element *previous_ptr;
friend class double_list;
};
public:
double_list_element *head_ptr;
double_list() {head_ptr = NULL;}
void enter(int item);
void remove(int item);
void p();
};
void double_list::enter(int item)
{
std::cout << item << "を追加" << std::endl;
double_list_element *new_ptr;
new_ptr = new double_list_element;
new_ptr->data = item;
if (head_ptr == NULL) {
new_ptr->next_ptr = NULL;
new_ptr->previous_ptr = NULL;
head_ptr = new_ptr;
}
else {
double_list_element *insert_ptr;
insert_ptr = head_ptr;
while (true) {
if (insert_ptr->next_ptr == NULL && item > insert_ptr->data) {
new_ptr->next_ptr = NULL;
new_ptr->previous_ptr = insert_ptr;
insert_ptr->next_ptr = new_ptr;
if (insert_ptr->previous_ptr == NULL)
head_ptr = insert_ptr;
break;
}
if (item <= insert_ptr->data) {
new_ptr->next_ptr = insert_ptr;
new_ptr->previous_ptr = insert_ptr->previous_ptr;
if (insert_ptr->previous_ptr != NULL)
insert_ptr->previous_ptr->next_ptr = new_ptr;
insert_ptr->previous_ptr = new_ptr;
if (new_ptr->previous_ptr == NULL)
head_ptr = new_ptr;
break;
}
insert_ptr = insert_ptr->next_ptr;
}
}
}
void double_list::remove(int item)
{
std::cout << item << "を削除" << std::endl;
double_list_element *current_ptr;
current_ptr = head_ptr;
while (true) {
if (current_ptr == NULL)
break;
if (item == current_ptr->data) {
if (current_ptr == head_ptr) {
head_ptr = current_ptr->next_ptr;
if (head_ptr != NULL)
head_ptr->previous_ptr = NULL;
}
else {
current_ptr->previous_ptr->next_ptr = current_ptr->next_ptr;
current_ptr->next_ptr->previous_ptr = current_ptr->previous_ptr;
}
delete current_ptr;
current_ptr = NULL;
break;
}
current_ptr = current_ptr->next_ptr;
}
}
void double_list::p()
{
std::cout << "二重リンクリスト" << std::endl;
double_list_element *current_ptr;
double_list_element *last_ptr;
current_ptr = head_ptr;
last_ptr = head_ptr;
std::cout << "昇順: ";
while (current_ptr != NULL) {
last_ptr = current_ptr;
std::cout << current_ptr->data << ' ';
current_ptr = current_ptr->next_ptr;
}
std::cout << std::endl << "降順: ";
current_ptr = last_ptr;
while (current_ptr != NULL) {
std::cout << current_ptr->data << ' ';
current_ptr = current_ptr->previous_ptr;
}
std::cout << std::endl;
}
test_double_linked.cpp
#include "double_linked.h"
int main(int argc, char *argv[])
{
double_list dl;
int nums[] = {10, 2, 8, 10, 4, 6};
int i;
dl.p();
for (i = 0; i < 6; ++i) {
dl.enter(nums[i]);
dl.p();
}
for (i = 1; i <= 10; ++i) {
dl.remove(i);
dl.p();
}
dl.remove(10);
dl.p();
dl.remove(100);
dl.p();
return (0);
}
Makefile
CC=g++
CFLAGS=-g -Wall
all: test_double_linked
test_double_linked: test_double_linked.cpp double_linked.h
${CC} ${CFLAGS} -o test_double_linked test_double_linked.cpp
clean:
rm test_double_linked
入出力結果(Terminal)
$ make && ./test_double_linked g++ -g -Wall -o test_double_linked test_double_linked.cpp 二重リンクリスト 昇順: 降順: 10を追加 二重リンクリスト 昇順: 10 降順: 10 2を追加 二重リンクリスト 昇順: 2 10 降順: 10 2 8を追加 二重リンクリスト 昇順: 2 8 10 降順: 10 8 2 10を追加 二重リンクリスト 昇順: 2 8 10 10 降順: 10 10 8 2 4を追加 二重リンクリスト 昇順: 2 4 8 10 10 降順: 10 10 8 4 2 6を追加 二重リンクリスト 昇順: 2 4 6 8 10 10 降順: 10 10 8 6 4 2 1を削除 二重リンクリスト 昇順: 2 4 6 8 10 10 降順: 10 10 8 6 4 2 2を削除 二重リンクリスト 昇順: 4 6 8 10 10 降順: 10 10 8 6 4 3を削除 二重リンクリスト 昇順: 4 6 8 10 10 降順: 10 10 8 6 4 4を削除 二重リンクリスト 昇順: 6 8 10 10 降順: 10 10 8 6 5を削除 二重リンクリスト 昇順: 6 8 10 10 降順: 10 10 8 6 6を削除 二重リンクリスト 昇順: 8 10 10 降順: 10 10 8 7を削除 二重リンクリスト 昇順: 8 10 10 降順: 10 10 8 8を削除 二重リンクリスト 昇順: 10 10 降順: 10 10 9を削除 二重リンクリスト 昇順: 10 10 降順: 10 10 10を削除 二重リンクリスト 昇順: 10 降順: 10 10を削除 二重リンクリスト 昇順: 降順: 100を削除 二重リンクリスト 昇順: 降順: $
0 コメント:
コメントを投稿