Advertisement
-Statement-

HW5

Jun 4th, 2014
294
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.53 KB | None | 0 0
  1. public class ListNode implements Cloneable{
  2.     Object data;
  3.     ListNode nextNode;
  4.  
  5.     public ListNode(Object o) {
  6.         this(o, null);
  7.     }
  8.  
  9.     public ListNode(Object o, ListNode node) {
  10.         data = o;
  11.         nextNode = node;
  12.     }
  13.  
  14.     public Object getObject() {
  15.         return data;
  16.     }
  17.  
  18.     public ListNode getNext() {
  19.         return nextNode;
  20.     }
  21.  
  22.     public void SetNull() {
  23.         nextNode = null;
  24.     }
  25.  
  26.     public void setNext(ListNode l) {
  27.         nextNode = l;
  28.     }
  29.  
  30.     public void show() {
  31.  
  32.         if (nextNode == null)
  33.             System.out.print(getObject().toString());
  34.  
  35.         else {
  36.             System.out.print(getObject().toString());
  37.             nextNode.show();
  38.  
  39.         }
  40.     }
  41.  
  42.     public void showRev() {
  43.         if (nextNode == null)
  44.             System.out.print(getObject().toString());
  45.  
  46.         else {
  47.             nextNode.showRev();
  48.             System.out.print(getObject().toString());
  49.  
  50.         }
  51.     }
  52.  
  53.     public void addAtRec(int k, Object obj) {
  54.         if (k == 2) {
  55.             ListNode Add = new ListNode(obj);
  56.             Add.setNext(getNext());
  57.             this.setNext(Add);
  58.         } else
  59.             if(nextNode!=null)
  60.             nextNode.addAtRec(k - 1, obj);
  61.  
  62.     }
  63.     public Object clone() throws CloneNotSupportedException{
  64.         //if(data instanceof Cloneable){                                        זה לא נותן לי לעשות קלון לדאטא, נותן לי
  65.         //Cloneable Ret=(Cloneable) data;                                        את השגיאה שאני יצרף בפוסט
  66.             //return data.clone();
  67.         }
  68.     }
  69. }
  70. _______________________________________________________________________________________________________________________________________
  71.  
  72. public class List{
  73.  
  74.     private ListNode firstNode;
  75.     private ListNode lastNode;
  76.     private String name;
  77.  
  78.     public List() {
  79.         this("list");
  80.     }
  81.  
  82.     public List(String listName) {
  83.         name = listName;
  84.         firstNode = lastNode = null;
  85.     }
  86.  
  87.     public void getFirstLast(){
  88.         System.out.println();
  89.         System.out.println("First:"+firstNode.getObject().toString()+"/"+"Last"+lastNode.getObject().toString());
  90.     }
  91.     public void insertAtFront(Object insertItem) {
  92.         if (isEmpty())
  93.             firstNode = lastNode = new ListNode(insertItem);
  94.         else
  95.             firstNode = new ListNode(insertItem, firstNode);
  96.     }
  97.  
  98.     public void insertAtBack(Object insertItem) {
  99.         if (isEmpty())
  100.             firstNode = lastNode = new ListNode(insertItem);
  101.         else
  102.             lastNode = lastNode.nextNode = new ListNode(insertItem);
  103.     }
  104.  
  105.     public int ChackLength() {
  106.         ListNode Next = firstNode;
  107.         int cnt = 0;
  108.         while (Next.getNext() != null) {
  109.             cnt++;
  110.             Next = Next.getNext();
  111.         }
  112.         cnt++;
  113.         return cnt;
  114.     }
  115.  
  116.     public Object removeFromFront() throws EmptyListException {
  117.         if (isEmpty())
  118.             throw new EmptyListException(name);
  119.         Object removedItem = firstNode.getObject();
  120.  
  121.         if (firstNode == lastNode)
  122.             firstNode = lastNode = null;
  123.         else
  124.             firstNode = firstNode.getNext();
  125.         return removedItem;
  126.     }
  127.  
  128.     public Object removeFromBack() throws EmptyListException {
  129.         if (isEmpty())
  130.             throw new EmptyListException(name);
  131.  
  132.         Object removedItem = lastNode.getObject();
  133.         if (firstNode == lastNode)
  134.             firstNode = lastNode = null;
  135.         else {
  136.             ListNode current = firstNode;
  137.  
  138.             while (current.getNext() != lastNode)
  139.                 current = current.getNext();
  140.  
  141.             lastNode = current;
  142.             current.setNext(null);
  143.         }
  144.         return removedItem;
  145.     }
  146.  
  147.     public boolean isEmpty() {
  148.         return firstNode == null;
  149.     }
  150.  
  151.     public void print() {
  152.         if (isEmpty()) {
  153.             System.out.printf("Empty %s\n", name);
  154.             return;
  155.         }
  156.         System.out.printf("The %s is : ", name);
  157.         ListNode current = firstNode;
  158.  
  159.         while (current != null) {
  160.             System.out.printf("%s", current.getObject());
  161.             current = current.getNext();
  162.         }
  163.         System.out.println("\n");
  164.     }
  165.  
  166.     public String toString() {
  167.         String Ret = "(";
  168.         ListNode Next = firstNode;
  169.  
  170.         while (Next != null) {
  171.             if (Next.getNext() != null) {
  172.                 Ret += Next.getObject().toString() + " , ";
  173.  
  174.             } else
  175.                 Ret += Next.getObject().toString() + ")";
  176.             Next = Next.getNext();
  177.         }
  178.         return Ret;
  179.     }
  180.  
  181.     public Object removeAt(int k) {
  182.         int cnt = 0;
  183.         ListNode Next = null;
  184.         ListNode Removed = null;
  185.  
  186.         if (firstNode == null)
  187.             System.out.println("The list is empty");
  188.  
  189.         if (k <= 0) {
  190.             throw new ListIndexOutOfBound("Index have to be bigger than 0");
  191.  
  192.         } else if (k > ChackLength())
  193.             throw new ListIndexOutOfBound(
  194.                     "Index have to be smaller than list size");
  195.         if (k == 1) {
  196.             Removed = firstNode;
  197.             firstNode = firstNode.getNext();
  198.             return Removed.getObject();
  199.         }
  200.         for (int i = 0; i < k; i++) {
  201.  
  202.             if (cnt == k - 1) {
  203.                 if (k == ChackLength()) {
  204.                     ListNode a = lastNode;
  205.                     Next.SetNull();
  206.                     lastNode = Next;
  207.                     return a.getObject();
  208.                 }
  209.  
  210.                 Removed = Next.getNext();
  211.                 Next.setNext(Next.getNext().getNext());
  212.                 Removed.SetNull();
  213.                 return Removed.getObject();
  214.             } else if (i == 0) {
  215.                 Next = firstNode;
  216.                 cnt++;
  217.             } else {
  218.                 cnt++;
  219.                 Next = Next.getNext();
  220.             }
  221.         }
  222.         return Removed.getObject();
  223.     }
  224.  
  225.     public ListNode ReturnFirst() {
  226.         return firstNode;
  227.     }
  228.  
  229.     public void show() {
  230.         if (firstNode == null)
  231.             return;
  232.         else
  233.             firstNode.show();
  234.     }
  235.  
  236.     public void showRev() {
  237.         if (firstNode != null)
  238.             firstNode.showRev();
  239.  
  240.     }
  241.  
  242.     public void addAt( Object obj,int k) {
  243.         int cnt = 0;
  244.         ListNode Next = null;
  245.  
  246.         if (k <= 0) {
  247.             throw new ListIndexOutOfBound("Index have to be bigger than 0");
  248.  
  249.         } else if (k > ChackLength() + 1)
  250.             throw new ListIndexOutOfBound(
  251.                     "Index have to be smaller than list size");
  252.  
  253.         if (k == 1) {
  254.             ListNode Add = new ListNode(obj);
  255.             ListNode a=firstNode;
  256.             firstNode = Add;
  257.         Add.setNext(a);
  258.         return;
  259.         }
  260.  
  261.         if (k == ChackLength() + 1) {
  262.             ListNode Add = new ListNode(obj);
  263.             lastNode.setNext(Add);
  264.             lastNode = Add;
  265.         return;
  266.         }
  267.         for (int i = 0; i < k; i++) {
  268.             if (cnt == k - 1) {
  269.                 ListNode n;
  270.                 n = Next.getNext();
  271.                 ListNode Add = new ListNode(obj);
  272.                 Next.setNext(Add);
  273.                 Add.setNext(n);
  274.  
  275.             } else if (i == 0) {
  276.                 Next = firstNode;
  277.                 cnt++;
  278.             } else {
  279.                 cnt++;
  280.                 Next = Next.getNext();
  281.             }
  282.         }
  283.  
  284.     }
  285.  
  286.  
  287. public void addAtRec(Object obj,int k){
  288.     if (k <= 0) {
  289.         throw new ListIndexOutOfBound("Index have to be bigger than 0");
  290.  
  291.     } else if (k > ChackLength() + 1)
  292.         throw new ListIndexOutOfBound(
  293.                 "Index have to be smaller than list size");
  294.    
  295.         if(k==1){
  296.         ListNode a=firstNode;
  297.         ListNode Add=new ListNode(obj);
  298.         firstNode =Add;
  299.         Add.setNext(a);
  300.     return;
  301.     }
  302.    
  303.     if(k==ChackLength()+1){
  304.         ListNode a=lastNode;
  305.         ListNode Add=new ListNode(obj);
  306.         lastNode =Add;
  307.         a.setNext(Add);
  308.     return;
  309.     }
  310.    
  311.     if (firstNode==null)
  312.           return;
  313.         else
  314.           firstNode.addAtRec(k,obj);
  315.         }
  316.  
  317. public Object[] toArray(){
  318. Object[] Ret=new Object[ChackLength()];
  319.     ListNode Moving=firstNode;
  320.     int cnt=0;
  321.    
  322.     while(Moving.getNext()!=null){
  323.         Ret[cnt]=Moving.getObject();
  324.         Moving=Moving.getNext();
  325.         cnt++;
  326.     }
  327.     Ret[cnt]=Moving.getObject();
  328.     return Ret;
  329.    
  330. }
  331. public void ShowArray(Object[] a){
  332.     for(int i=0;i<a.length;i++){
  333.         System.out.print(a[i].toString());
  334.     }
  335. }
  336. public void setFirst(ListNode a){
  337.     firstNode=a;
  338. }
  339. public List clone() throws CloneNotSupportedException{
  340. //לעשות לולאה שתכנס כל פעם לאיבר הבא ותשתמש בקלון שלו ושיחזיר דאטא שעבר קלון, ואז להשתמש במה שהוחזר וליצור ליסטנוד חדש שיצביעו העליו האחד הקודם שיצרנו, עד שמסתיימת הרשימה.
  341. }
  342.  
  343. }
  344.  
  345.  
  346. ______________________________________________________________________________________________________________________________________
  347. ListIndexOutOfBoundsException זה Exception סטנדרטי
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement