program sorting;
uses def24;
var c : char;
    s : stack;
    t : telem;

procedure Concat(var s1	: stack;s2:stack);
var s3:stack;
begin
   init(s3);
   while not empty(s2) do begin
      put(s3,top(s2));get(s2);
   end;
   while not empty(s3) do begin
      put(s1,top(s3));
      get(s3)
   end;
end; { Concat }

procedure QuickSort(var s : stack);
var s1,s2 : stack;V,k:telem;
begin
   if not empty(s) then begin
      init(s1);init(s2);
      V:=top(s);get(s);
      while not empty(s) do begin
	 k:=top(s);
	 if k.key< V.key then put(s1,k)
	 else put(s2,k);
	 get(s);
      end;
     QuickSort(s1);
     QuickSort(s2);
      put(s1,V);
      Concat(s1,s2);
      s:=s1;
   end;
end; { QuickSort }

var
  st: string;
  i: colors;
Begin
   init(s);
   repeat
    writeln('1=Init 2=Empty 3=Get 4=Top 5=Put 6=Print 7=Kolvo 8=Sort 9=Exit');
      readln(c); case c of

	'1' : init(s);

	'2' : writeln(empty(s));

	'3' : if not empty(s) then get(s)
	        else writeln('Stack is empty!');

        '4' : if not empty(s) then begin
	           t:=top(s);
	           writeln('Key: ',t.key:1,'. Data: ',s_colors[t.data],'.');
	      end
	        else writeln('Stack is empty!');

        '5' : begin
	        writeln('Input key:'); readln(t.key);
	        writeln('Input data:'); // readln(t.data);
                readln(st);
                for i := low(colors) to high(colors) do
                  if s_colors[i] = st then begin
                    t.data := i; put(s, t); break;
                  end;
	        // put(s,t);
	      end;

	'6' : if not empty(s) then print(s)
	        else writeln('Stack is empty!');

	'7' : writeln('Number of elements: ',kolvo(s):1);

	'8' :  if empty(s) then writeln('Stack is empty!')
               else if kolvo(s)>1 then QuickSort(s);
      end;
   until c='9';
end.