Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/search/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 为使用节点的堆栈创建一个int搜索(objecto)方法_Java_Search_Linked List_Stack_Nodes - Fatal编程技术网

Java 为使用节点的堆栈创建一个int搜索(objecto)方法

Java 为使用节点的堆栈创建一个int搜索(objecto)方法,java,search,linked-list,stack,nodes,Java,Search,Linked List,Stack,Nodes,我试图创建一个使用通用节点类的通用堆栈和队列类。它有empty(),pop(),peek(),push(),以及search()方法。我知道有一个内置的堆栈类和堆栈搜索方法,但我们必须使用节点类来实现它 我不确定如何进行搜索方法。search方法应该返回距引用堆栈顶部最近的距离。最上面的项目被认为位于距离1处;下一项位于距离2处;等等 我的课程如下: import java.io.*; import java.util.*; public class MyStack<E> imp

我试图创建一个使用通用节点类的通用堆栈和队列类。它有
empty()
pop()
peek()
push()
,以及
search()
方法。我知道有一个内置的
堆栈
类和堆栈
搜索
方法,但我们必须使用
节点
类来实现它

我不确定如何进行
搜索
方法。
search
方法应该返回距引用堆栈顶部最近的距离。最上面的项目被认为位于距离1处;下一项位于距离2处;等等

我的课程如下:

import java.io.*; 
import java.util.*;

public class MyStack<E> implements StackInterface<E>
{
    private Node<E> head;
    private int nodeCount;

    public static void main(String args[]) {
    }
    
    public E peek() {
        return this.head.getData();
    }
   
    public E pop() {
        E item;
        item = head.getData();
        head = head.getNext();
        nodeCount--;
        return item;
    }
    
    public boolean empty() {
        if (head==null) {
            return true; 
        } else {
            return false;
        }
    }
   
    public void push(E data) {
        Node<E> head = new Node<E>(data);
        nodeCount++;
    }
   
    public int search(Object o) {
        // todo
    }
}

public class Node<E>
{
    E data;
    Node<E> next;
    // getters and setters  
    public Node(E data) 
    { 
        this.data = data; 
        this.next = null; 
    } 
    public E getData() {
        return data;
    }
    public void setData(E data) {
        this.data = data;
    }
    public Node<E> getNext() {
        return next;
    }
    public void setNext(Node<E> next) {
        this.next = next;
    }
}

public class MyQueue<E> implements QueueInterface<E>
{
    private Node<E> head;
    private int nodeCount;
    Node<E> rear;
    public MyQueue() 
    { 
        this.head = this.rear = null; 
    } 
    
    public void add(E item){
        Node<E> temp = new Node<E>(item);
        if (this.rear == null) { 
            this.head = this.rear = temp; 
            return; 
        }  
        this.rear.next = temp; 
        this.rear = temp; 
    }
    
    public E peek(){
        return this.head.getData();
    }
    
    public E remove(){ 
        E element = head.getData();
        Node<E> temp = this.head;
        this.head = this.head.getNext();
        nodeCount--;
        return element;
    }
}
import java.io.*;
导入java.util.*;
公共类MyStack实现StackInterface
{
专用节点头;
私有int节点计数;
公共静态void main(字符串参数[]){
}
公共E peek(){
返回此.head.getData();
}
公共E-pop(){
E项目;
item=head.getData();
head=head.getNext();
节点计数--;
退货项目;
}
公共布尔空(){
if(head==null){
返回true;
}否则{
返回false;
}
}
公共无效推送(E数据){
节点头=新节点(数据);
nodeCount++;
}
公共整数搜索(对象o){
//待办事项
}
}
公共类节点
{
E数据;
节点下一步;
//接球手和接球手
公共节点(E数据)
{ 
这个数据=数据;
this.next=null;
} 
公共E getData(){
返回数据;
}
公共无效设置数据(E数据){
这个数据=数据;
}
公共节点getNext(){
下一步返回;
}
公共void setNext(节点next){
this.next=next;
}
}
公共类MyQueue实现QueueInterface
{
专用节点头;
私有int节点计数;
节点后方;
公共MyQueue()
{ 
this.head=this.rear=null;
} 
公共作废添加(E项){
节点温度=新节点(项目);
如果(this.rear==null){
this.head=this.rear=温度;
返回;
}  
this.rear.next=温度;
这个。后=温度;
}
公共E peek(){
返回此.head.getData();
}
公共E remove(){
E元素=head.getData();
节点温度=this.head;
this.head=this.head.getNext();
节点计数--;
返回元素;
}
}
在根据第一条评论进行研究后,我有以下几点:

public int search(Object o){
    int count=0;
    Node<E> current = new Node<E> (head.getData());
    while(current.getData() != o){
        current.getNext();
        count++;
    }
    
    return count;
}
公共整数搜索(对象o){
整数计数=0;
当前节点=新节点(head.getData());
while(current.getData()!=o){
current.getNext();
计数++;
}
返回计数;
}

它没有任何错误,但我无法判断它是否真的正常工作。这似乎正确吗?

它需要以下改进

  • 搜索方法应具有类型为“E”的参数。因此,签名应该看起来像
    public int search(E元素)
  • 以1而不是0开始计数。如您所述,最顶端的项目被认为位于距离1处
  • 使用head初始化current,因为创建一个数据值为head的新节点(new node(head.getData())将创建一个数据与head节点相同的独立节点;而while将只对head节点运行,因为current.getNext()将始终为null<代码>节点当前=头部将创建另一个指向头部的参考变量
  • 而不是=在条件中,如果(!current.getData().equals(element.getData()))
  • 如果使用自己的类作为数据类型,不要忘记重写equals方法
  • 更改
    current.getNext()
    to
    current=current.getNext()

  • 你有其他方法的问题。注意
    top==null
    。要计算
    search()
    只需迭代元素并找到所需值的位置:

    public class MyStack<E> {
    
        private Node<E> top;
        private int size;
    
        public void push(E val) {
            Node<E> node = new Node<>(val);
            node.next = top;
            top = node;
            size++;
        }
    
        public E element() {
            return top == null ? null : top.val;
        }
    
        public E pop() {
            if (top == null)
                return null;
    
            E val = top.val;
            top = top.next;
            size--;
            return val;
        }
    
        public boolean empty() {
            return size == 0;
        }
    
        public int search(E val) {
            int res = 1;
            Node<E> node = top;
    
            while (node != null && node.val != val) {
                node = node.next;
                res++;
            }
    
            return node == null ? -1 : res;
        }
    
        private static final class Node<E> {
    
            private final E val;
            private Node<E> next;
    
            public Node(E val) {
                this.val = val;
            }
    
        }
    }
    
    公共类MyStack{
    私有节点顶部;
    私有整数大小;
    公共无效推送(E val){
    节点=新节点(val);
    node.next=top;
    顶部=节点;
    大小++;
    }
    公共E元素(){
    返回top==null?null:top.val;
    }
    公共E-pop(){
    if(top==null)
    返回null;
    E val=top.val;
    top=top.next;
    大小--;
    返回val;
    }
    公共布尔空(){
    返回大小==0;
    }
    公共整数搜索(E val){
    int res=1;
    节点=顶部;
    while(node!=null&&node.val!=val){
    node=node.next;
    res++;
    }
    返回节点==null?-1:res;
    }
    私有静态最终类节点{
    私人终审法院;
    私有节点下一步;
    公共节点(E val){
    this.val=val;
    }
    }
    }
    
    我假设您的
    MyStack
    类应该与您在问题中提到的Java提供的
    堆栈
    类兼容。这意味着您的签名
    public int search(Object o)
    与的签名匹配(除了
    synchronized

    要使用
    节点
    类实现
    搜索
    方法,我们需要遍历堆栈并返回第一个(最上面的)匹配的索引。首先,将
    head
    分配给一个局部变量(
    current
    )。然后,您可以创建一个循环,在该循环的末尾使用
    current.getNext()
    获取下一个元素。如果下一个元素为
    null
    ,则停止,因为我们已经到达堆栈的末尾。在循环中,可以对
    索引进行计数,也可以在当前元素的数据与
    
    public int search(Object o) {
        int index = 1;
        Node<E> current = head;
        while (current != null) {
            if (o == null) {
                if (current.getData() == null) {
                    return index;
                }
            } else {
                if (o.equals(current.getData())) {
                    return index;
                }
            }
            current = current.getNext();
            index++;
        }
        return -1; // nothing found
    }
    
    @Test
    public void testMyStackSearch() {
        // initialize
        final MyStack<String> stack = new MyStack<>();
        stack.push("e5");
        stack.push("e4");
        stack.push(null);
        stack.push("e2");
        stack.push("e1");
    
        // test (explicitly creating a new String instance)
        assertEquals(5, stack.search(new String("e5")));
        assertEquals(3, stack.search(null));
        assertEquals(2, stack.search(new String("e2")));
        assertEquals(1, stack.search(new String("e1")));
        assertEquals(-1, stack.search("X"));
    }