/media/sda-magnetic/david/Extern-Magnetic-2022-06-29/Extern01/Dokumente-2021-05-8/disk10-ab-2020-01-10/02-debian-pc2-work/informatik/java-src/java-2020-10-14/Ringpuffer2.java


public class Ringpuffer2 {
    public static void main (String [] args) {
        int i;
        

        
            if (args.length < 1) {
                System.out.println ("You have not given the neccessary number of arguments");
            }
            else {
                Buffer buf = new Buffer (args[0]);
        
                for (i = 1;  i < args.length;  i++)
                    buf = buf.insert (args[i]);
                buf.print();
                
            }
    }
    
    public static class Buffer {
        Buffer next;
        Buffer prev;
        String info;
        
        Buffer (String info) {
            this.next = this;
            this.prev = this;
            this.info = info;
        }
        
        public Buffer insert (String info) {
            Buffer ptr = this;
            Buffer prev_ptr = this;
            Buffer new_ptr = new Buffer (info);
    
    
            if ((ptr.next == this) && (ptr.prev == this)) {
                new_ptr.prev = ptr;
                new_ptr.next = ptr;
                ptr.prev = new_ptr;
                ptr.next = new_ptr;
                if (ptr.info.compareTo(new_ptr.info) <= 0)
                    return ptr;
                else
                    return new_ptr;
            }
            if (info.compareTo(ptr.info) < 0) {
                new_ptr.next = ptr;
                new_ptr.prev = ptr.prev;
                ptr.prev.next = new_ptr;
                ptr.prev = new_ptr;
                return new_ptr; 
            }
            else {
                ptr = ptr.next;
                prev_ptr = ptr;
                while ((ptr != this) && (info.compareTo(ptr.info) > 0)){
                    prev_ptr = ptr;
                    ptr = ptr.next;
                }
                if (ptr == this) {
                    new_ptr.prev = prev_ptr;
                    new_ptr.next = ptr;
                    ptr.prev = new_ptr;
                    prev_ptr.next = new_ptr;
                    return ptr;
                }
                else {
                    if (ptr.next != this) {
                        new_ptr.next = prev_ptr.next;
                        new_ptr.prev = prev_ptr;
                        new_ptr.next.prev = new_ptr;
                        new_ptr.prev.next = new_ptr;
                        return this;
                    }
                    else {
                        new_ptr.next = this;
                        new_ptr.prev = this.prev;
                        this.prev.next = new_ptr;
                        this.prev = new_ptr;
                        return new_ptr;
                    }
                }
            }
        }
        public void print () {
            Buffer ptr = this;
            
            System.out.println (ptr.info);
            if (ptr.next != this) {
                ptr = ptr.next;
                while (ptr != this) {
                    System.out.println (ptr.info);
                    ptr = ptr.next;
                }
            }
        
        }
    }
}