Advertisement
Kulas_Code20

Untitled

Nov 1st, 2021
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.70 KB | None | 0 0
  1. package com.files;
  2.  
  3.  
  4. interface MyList<e> {
  5.     //sentinels
  6.     public boolean isEmpty();
  7.     public boolean isFull();
  8.     public boolean addItem(e item);
  9.     public boolean isItem(e item);
  10.     public boolean removeItem(e item);
  11.     public boolean insertAfter(e item, e newitem);
  12.     public boolean insertBefore(e item, e newitem);
  13.     public boolean updateItem(e item, e newitem);
  14.  
  15. }// end of interface
  16.  
  17. class MyListArray<E> implements MyList<E>{
  18.    
  19.     private int count;
  20.     private int position;
  21.     private E[] list;
  22.    
  23.     @SuppressWarnings("unchecked")
  24.     public MyListArray(){
  25.         this.list = (E[]) new Object[count];
  26.     }
  27.    
  28.     @SuppressWarnings("unchecked")
  29.     public MyListArray(int size) {
  30.         this.list = (E[]) new Object[size];
  31.     }
  32.    
  33.     @Override
  34.     public boolean isEmpty() {  return count == 0;  }
  35.  
  36.     @Override
  37.     public boolean isFull() {   return count == list.length;    }
  38.  
  39.     @Override
  40.     public boolean addItem(E item) {
  41.         if(!isEmpty()) {
  42.             list[count] = item;
  43.             count += 1;
  44.         }
  45.         return true;
  46.     }
  47.  
  48.     @Override
  49.     public boolean isItem(E item) {
  50.         boolean found = false;
  51.         for(int i=0; i<list.length; i++) {
  52.             if(item.equals(list[i])) {
  53.                 found = true;
  54.                 position = i;
  55.                 break;
  56.             }
  57.         }
  58.         return found;
  59.     }
  60.  
  61.     @Override
  62.     public boolean removeItem(E item) {
  63.         boolean isElem = isItem(item);
  64.         if(isElem) {
  65.             for(int i=0; i<position; i++) {
  66.                 if(list[i] == item) {
  67.                     list[i--] = null;
  68.                 }
  69.             }
  70.         }
  71.         return isElem;
  72.     }
  73.  
  74.     @Override
  75.     public boolean insertAfter(E item, E newitem) {
  76.         boolean isElem = isItem(item);
  77.         if(isElem) {
  78.             for(int i=0; i<list.length; i++) {
  79.                 if(position == i) {
  80.                     list[i++] = newitem;
  81.                 }
  82.             }
  83.         }
  84.         return isElem;
  85.     }
  86.  
  87.     @Override
  88.     public boolean insertBefore(E item, E newitem) {
  89.         boolean isElem = isItem(item);
  90.         if(isElem) {
  91.             for(int i=0; i<list.length; i++) {
  92.                 if(position == i) {
  93.                     list[++i] = newitem;
  94.                 }
  95.             }
  96.         }
  97.         return isElem;
  98.     }
  99.  
  100.     @Override
  101.     public boolean updateItem(E item, E newitem) {
  102.         boolean isElem = isItem(item);
  103.         if(isElem) {
  104.             for(int i=0; i<list.length; i++) {
  105.                 if(position == i) {
  106.                     list[i] = newitem;
  107.                 }
  108.             }
  109.         }
  110.         return isElem;
  111.     }
  112.    
  113.     @Override
  114.     public String toString() {
  115.         StringBuffer sb = new StringBuffer();
  116.         for(int i=0; i<list.length; i++) {
  117.             if(list.length != 0)
  118.                 sb.append("->");
  119.         }
  120.         return sb.toString();
  121.     }
  122. }//End of the class
  123.  
  124. public class TestList {
  125.     static public void main(String... args){
  126.           MyList<String> list=new MyListArray<String>();
  127.              list.addItem(new String("A"));
  128.              list.addItem(new String("B"));
  129.              list.addItem(new String("C"));
  130.              list.addItem(new String("D"));
  131.              list.addItem(new String("E"));
  132.              System.out.println(list);
  133.              System.out.println("Remove the item in the head:");
  134.              list.removeItem(new String("A"));
  135.              System.out.println(list);
  136.              System.out.println("Insert a new  item after tail:");
  137.              list.insertAfter(new String("E"),new String("X"));
  138.              System.out.println(list);
  139.              System.out.println("Insert a new  item after any item not the tail:");
  140.              list.insertAfter(new String("B"),new String("X"));
  141.              System.out.println(list);
  142.              System.out.println("Insert a new  item before head:");
  143.              list.insertBefore(new String("B"),new String("W"));
  144.              System.out.println(list);
  145.              System.out.println("Insert a new  item ebfore any item not the head:");
  146.              list.insertBefore(new String("D"),new String("W"));
  147.              System.out.println(list);
  148.              System.out.println("Update element D, changed it to Y");
  149.              list.updateItem(new String("D"),new String("Y"));
  150.              System.out.println(list);
  151.     }
  152. }
  153.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement