Thursday, January 5, 2017

Data Structures 2214 Practicals - WUSL - Wayamba University

______________________________
-----arraytest.java------

public class arraytest {

 private double[] arrayname;
 private int nElements;

 public arraytest(int max){

     arrayname=new double[max];
     nElements=0;
 }
   
  public void insert(double value){
  arrayname[nElements]=value;
  nElements++;
  }
   
  public void display(){
      for(int j=0; j<nElements; j++)
          System.out.print(arrayname[j]+ " ");
      System.out.println("");
  }

  public boolean find(double searchkey){
      int i;
      for(i=0; i<nElements; i++){
          if(arrayname[i]==searchkey)
        break;}
      if (i==nElements)
        return false;
      else
        return true;
  }

  public boolean delete(double value){

      int a;
      for(a=0; a<nElements; a++)
          if(arrayname[a]==value)
              break;
      if(a==nElements)
          return false;
      else
        {
            for(int b=a; b<nElements; b++)
                arrayname[b]=arrayname[b+1];
            nElements--;
            return true;
        }

  }

  public void orderedinsert(double value){
      int c;
      for(c=0; c<nElements; c++)
          if(value<arrayname[c])
              break;
      for(int d=nElements; d>c ;d--)
          arrayname[d]=arrayname[d-1];
      arrayname[c]=value;
      nElements++;
         

  }

  public int findbinarysearch(double searchkey){
      int lowerbound=0;
      int upperbound=nElements-1;
      int curin;

     
      while (true){
          curin=(upperbound+lowerbound)/2;
          if(arrayname[curin]== searchkey)
              return curin;
          else if(upperbound<lowerbound)
              return nElements;
          else
          { if (arrayname[curin]<searchkey)
              lowerbound=curin+1;
          else
              upperbound=curin-1;                
          }
      }

  }
  //BUBBLE Sort
  private void swap(int one, int two){
      double temp= arrayname[one];
      arrayname[one]=arrayname[two];
      arrayname[two]=temp;
  }

  public void bubblesort(){
  int in, out;

  for(out=nElements; 1<out; out--)
      for (in=0; in<out; in++)
          if (arrayname[in]> arrayname[in+1])
              swap (in, in+1);

  }
  //SELECTION SORT

  public void selectionsort(){
      int out, in, min;
     
      for(out=0; out<nElements-1; out++){
          min=out;
     
          for(in=out+1; in<nElements; in++)
              if(arrayname[in]<arrayname[min])
                  min=in;
          swap(out,min);
  }
  }
  }

________________________

------arrayTestapp.java------


class arrayTestapp{

   
    public static void main(String[]args){
        int maxsize=50;
        arraytest arr = new arraytest(maxsize);
       
        arr.insert(50);
        arr.insert(20);
        arr.insert(15);
        arr.insert(89);
        arr.insert(21);
        arr.insert(89);
        arr.insert(70);
        arr.insert(42);
        arr.insert(11);

        arr.find(42);

       
        int searchkey=73;
        if(arr.find(searchkey))
            System.out.println(searchkey+ " Found");
        else
            System.out.println(searchkey+ " was not found");
    }
}

______________________

-----link.java-----


public class link {

    public int idata;
    public link next;
    public link first;
   
    public link(int id){
        idata=id;
    }
    public void displaylink(){
    System.out.println("{"+idata+"}");
    }
}

_______________


---linkedlistApp.java


public class linkedlist{
       private link first;
       
   public void linkist(){
       first=null;
      }
   public boolean isEmpty(){
       return (first==null);
   }
   
   
   public void insertfirst(int id){
       link newlink= new link(id);
       newlink.next=first;
       first=newlink;
   
   }
    public link deletefirst(){
        if(isEmpty())
            System.out.println("Empty List!!");
        else
            {link temp=first;
             first=first.next;
             return temp;
            }
        return null;
    }
    
    public void displayList(){
        link current=first;
        
        while(current != null){
            current.displaylink();
            current=current.next;
        }
    }

    public link find(int key){
    if(isEmpty())
        System.out.println("Empty List");
    else{
        link current=first;
        
        while(current.idata != key){
            if(current.next==null)
                return null;
            else
                current=current.next;
        }
        return current;    
    }  
    return null;
    }
    
    public link delete(int key){
        if(isEmpty())
            System.out.println("Empty List");
        else{
            link current=first;
            link previous=first;
            
            while(current.idata != key){
                if(current.next == null)
                    return null;
                else{
                    previous=current;
                    current=current.next;
                }
            }
            if(current==first)
                first=first.next;
            else
                previous.next=current.next;
            return current;  
        }
        return null;
    }
        
}

_______________________
//doubly linked list : few errors

class EmployeeDoublyLinkedList {
    int data;
    int empno; 
    String empname;
    String sex;
    double salary; 
    EmployeeDoublyLinkedList next;
    EmployeeDoublyLinkedList previous;
    
    EmployeeDoublyLinkedList(int x){
       data = x;
       next = previous = null;
   }
    public EmployeeDoublyLinkedList(int id, String name, String sex, double salary){       
        empno=id;  
        empname=name;
        this.sex=sex;
        this.salary=salary;
    }

    class linkedList{
    private EmployeeDoublyLinkedList first;         
    
    public void linkedList(){   
        first=null;             
    }

    EmployeeDoublyLinkedList(int x, EmployeeDoublyLinkedList nextNode, EmployeeDoublyLinkedList previousNode){
       data = x;
       next = nextNode;
       previous = previousNode;
   }
    
       public class DoublyLinkedList2 {
       private doublyNode head;
       private doublyNode tail;
       
       public DoublyLinkedList2(){
           head = tail = null;
       }
    public boolean isEmpty() {
        return head == null;
    }  
   
   
  public void addToHead(int item){
      if (isEmpty())
        head = tail = new EmployeeDoublyLinkedList(item);
      else
        head = head.previous = new EmployeeDoublyLinkedList(item, head, null);
  } 
   public void addToTail (int item){
      if (isEmpty())
          head = tail = new EmployeeDoublyLinkedList(item);
      else
          tail = tail.next = new EmployeeDoublyLinkedList(item, null,tail);
  } 
  
  
  public int removeFromHead(){
      int item=0;
      if(isEmpty())
          System.out.println("empty list!");
      item= head.data;
      if(head == tail)
          head = tail = null;
      else{
          head = head.next;
          head.previous=null;
      }
      return item;     
  }//End removeFromHead
  
  public int removeFromTail(){
      int item = 0;
      if(isEmpty())
          System.out.println("empty list!");
      item= tail.data;
      if(head == tail)
          head = tail = null;
      else{
          tail = tail.previous;
          tail.next=null;
      }
      return item;     
  }//End removeFromTail
  
  
  }
}
}

_______________________
----doublyNode.java----

public class doublyNode {
   int data;
   doublyNode next;
   doublyNode previous;
   
   doublyNode(int x){
       data = x;
       next = previous = null;
   }
   
   doublyNode(int x, doublyNode nextNode, doublyNode previousNode){
       data = x;
       next = nextNode;
       previous = previousNode;
   }
   
   public class DoublyLinkedList {
       private doublyNode head;
       private doublyNode tail;
       
       public DoublyLinkedList(){
           head = tail = null;
       }
    public boolean isEmpty() {
        return head == null;
    }  
   
   
  public void addToHead(int item){
      if (isEmpty())
        head = tail = new doublyNode(item);
      else
        head = head.previous = new doublyNode(item, head, null);
  } 
  
  public void addToTail (int item){
      if (isEmpty())
          head = tail = new doublyNode(item);
      else
          tail = tail.next = new doublyNode(item, null,tail);
  } 
  
  public int removeFromHead(){
      int item=0;
      if(isEmpty())
          System.out.println("empty list!");
      item= head.data;
      if(head == tail)
          head = tail = null;
      else{
          head = head.next;
          head.previous=null;
      }
      return item;     
  }//End removeFromHead
  
  public int removeFromTail(){
      int item = 0;
      if(isEmpty())
          System.out.println("empty list!");
      item= tail.data;
      if(head == tail)
          head = tail = null;
      else{
          tail = tail.previous;
          tail.next=null;
      }
      return item;     
  }//End removeFromTail
  
  public void print() {
      System.out.print("[ ");
      doublyNode current = head;
      while( current != null){
          System.out.print(current.data + " ");
          current = current.next;
      }
      System.out.print("] \n");
  }//End Print
  
  public void printReverse() {
      System.out.print("[ ");
      doublyNode current = tail;
      while( current != null){
          System.out.print(current.data + " ");
          current = current.previous;
      }
      System.out.print("] \n");
  }//END printReverse
  
  }//END DoublyLinkedList
   
   public static void main(String[] args) {
        DoublyLinkedList dll= new DoublyLinkedList();
        
        dll.addToHead(33);
        dll.addToTail(55);
        dll.addToHead(22);
        dll.addToTail(66);
        
        dll.print();
        dll.printReverse();
        
        dll.removeFromHead();
        dll.print();
        
        dll.removeFromTail();
        dll.print();
        
   }
   
}//END doublyNode


________________










public class binaryNode {
    int data;
    binaryNode left;
    binaryNode right;
    
    public binaryNode(int d){
        data=d;
        left=right= null;
    }
    
    public int getData(){
        return data;
    }
    
    
    class binarytree{
        private binaryNode root;
        
        public binarytree(){
            root=null;
        }
        public boolean isEmpty(){
            return root == null;
        }
       
    
    public void insert(int data){
    
    binaryNode newNode= new binaryNode(data);
    
    if(root==null)
        root=newNode;
    else{
        binaryNode current = root;
        binaryNode parent;
    }
    
    }

}
}



Data Structures 2214 Practicals - WUSL - Wayamba University