[練習問題 3-1]次の操作後のスタック S の内容と,取り出された要素を示せ.
(0) PUSH(’s’,S) (1) PUSH(’t’,S) (2) PUSH(’a’,S)
(3) PUSH(’c’,S) (4) POP(S) (5) PUSH(’k’,S)
(6) POP(S) (7) POP(S) (8) PUSH(’t’,S)
(9) POP(S) (10) PUSH(’o’,S) (11) PUSH(’p’,S)
(1) (2) (3) (4) (5) (6) (7) (8) (9) (10) (11)
’s’
[練習問題 3-2]データ構造 struct Lcellを用いてスタックを実現するための push 関数 void StPush(elmtype x, StackP st) pop 関数 elmtype StPop(StackP st)
を記述しなさい.github 上の queue_sample1.c を参考にすると良いかもしれません 提出:2018/1/21
学籍番号 氏名 点数
アルゴリズム論第一(庄野) 課題 その3
(裏面の使用可.複数枚のホッチキス止めはダメ)
't'
's' 'a' 't'
's' 't' 'a' 'c'
's' 't' 'a'
'c'
's' 't' 'a' 'k'
's' 't' 'a'
'k'
's' 't' 'a'
's' 't' 't'
's' 't' 't'
's' 't' 'o'
's' 't' 'o' 'p'
typedef struct __Stack__{
LcellP top;
int n;
} Stack;
typedef Stack* StackP;
StackP StCreate(void) { StackP s;
s = (StackP)malloc(sizeof(Stack));
s->top = NULL;
s->n = 0;
return s;
}
void StPush(elmtype x, StackP st) { n = GetNewLCell();
n->elment = x;
n->next = NULL;
if(StIsEmpty(st)){
st->top = n;
st->n = 1;
}else{
n->next = st->top;
st->top = n;
st->n += 1;
} }
elmtype StPop(StackP st)
{ elmtype x;
LcellP p;
if(st->n > 0){
x = st->top->x;
p = st->top;
st->top = st->top->next;
st->n -= 1;
FreeCell(p);
}else{ // Stack が空の場合(エラー)
x = 0; //エラー処理は適当です.
} return x;
}