アルゴリズム(1)
本問を選択(Select this problem){ する(Yes),しない(No) } 受験番号(No.)
以下は,値が文字(char型のデータ)のノードを持つリストを扱うC言語の関数群である.リストはinit_list() によって初期化され,insert_next()によってノードが追加されdelete_next()によって削除される.このプ ログラムに基づいて問に答えよ.(The following is a set of C functions dealing with a linear list whose values are characters (typechar). The list is initialized byinit_list(), and a node is inserted byinsert_next() and deleted by delete_next(). Assuming these functions, answer the questions below.)
#include <stdio.h>
#include <stdlib.h>
struct node { char key; struct node *next; };
struct node *head;
void init_list() {
head = (struct node *)malloc(sizeof(*head));
head->next = NULL;
}
struct node *insert_next(char v, struct node *t) {
struct node *x = (struct node *)malloc(sizeof(*x));
x->key = v; x->next = t->next; t->next = x;
return x;
}
void delete_next(struct node *t) {
if(t->next != NULL) {
struct node *x = t->next;
t->next = t->next->next;
free(x);
} }
問1:以下は全てのノードを順に出力するプログラムである.空白([ (A) ]および[ (B) ]で示されている)
に入るべき文字を答えよ.(Q1: The folloing function prints out all the values in the list in order. Answer the parts of the program needed in the blanks indicated by [ (A) ]and [ (B) ].)
答1(A1): (A) (B)
void print_all(void) {
struct node *x = [ (A) ];
while(x != [ (B) ]) { printf("%c",x->key);
x = x->next;
}
printf("\n");
}
問2:次のプログラムの出力を答えよ.(Q2: Answer the output of the following program.) 答2(A2):
int main(void) {
struct node *x;
init_list();
(void)insert_next(’A’,head);
x = insert_next(’B’,head);
(void)insert_next(’C’,x);
(void)insert_next(’D’,x);
print_all();
}
問3:リストの中から,引数で指定された文字と同じ値を持つノードを全て削除する関数remove_char()を作成せ よ.remove_char()はvoid型の関数で,char型の引数を一つ取るものとする.なお 解答には裏面を用いること.
(Q3: Write functionremove_char()that deletes all nodes whose value is equal to the character given through the argument. The function has onechar type argument, and the return type isvoid. Use the reverse side for the answer.)