c-----------------------------------------------------------
c Chapter 14: (p19)
c-----------------------------------------------------------
c   name of subroutine: NEXYTB
c
c   Algorithm:
c
c   input: n=6   
c   complier: f77 nexytb_2.f
c-----------------------------------------------------------

	parameter (n=6)
	logical mtc
	integer lambda(n), y(n)
	integer i

	write(6,3)
3	format('please enter the array lambda:')
	read(5,*) (lambda(i),i=1,n)
	mtc=.false.
	
	write(*,12)
12 	format('the output y is:')
20      call nexytb(n, lambda, y, mtc)
	
	write(*,8) (y(i),i=1,n)
8       format(10i3)
        if(mtc) go to 20
        stop
	end

c-------subroutine begins here--------------------------------
	subroutine nexytb (n, lambda, y, mtc)
	logical mtc
	integer lambda(n), y(n), r, t
	t=n
	if (.not. mtc) go to 40
20	lambda(1)=1
	do 21 i=2, n
21	lambda(i)=0
	do 22 j=2,n
	lambda(y(j))=lambda(y(j))+1
	if (y(j) .lt. y(j-1)) go to 30
22	continue
	mtc=.false.
	return
30	t=lambda(1+y(j))
	i=n
31	if (lambda(i) .eq. t) go to 32
	i=i-1
	go to 31
32	y(j)=i
	lambda(i)=lambda(i)-1
	t=j-1
40	l=1
43	r=1
42	if (r .gt. n) go to 45
	if (lambda(r) .eq. 0) go to 41
	y(l)=r
	lambda(r)=lambda(r)-1
	l=l+1
	r=r+1
	go to 42
41	if (l .le. t) go to 43
45	if (n .eq. 1) go to 47
	do 46 j=2,n
	if (y(j) .lt. y(j-1)) go to 50
46	continue
47	mtc=.false.
	return
50	mtc=.true.
	return
	end

