Not a member of Pastebin yet?
                        Sign Up,
                        it unlocks many cool features!                    
                - public class ListNode implements Cloneable{
 - Object data;
 - ListNode nextNode;
 - public ListNode(Object o) {
 - this(o, null);
 - }
 - public ListNode(Object o, ListNode node) {
 - data = o;
 - nextNode = node;
 - }
 - public Object getObject() {
 - return data;
 - }
 - public ListNode getNext() {
 - return nextNode;
 - }
 - public void SetNull() {
 - nextNode = null;
 - }
 - public void setNext(ListNode l) {
 - nextNode = l;
 - }
 - public void show() {
 - if (nextNode == null)
 - System.out.print(getObject().toString());
 - else {
 - System.out.print(getObject().toString());
 - nextNode.show();
 - }
 - }
 - public void showRev() {
 - if (nextNode == null)
 - System.out.print(getObject().toString());
 - else {
 - nextNode.showRev();
 - System.out.print(getObject().toString());
 - }
 - }
 - public void addAtRec(int k, Object obj) {
 - if (k == 2) {
 - ListNode Add = new ListNode(obj);
 - Add.setNext(getNext());
 - this.setNext(Add);
 - } else
 - if(nextNode!=null)
 - nextNode.addAtRec(k - 1, obj);
 - }
 - public Object clone() throws CloneNotSupportedException{
 - //if(data instanceof Cloneable){ זה לא נותן לי לעשות קלון לדאטא, נותן לי
 - //Cloneable Ret=(Cloneable) data; את השגיאה שאני יצרף בפוסט
 - //return data.clone();
 - }
 - }
 - }
 - _______________________________________________________________________________________________________________________________________
 - public class List{
 - private ListNode firstNode;
 - private ListNode lastNode;
 - private String name;
 - public List() {
 - this("list");
 - }
 - public List(String listName) {
 - name = listName;
 - firstNode = lastNode = null;
 - }
 - public void getFirstLast(){
 - System.out.println();
 - System.out.println("First:"+firstNode.getObject().toString()+"/"+"Last"+lastNode.getObject().toString());
 - }
 - public void insertAtFront(Object insertItem) {
 - if (isEmpty())
 - firstNode = lastNode = new ListNode(insertItem);
 - else
 - firstNode = new ListNode(insertItem, firstNode);
 - }
 - public void insertAtBack(Object insertItem) {
 - if (isEmpty())
 - firstNode = lastNode = new ListNode(insertItem);
 - else
 - lastNode = lastNode.nextNode = new ListNode(insertItem);
 - }
 - public int ChackLength() {
 - ListNode Next = firstNode;
 - int cnt = 0;
 - while (Next.getNext() != null) {
 - cnt++;
 - Next = Next.getNext();
 - }
 - cnt++;
 - return cnt;
 - }
 - public Object removeFromFront() throws EmptyListException {
 - if (isEmpty())
 - throw new EmptyListException(name);
 - Object removedItem = firstNode.getObject();
 - if (firstNode == lastNode)
 - firstNode = lastNode = null;
 - else
 - firstNode = firstNode.getNext();
 - return removedItem;
 - }
 - public Object removeFromBack() throws EmptyListException {
 - if (isEmpty())
 - throw new EmptyListException(name);
 - Object removedItem = lastNode.getObject();
 - if (firstNode == lastNode)
 - firstNode = lastNode = null;
 - else {
 - ListNode current = firstNode;
 - while (current.getNext() != lastNode)
 - current = current.getNext();
 - lastNode = current;
 - current.setNext(null);
 - }
 - return removedItem;
 - }
 - public boolean isEmpty() {
 - return firstNode == null;
 - }
 - public void print() {
 - if (isEmpty()) {
 - System.out.printf("Empty %s\n", name);
 - return;
 - }
 - System.out.printf("The %s is : ", name);
 - ListNode current = firstNode;
 - while (current != null) {
 - System.out.printf("%s", current.getObject());
 - current = current.getNext();
 - }
 - System.out.println("\n");
 - }
 - public String toString() {
 - String Ret = "(";
 - ListNode Next = firstNode;
 - while (Next != null) {
 - if (Next.getNext() != null) {
 - Ret += Next.getObject().toString() + " , ";
 - } else
 - Ret += Next.getObject().toString() + ")";
 - Next = Next.getNext();
 - }
 - return Ret;
 - }
 - public Object removeAt(int k) {
 - int cnt = 0;
 - ListNode Next = null;
 - ListNode Removed = null;
 - if (firstNode == null)
 - System.out.println("The list is empty");
 - if (k <= 0) {
 - throw new ListIndexOutOfBound("Index have to be bigger than 0");
 - } else if (k > ChackLength())
 - throw new ListIndexOutOfBound(
 - "Index have to be smaller than list size");
 - if (k == 1) {
 - Removed = firstNode;
 - firstNode = firstNode.getNext();
 - return Removed.getObject();
 - }
 - for (int i = 0; i < k; i++) {
 - if (cnt == k - 1) {
 - if (k == ChackLength()) {
 - ListNode a = lastNode;
 - Next.SetNull();
 - lastNode = Next;
 - return a.getObject();
 - }
 - Removed = Next.getNext();
 - Next.setNext(Next.getNext().getNext());
 - Removed.SetNull();
 - return Removed.getObject();
 - } else if (i == 0) {
 - Next = firstNode;
 - cnt++;
 - } else {
 - cnt++;
 - Next = Next.getNext();
 - }
 - }
 - return Removed.getObject();
 - }
 - public ListNode ReturnFirst() {
 - return firstNode;
 - }
 - public void show() {
 - if (firstNode == null)
 - return;
 - else
 - firstNode.show();
 - }
 - public void showRev() {
 - if (firstNode != null)
 - firstNode.showRev();
 - }
 - public void addAt( Object obj,int k) {
 - int cnt = 0;
 - ListNode Next = null;
 - if (k <= 0) {
 - throw new ListIndexOutOfBound("Index have to be bigger than 0");
 - } else if (k > ChackLength() + 1)
 - throw new ListIndexOutOfBound(
 - "Index have to be smaller than list size");
 - if (k == 1) {
 - ListNode Add = new ListNode(obj);
 - ListNode a=firstNode;
 - firstNode = Add;
 - Add.setNext(a);
 - return;
 - }
 - if (k == ChackLength() + 1) {
 - ListNode Add = new ListNode(obj);
 - lastNode.setNext(Add);
 - lastNode = Add;
 - return;
 - }
 - for (int i = 0; i < k; i++) {
 - if (cnt == k - 1) {
 - ListNode n;
 - n = Next.getNext();
 - ListNode Add = new ListNode(obj);
 - Next.setNext(Add);
 - Add.setNext(n);
 - } else if (i == 0) {
 - Next = firstNode;
 - cnt++;
 - } else {
 - cnt++;
 - Next = Next.getNext();
 - }
 - }
 - }
 - public void addAtRec(Object obj,int k){
 - if (k <= 0) {
 - throw new ListIndexOutOfBound("Index have to be bigger than 0");
 - } else if (k > ChackLength() + 1)
 - throw new ListIndexOutOfBound(
 - "Index have to be smaller than list size");
 - if(k==1){
 - ListNode a=firstNode;
 - ListNode Add=new ListNode(obj);
 - firstNode =Add;
 - Add.setNext(a);
 - return;
 - }
 - if(k==ChackLength()+1){
 - ListNode a=lastNode;
 - ListNode Add=new ListNode(obj);
 - lastNode =Add;
 - a.setNext(Add);
 - return;
 - }
 - if (firstNode==null)
 - return;
 - else
 - firstNode.addAtRec(k,obj);
 - }
 - public Object[] toArray(){
 - Object[] Ret=new Object[ChackLength()];
 - ListNode Moving=firstNode;
 - int cnt=0;
 - while(Moving.getNext()!=null){
 - Ret[cnt]=Moving.getObject();
 - Moving=Moving.getNext();
 - cnt++;
 - }
 - Ret[cnt]=Moving.getObject();
 - return Ret;
 - }
 - public void ShowArray(Object[] a){
 - for(int i=0;i<a.length;i++){
 - System.out.print(a[i].toString());
 - }
 - }
 - public void setFirst(ListNode a){
 - firstNode=a;
 - }
 - public List clone() throws CloneNotSupportedException{
 - //לעשות לולאה שתכנס כל פעם לאיבר הבא ותשתמש בקלון שלו ושיחזיר דאטא שעבר קלון, ואז להשתמש במה שהוחזר וליצור ליסטנוד חדש שיצביעו העליו האחד הקודם שיצרנו, עד שמסתיימת הרשימה.
 - }
 - }
 - ______________________________________________________________________________________________________________________________________
 - ListIndexOutOfBoundsException זה Exception סטנדרטי
 
Advertisement
 
                    Add Comment                
                
                        Please, Sign In to add comment