/media/sda-magnetic/david/Dokumente-15/fernuni-hagen/cs-i-ii/old-cs-2-01/2021-11-20-java/RingListExample.java


/* 

David Vajda, 7828020
Schickhardstrasse 5
D-72072, Tuebingen
https://www.ituenix.de 
david@ituenix.de 

*/

class RingListExample {
    public static void main (String [] args) {
        int i;
        RingLinList head;
        if (args.length != 0)
            head = new RingLinList (args [0]);
        else
            head = new RingLinList ();
        
        for (i = 1;  i < args.length;  i++)
            head.insert (args [i]);
        head.print ();
            
    }
    
    static class RingLinList {
        RingLinList next;
        RingLinList prev;
        String val;
        
        RingLinList () {
            this.next = this;
            this.prev = this;
            this.val = null;
        }
        RingLinList (String val) {
            this.next = this;
            this.prev = this;
            this.val = val;
        }
        void insert (String val) {
            RingLinList ptr = new RingLinList (val);
            ptr.prev = this.prev;
            ptr.next = this;
            this.prev.next = ptr;
            this.prev = ptr;
            return;
        }
        void print () {
            if ((this.next == this) && (this.prev == this) && (this.val == null)) 
                System.out.println ("This ring list is empty!");
            else if ((this.next == this) && (this.prev == this) && (this.val != null)) {
                System.out.println (this.val);
            }
            else {
                RingLinList ptr = this;
                while (ptr.next != this) {
                    System.out.println (ptr.val);
                    ptr = ptr.next;
                }
                System.out.println (ptr.val);
            }
            return;
        }
    }

}