2014年4月23日水曜日

開発環境

C++実践プログラミング (スティーブ オウアルライン (著)、Steve Oualline (原著)、Steve Oualline(原著)、望月 康司(翻訳)、クイープ(翻訳) 、オライリー・ジャパン)のⅣ部(高度なプログラミング概念)の20章(高度なポインタ)、20.10(プログラミング実習)、実習 20-2.を解いてみる。

その他参考書籍

実習 20-2.

コード(BBEdit, Emacs)

test_linked.cpp

#include <string>
#include "linked.h"

void linked_list::remove(const std::string& item)
{
  std::cout << "「" << item << "」を削除" << std::endl;
  linked_list_element *previous_ptr;
  linked_list_element *current_ptr;

  if (first_ptr == NULL)
    return;
  
  current_ptr = first_ptr;
  previous_ptr = first_ptr;
  while (current_ptr != NULL && current_ptr->data != item) {
    previous_ptr = current_ptr;
    current_ptr = current_ptr->next_ptr;
  }
  if (current_ptr != NULL) {
    if (current_ptr == first_ptr)
      first_ptr = NULL;
    else {
      previous_ptr->next_ptr = current_ptr->next_ptr;
      delete current_ptr;
      current_ptr = NULL;
    }
  }
}

void linked_list::p()
{
  linked_list_element *current_ptr;
  std::cout << "リンクリスト: ";
  for (current_ptr = first_ptr; current_ptr != NULL;
       current_ptr = current_ptr->next_ptr)
    std::cout << current_ptr->data << " ";
  std::cout << std::endl;
}

int main(int argc, char *argv[])
{
  linked_list words;

  words.p();
  
  words.add_list("c");
  words.p();
  
  words.add_list("cpp");
  words.p();
  
  words.remove("c");
  words.p();

  words.add_list("c");
  words.p();

  words.remove("cpp");
  words.p();

  words.remove("python");
  words.p();

  words.remove("c");
  words.p();

  words.remove("scheme");
  words.p();

  words.add_list("cpp");
  words.p();
  
  return (0);
}

Makefile

CC=g++
CFLAGS=-g -Wall
all: test_linked

test_linked: test_linked.cpp
 ${CC} ${CFLAGS} -o test_linked test_linked.cpp

clean:
 rm test_linked

入出力結果(Terminal)

$ make && ./test_linked
g++ -g -Wall -o test_linked test_linked.cpp
リンクリスト: 
「c」を追加
リンクリスト: c 
「cpp」を追加
リンクリスト: cpp c 
「c」を削除
リンクリスト: cpp 
「c」を追加
リンクリスト: c cpp 
「cpp」を削除
リンクリスト: c 
「python」を削除
リンクリスト: c 
「c」を削除
リンクリスト: 
「scheme」を削除
リンクリスト: 
「cpp」を追加
リンクリスト: cpp 
$

0 コメント:

コメントを投稿