program stek11; type ttype = integer; ptitem = ^titem; titem = object info: ttype; next: ptitem; constructor init(x: ttype; nxt: ptitem); destructor done; end; constructor titem.init(x: ttype; nxt: ptitem); begin info := x; next := nxt end; destructor titem.done; begin end; type pttype = ^ttype; tstack = object head: ptitem; constructor init; destructor done; procedure push(x: ttype); function pop: ttype; function top: ttype; function empty: boolean; procedure print; end; constructor tstack.init; begin head := nil; end; destructor tstack.done; begin while not empty do pop end; procedure tstack.push(x: ttype); var p: ptitem; begin new(p, init(x, head)); head := p end; function tstack.pop: ttype; var p: ptitem; begin pop := -1; if not empty then begin p := head; head := head^.next; pop := p^.info; dispose(p, done); end else begin writeln('reading from empty stack'); halt(101) end; end; function tstack.top: ttype; begin if empty then top := maxInt else top := head^.info; end; function tstack.empty: boolean; begin empty := not assigned(head) end; procedure tstack.print; var p: ptitem; begin p := head; write('(stack) <'); while assigned(p) do begin write(p^.info, ' '); p := p^.next end; writeln('>') end; procedure sort_stack(var st: tstack); var changed: boolean; procedure deface(var new_st: tstack; next: ttype); var X: ttype; begin if not new_st.empty then begin X := new_st.pop; deface(new_st, X); end; if new_st.top < next then begin X := new_st.pop; new_st.push(next); new_st.push(X); changed := true; end else new_st.push(next); end; begin repeat changed := false; deface(st, st.pop); until not changed; end; function max_stack(var st: tstack): ttype; var max_value: ttype; procedure deface(var new_st: tstack; next: ttype); var X: integer; begin if not new_st.empty then begin X := new_st.pop; if max_value < X then max_value := X; deface(new_st, X); end; new_st.push(next); end; begin max_value := st.top; deface(st, st.pop); max_stack := max_value; end; var st_arr: array[1 .. 4] of tstack; i, j, k, X: integer; begin for i := 1 to 3 do begin st_arr[i].init; k := random(1) + 5; writeln('vvedite ', k, ' elementov ', i, '-go steka:'); for j := 1 to k do begin write(' -> '); readln(X); st_arr[i].push(X); end; end; st_arr[4].init; for i := 1 to 3 do begin writeln('#', i); st_arr[i].print; st_arr[4].push(max_stack(st_arr[i])); end; sort_stack(st_arr[4]); writeln('#4'); st_arr[4].print; end.