Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class ILinkedList {
- private Node head ;
- private Node tail ;
- private int size ;
- public Node getHead() {
- return head;
- }
- public void setHead(Node head) {
- this.head = head;
- }
- public Node getTail() {
- return tail;
- }
- public void setTail(Node tail) {
- this.tail = tail;
- }
- public int getSize() {
- return size;
- }
- public void setSize(int size) {
- this.size = size;
- }
- public void add (int index , Term a)
- {
- Node n = new Node (a);
- Node curr = head ;
- if( index > size - 1 || index < 0)
- {
- return;
- }
- if ( head == null )
- {
- head = n ;
- tail = n ;
- }
- else if (index == 0)
- {
- n.setNext(head);
- head = n;
- }
- else if (index == size )
- {
- add( a );
- }
- else
- {
- for( int i = 0 ; i < index - 1 ; i++ )
- {
- curr = curr.getNext();
- }
- n.setNext(curr.getNext());
- curr.setNext(n);
- }
- size++;
- }
- public void add(Term a)
- {
- Node n = new Node (a) ;
- if( head == null )
- {
- head = n ;
- }
- else
- {
- tail.setNext(n);
- }
- tail = n ;
- size++ ;
- }
- public Term get ( int index )
- {
- if( index < 0 || index > size - 1 )
- {
- return null ;
- }
- Node curr = head ;
- if(head == null )
- {
- return null ;
- }
- for(int i = 0 ; i < index ; i++ )
- {
- curr = curr.getNext();
- }
- return curr.getX();
- }
- public void set( int index , Term a )
- {
- if( index > size - 1 || index < 0)
- {
- return;
- }
- Node curr = head ;
- for(int i = 0 ; i < index ; i++ )
- {
- curr = curr.getNext();
- }
- curr.setX(a);
- }
- public void clear ( )
- {
- head = null ;
- tail = null ;
- size = 0 ;
- }
- public boolean isEmpty ()
- {
- return head == null ;
- }
- public void remove (int index)
- {
- if( head == null || index > size - 1 || index < 0 )
- {
- return;
- }
- if( head == tail )
- {
- head = null ;
- tail = null ;
- }
- else if( index == 0 )
- {
- Node removed = head ;
- head = head.getNext();
- removed.setNext(null);
- }
- else
- {
- Node curr = head;
- for(int i = 0 ; i < index-1 ; i++ )
- {
- curr = curr.getNext();
- }
- Node removed=curr.getNext();
- curr.setNext(removed.getNext());
- removed.setNext(null);
- }
- size--;
- }
- public ILinkedList sublist ( int fromIndex , int toIndex )
- {
- if ( head == null || fromIndex < 0 || fromIndex > size - 1 || toIndex < 0 || toIndex > size - 1 || fromIndex > toIndex)
- {
- return null ;
- }
- int sizeSublist = ( toIndex - fromIndex ) + 1 ;
- ILinkedList sublist = new ILinkedList() ;
- Node curr = head ;
- for( int i = 0 ; i < size ; i++)
- {
- if( IsIncluded(i, fromIndex, toIndex) )
- {
- sublist.add(curr.getX());
- }
- curr = curr.getNext() ;
- }
- sublist.setSize(sizeSublist);
- return sublist ;
- }
- public boolean IsIncluded (int index , int fromIndex , int toIndex)
- {
- return index >= fromIndex && index <= toIndex ;
- }
- public boolean contains ( Term a )
- {
- if ( head == null )
- {
- return false ;
- }
- Node curr = head ;
- while (curr != null )
- {
- if( curr.getX().equals(a) )
- {
- return true;
- }
- curr = curr.getNext() ;
- }
- return false ;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement