Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.files;
- interface MyList<e> {
- //sentinels
- public boolean isEmpty();
- public boolean isFull();
- public boolean addItem(e item);
- public boolean isItem(e item);
- public boolean removeItem(e item);
- public boolean insertAfter(e item, e newitem);
- public boolean insertBefore(e item, e newitem);
- public boolean updateItem(e item, e newitem);
- }// end of interface
- class MyListArray<E> implements MyList<E>{
- private int size;
- private int count;
- private E[] entries;
- private int position;
- @SuppressWarnings("unchecked")
- public MyListArray(int size) {
- this.entries = (E[]) new Object[size];
- }
- public MyListArray() { this(10); }
- @Override
- public boolean isEmpty() { return count == 0; }
- @Override
- public boolean isFull() { return count == entries.length; }
- @Override
- public boolean addItem(E item) {
- entries[count++] = item;
- return true;
- }
- @Override
- public boolean isItem(E item) {
- boolean found = false;
- for(int i=0; i<entries.length; i++) {
- if(item.equals(entries[i])) {
- found = true;
- position = i;
- break;
- }
- }
- return found;
- }
- @Override
- public boolean removeItem(E item) {
- boolean isElem = isItem(item);
- if(isElem) {
- for(int i=0; i<entries.length; i++) {
- if(i == 0) {
- entries[0] = null;
- count--;
- break;
- }
- }
- }
- return isElem;
- }
- @Override
- public boolean insertAfter(E item, E newitem) {
- boolean isElem = isItem(item);
- if(isElem) {
- count++;
- for(int i=entries.length-1; i>position; i--) {
- entries[i] = entries[i-1];
- }
- entries[position+1] = newitem;
- }
- return isElem;
- }
- @Override
- public boolean insertBefore(E item, E newitem) {
- boolean isElem = isItem(item);
- if(isElem) {
- count++;
- for(int i=entries.length-1; i>position; i--) {
- entries[i] = entries[i-1];
- }
- entries[position] = newitem;
- }
- return isElem;
- }
- @Override
- public boolean updateItem(E item, E newitem) {
- boolean isElem = isItem(item);
- if(isElem) {
- for(int i=0; i<entries.length; i++) {
- if(position == i) {
- entries[count++] = newitem;
- }
- }
- }
- return isElem;
- }
- public String toString() {
- StringBuffer sb = new StringBuffer();
- //sb.append(entries[0]);
- for(int i=0; i<entries.length; i++) {
- if(entries[i] != null) {
- sb.append(entries[i]+"->");
- }
- }
- return sb.toString();
- }
- }//End of the class
- public class TestList {
- static public void main(String... args){
- MyList<String> list = new MyListArray<String>();
- list.addItem(new String("A"));
- list.addItem(new String("B"));
- list.addItem(new String("C"));
- list.addItem(new String("D"));
- list.addItem(new String("E"));
- System.out.println(list);
- System.out.println("Remove the item in the head:");
- list.removeItem(new String("A"));
- System.out.println(list);
- System.out.println("Insert a new item after tail:");
- list.insertAfter(new String("E"),new String("X"));
- System.out.println(list);
- System.out.println("Insert a new item after any item not the tail:");
- list.insertAfter(new String("B"),new String("X"));
- System.out.println(list);
- System.out.println("Insert a new item before head:");
- list.insertBefore(new String("B"),new String("W"));
- System.out.println(list);
- System.out.println("Insert a new item ebfore any item not the head:");
- list.insertBefore(new String("D"),new String("W"));
- System.out.println(list);
- System.out.println("Update element D, changed it to Y");
- list.updateItem(new String("D"),new String("Y"));
- System.out.println(list);
- }
- }//End of the class
Add Comment
Please, Sign In to add comment