#include "SLList.h"
#include <iostream>
int main(){
ods::SLList<int> l;
for (int i = 0; i < 10; i++){
l.add(i);
}
// l = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
std::cout << l.secondLast() << std::endl;
// output "8"
}
コンパイル, 実行例
bash-3.2$ g++-mp-4.9 -o checkEx3_2 checkEx3_2.cpp bash-3.2$ ./checkEx3_2 8
#include "SLList.h"
#include <iostream>
int main(){
ods::SLList<int> l;
for (int i = 0; i < 10; i++){
l.add(10+ i);
}
// l = [10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
l.set(5, 105);
// l = [10, 11, 12, 13, 14, 105, 16, 17, 18, 19]
l.add(2, 22);
// l = [10, 11, 22, 12, 13, 14, 105, 16, 17, 18, 19]
l.add(0, 20);
// l = [20, 10, 11, 22, 12, 13, 14, 105, 16, 17, 18, 19]
l.add(12, 30);
// l = [20, 10, 11, 22, 12, 13, 14, 105, 16, 17, 18, 19, 30]
std::cout << l.remove(1) << std::endl;;
// output 10
// l = [20, 11, 22, 12, 13, 14, 105, 16, 17, 18, 19, 30]
for (int i = 0; i < l.size(); i++)
std::cout << (i == 0 ? "[" : ", ") << l.get(i);
std::cout << "]" << std::endl;
// output [20, 11, 22, 12, 13, 14, 105, 16, 17, 18, 19, 30]
}
コンパイル, 実行例
bash-3.2$ g++-mp-4.9 -o checkEx3_3 checkEx3_3.cpp bash-3.2$ ./checkEx3_3 10 [20, 11, 22, 12, 13, 14, 105, 16, 17, 18, 19, 30]
#include <iostream>
#include "SLList.h"
int main(){
ods::SLList<int> l;
for (int i = 0; i < 10; i++){
l.add(10+ i);
}
// l = [10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
l.reverse();
// l = [19, 18, 17, 16, 15, 14, 13, 12, 11, 10]
for (int i = 0; i < l.size(); i++)
std::cout << (i == 0 ? "[" : ", ") << l.get(i);
std::cout << "]" << std::endl;
// output [19, 18, 17, 16, 15, 14, 13, 12, 11, 10]
}
コンパイル,実行例
bash-3.2$ g++-mp-4.9 -o checkEx3_4 checkEx3_4.cpp bash-3.2$ ./checkEx3_4 [19, 18, 17, 16, 15, 14, 13, 12, 11, 10]
T peek() {
return head->x;
}
T secondLast() {
Node *u = head;
for (int i = 0; i < n - 2; i++) u = u->next;
return u->x;
}
bool add(T x) {
(注) 要素数が0又は1の時にsecondLast()がどのような挙動を示すべきかは問題
には書かれていない.ここでは, エラーチェックを行っていないので,要素数
が0の時は,実行時にはSegmentation faultのようなエラーが発生する可能性が
あり,要素数が1の時はsecondLast() ではなくlast()を返す.
参考のために,変数tailも変数nも使わないlast()の定義を示します.
T last() {
if(n==0) return null;
Node * u = head;
while(u->next !=NULL) u = u->next;
return u->x;
}
Exercise 3.3はSLList.hのSLListの定義の中に,以下の行を入れてから「ここ
を埋める」のところにコードを書いて完成させる.
T get(int i) {
// ここを埋める
}
T set(int i, T x){
// ここを埋める
}
void add(int i, T x){
// ここを埋める
}
T remove(int i){
// ここを埋める
}
Exercise 3.4はSLList.hのSLListの定義の中に,以下の行を入れてから「ここ
を埋める」のところにコードを書いて完成させる.
void reverse() {
// ここを埋める
}
Node(T x0) {
x = 0;
next = NULL;
}
を
Node(T x0) {
x = x0;
next = NULL;
}
と修正する必要がある.
締切は,6/5(金) 23:59.締切を過ぎても2015年7月末までは6割を上限に採点する.