簡體   English   中英

關於search()

[英]About the search()

import java.util.*;
public class ListStack extends LinkedList{


public ListStack() {    // <== constructor, different from ListStackComp.java
    super();
}

public boolean empty() {
   if(isEmpty()){
       return true;
   }else{
       return false;
   }
}

public Object push(Object item) {
    addToHead(item);
    return item;
}

public Object pop() {
        Object item = removeFromHead();
        return item;
}

public Object peek() { 
        Object item = get(0);
        return item;
}

public int search(Object item) {
    ListNode current = head;
    int num=-1;
    for(int i = 0;i<length;i++){
        if(item.equals(current.getData())){
            num = i;
        }
        else{
            current = current.getNext();
        }       
    }
    return num; 
}


}

結果是:
[789.123 E Patrick 123 Dog Cat BA]
peek()返回:789.123
帕特里克7歲
A在7
789.123是7
彼得在-1
能幫我解決問題嗎? search()是否有錯誤?

class ListNode {
    private Object data;
    private ListNode next;
    ListNode(Object o) { data = o; next = null; }
    ListNode(Object o, ListNode nextNode) 
        { data = o; next = nextNode; }
    public void setData(Object data){
        this.data = data;
    }
    public void setNext(ListNode next){
        this.next = next;
    }
    public Object getData() { return data; }
    public ListNode getNext() { return next; }
} // class ListNode

class EmptyListException extends RuntimeException {
    public EmptyListException () { super ("List is empty"); }
} // class EmptyListException

class LinkedList {

    protected ListNode head;   // <== chnage to protected for inheriting
    protected ListNode tail;      // <== change to protected for inheriting

    protected  int length;      // the length of the list   <== chnage to protected for inheriting

    public LinkedList() {
        head = tail = null;
        length = 0;
    }

    public boolean isEmpty() { return head == null; }

    public void addToHead(Object item) {
        if (isEmpty())
            head = tail = new ListNode(item);
        else
            head = new ListNode(item, head);
        length++;
    }

    public void addToTail(Object item) {
        if (isEmpty())
            head = tail = new ListNode(item);
        else {
            tail.setNext(new ListNode(item));
            tail = tail.getNext();
        }
        length++;
    }

    public Object removeFromHead() throws EmptyListException {
        Object item = null;
        if (isEmpty())
            throw new EmptyListException();
        item = head.getData();
        if (head == tail)
            head = tail = null;
        else
            head = head.getNext();
        length--;
        return item;
    }

    public Object removeFromTail() throws EmptyListException {
        Object item = null;
        if (isEmpty())
            throw new EmptyListException();
        item = tail.getData();
        if (head == tail)
            head = tail = null;
        else {
            ListNode current = head;
            while (current.getNext() != tail)
                current = current.getNext();
            tail = current;
            current.setNext(null);
        }
        length--;
        return item;
    }

    public String toString() {
        String str = "[ ";
        ListNode current = head;
        while (current != null) {
            str = str + current.getData() + " ";
            current = current.getNext();
        }
        return str + " ]";
    }

    public int count() {
        return length;
    }

    public Object remove(int n) {
        Object item = null;
        if (n <= length) { // make sure there is nth node to remove
            // special treatment for first and last nodes
            if (n == 1) return removeFromHead();
            if (n == length) return removeFromTail();
            // removal of nth node which has nodes in front and behind
            ListNode current = head;
            ListNode previous = null;
            for (int i = 1; i < n; i++) { // current will point to nth node
                previous = current;
                current = current.getNext();
            }
            // data to be returned
            item = current.getData();
            // remove the node by adjusting two pointers (object reference)
            previous.setNext(current.getNext());
        }
        length--;
        return item;
    }

    public void add(int n, Object item) {
        // special treatment for insert as first node
        if (n == 1) {
            addToHead(item);
            return;
        }
        // special treatment for insert as last node
        if (n > length) {
            addToTail(item);
            return;
        }
        // locate the n-1th node
        ListNode current = head;
        for (int i = 1; i < n-1; i++)   // current will point to n-1th node
            current = current.getNext();
        // create new node and insert at nth position
        current.setNext(new ListNode(item, current.getNext()));
        length++;
    }

    public Object get(int n) {
        // n is too big, no item can be returned
        if (length < n) return null;
        // locate the nth node
        ListNode current = head;
        for (int i = 1; i < n; i++)
            current = current.getNext();
        return current.getData();
    }

} // class LinkedList



   import java.util.Stack;
import java.util.Iterator;

public class TestStack {
    public static void main (String args[]) {               
        ListStack s = new ListStack();  
        System.out.println(s);

        System.out.println("Patrick is at " + s.search("Patrick"));

        s.push(new Character('A'));
        System.out.println(s);
        s.push(new Character('B'));
        System.out.println(s);
        s.push("Cat");
        System.out.println(s);
        s.push("Dog");
        System.out.println(s);
        s.push(new Integer(123));
        System.out.println(s);
        s.push("Patrick");
        System.out.println(s);
        s.push(new Character('E'));
        System.out.println(s);
        s.push(new Double(789.123));
        System.out.println(s);

        System.out.println("peek() returns: " + s.peek());

        System.out.println("Patrick is at " + s.search("Patrick"));
        System.out.println("A is at " + s.search(new Character('A')));
        System.out.println("789.123 is at " + s.search(new Double(789.123)));
        System.out.println("Peter is at " + s.search("Peter"));
        System.out.println();
    }

} // class TestStack

還有另一個LinkedList和Test文件的代碼

public int search(Object item) {
        ListNode current = head;
        int num=-1;
        for(int i = 0;i<length;i++){
            if(item.equals(current.getData())){
                return i;
            }
            else{
                current = current.getNext();
            }       
        }
        return num; 
    }

希望它會工作。

public Object peek() { 
        Object item = get(0);
        return item;
}


 public int search(Object item) {
            ListNode current = head;
            int num=-1;
            for(int i = 1;i<length;i++){
                if(item.equals(current.getData())){
                    num = i;
                    return num;
                }
                else{
                    current = current.getNext();
                }       
            }
            return num; 
        }

結果中出現了新問題:

[ AB Cat Dog 123 Patrick E 789.123 ]
peek()返回: A
Patrick is at 6
A is at 1
789.123 is at -1
Peter is at -1
為什么結果找不到789.123
我如何改進可以發現789.123的peek()方法?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM