Listen Klasse Implementieren

    0

    0

    anlinn

    The List class contains an integer "info" field and a next field. The List class also has two constructors: one that takes two int values (x,l), and another that takes one int value (x). The List class also has two methods: one that inserts an element at position x in the list, and another that deletes the element at position x from the list. The toString() method returns a string that shows the list's contents, starting with the info field and continuing for each element in the list, separated by a comma. The List class also has a public instance variable next that points to the next list in the list's linked list.

    public class List{
      public int info;
      public List next;
      
      //Konstruktoren:
      public List (int x, List l){
        info = x;
        next=l;
      }
      
      public List (int x){
        info = x;
        next =null; 
      
      // Objekt- Methoden
        public void insert(int x){
        next = new List(x,next);
        }
      
        public void delete(){
          if (next != null)
            next = next.next;
        
        }
        
        public String toString(){
          String result = "["+info;
          for(List t = next; t!=null; t=t.next)
            result = result +", "+ t.info;
          return result +"]";
        }
        
        
        
        
        
        
      }
    
    
    }
    Codiga Logo
    Codiga Hub
    • Rulesets
    • Playground
    • Snippets
    • Cookbooks
    soc-2 icon

    We are SOC-2 Compliance Certified

    G2 high performer medal

    Codiga – All rights reserved 2022.