Я пробовал этот точный код без изменений пару дней go, и у меня не было проблем, но теперь, по-видимому, без причин и без внесения изменений, я получаю эту ошибку:
Exception in thread "main" java.lang.NoSuchMethodError: Node: method 'void <init>()' not found
at List.InsertAfter(List.java:217)
at List.InsertBefore(List.java:186)
at Stack.Push(Stack.java:31)
at StackTest.main(StackTest.java:19)
Это файл стека
public class Stack<mT>
{
private List<mT> l;
// Stack constructor
public Stack()
{
this.l = new List<mT>();
}
// copy constructor
public Stack(Stack<mT> s)
{
this.l = new List<mT>(s.l);
l.First();
}
// inserts an item on top of the stack
public void Push(mT data)
{
l.First();
l.InsertBefore(data);
}
// deletes an item from the top of the stack (and returns it)
public mT Pop()
{
l.First();
l.GetValue();
l.Remove();
return l.GetValue();
}
// returns (but does not delete) the item on top of the stack
public mT Peek()
{
l.First();
return l.GetValue();
}
// returns the size of the stack
// size does not imply capacity
public int Size()
{
return this.l.GetSize();
}
// returns if the stack is empty
public boolean IsEmpty()
{
return l.IsEmpty();
}
// returns if the stack is full
public boolean IsFull()
{
return l.IsFull();
}
// returns if two stacks are equal (by value)
public boolean Equals(Stack<mT> s) //changed during class
{
return l.Equals(s.l);
}
// Concatenate two stacks
public Stack Add(Stack s)
{
this.l = this.l.Add(s.l);
return this;
}
// returns a string representation of the entire stack (e.g., 1 2 3 4 5)
// the string "NULL" should be returned for an empty stack
public String toString()
{
return l.toString();
}
}
А вот часть списка:
class Node <mT>
{
private mT data;
private Node<mT> link; //changed during class
// constructor
public Node()
{
this.data = null;
this.link = null;
}
// accessor and mutator for the data component
public mT getData()
{
return this.data;
}
public void setData(mT data) //changed during class
{
this.data = data;
}
// accessor and mutator for the link component
public Node<mT> getLink() //changed during class
{
return this.link;
}
public void setLink(Node<mT> link) //changed during class
{
this.link = link;
}
}
// the List class
public class List<mT> //changed during class
{
public static final int MAX_SIZE = 50;
private Node <mT> head;
private Node <mT> tail;
private Node <mT> curr;
private int num_items;
// constructor
// remember that an empty list has a "size" of -1 and its "position" is at -1
public List()
{
this.head = null;
this.tail = null;
this.curr = null;
this.num_items = 0;
}
// copy constructor
// clones the list l and sets the last element as the current
public List(List l)
{
Node<mT> n = l.head; //changed during class
this. head = this.tail = this.curr = null;
this.num_items = 0;
while(n != null)
{
this.InsertAfter(n.getData());
n = n.getLink();
}
}
// navigates to the beginning of the list
public void First()
{
this.curr = this.head;
}
// navigates to the end of the list
// the end of the list is at the last valid item in the list
public void Last()
{
this.curr = this.tail;
}
// navigates to the specified element (0-index)
// this should not be possible for an empty list
// this should not be possible for invalid positions
public void SetPos(int pos)
{
if(!this.IsEmpty() && pos >= 0 && pos < this.num_items)
{
// start at the head node
this.curr = this.head;
// move current to the specified position
for(int i = 0; i < pos; i++)
this.curr = this.curr.getLink();
}
}
// navigates to the previous element
// this should not be possible for an empty list
// there should be no wrap-around
public void Prev()
{
if(!this.IsEmpty() && this.curr != this.head)
{
Node<mT> n = this.head; //changed during class
// move node n to the previous
while(n.getLink() != this.curr)
n = n.getLink();
this.curr = n;
}
}
// navigates to the next element
// this should not be possible for an empty list
// there should be no wrap-around
public void Next()
{
if(!this.IsEmpty() && this.curr != this.tail)
{
this.curr = this.curr.getLink();
}
}
// returns the location of the current element (or -1)
public int GetPos()
{
//if list is empty, return -1
if(this.IsEmpty())
return -1;
//otherwise, start at the head node
Node<mT> n = this.head;
int i = 0;
//traverse the list to get to the curr pos, incremeenting i each time
while(n != this.curr)
{
n = n.getLink();
i++;
}
return i;
}
// returns the value of the current element (or -1)
public mT GetValue()
{
//if the list is empty, return null
if(this.IsEmpty())
return null;
// otherwise, return the data in the curr node
else
return curr.getData();
}
// returns the size of the list
// size does not imply capacity
public int GetSize()
{
return this.num_items;
}
// inserts an item before the current element
// the new element becomes the current
// this should not be possible for a full list
public void InsertBefore(mT data)
{
//we only add to the list if the list is not full
if(!this.IsFull())
{
if(this.IsEmpty())
this.InsertAfter(data);
else
{
//if curr is at the head, create a new head node that points to the curr one
if(this.curr == this.head)
{
this.head = new Node<mT>();
this.head.setData(data);
this.head.setLink(curr);
this.curr = this.head;
this.num_items++;
}
//otherwise, move to the prev node and insert after item
else
{
this.Prev();
this.InsertAfter(data);
}
}
}
}
// inserts an item after the current element
// the new element becomes the current
// this should not be possible for a full list
public void InsertAfter(mT data) //changed during class
{
if(!this.IsFull())
{
Node<mT> n = new Node<mT>(); //changed during class
n.setData(data);
// if the list is empty, head, tail, and curr point to node name
if(this.IsEmpty())
this.head = this.tail = this.curr = n;
else
{
//if at the end of the list, add the new node - this becomes the new tail node
if(this.curr == this.tail)
{
this.curr.setLink(n);
this.curr = this.tail = n;
}
//otherwise, change links to insert this node
else
{
n.setLink(this.curr.getLink());
this.curr.setLink(n);
this.curr = n;
}
}
this.num_items++;
}
}
// removes the current element (collapsing the list)
// this should not be possible for an empty list
public void Remove()
{
if(!this.IsEmpty())
{
// if at the head node, reset the head
if(this.curr == this.head)
{
this.head = this.curr = this.curr.getLink();
// if this was the only node in the list, set tail to null
if(this.head == null)
this.tail = null;
}
// otherwise, go back and change the link to reroute around the node to be removed
else
{
this.Prev();
this.curr.setLink(this.curr.getLink().getLink());
if(this.curr.getLink() == null)
this.tail = this.curr;
this.Next();
}
this.num_items--;
}
}
// replaces the value of the current element with the specified value
// this should not be possible for an empty list
public void Replace(mT data)
{
if(!this.IsEmpty())
curr.setData(data);
}
// returns if the list is empty
public boolean IsEmpty()
{
return (this.head == null);
}
// returns if the list is full
public boolean IsFull()
{
return (this.num_items == MAX_SIZE);
}
// returns if two lists are equal (by value)
public boolean Equals(List<mT> l) //changed during class
{
if(this.num_items != l.num_items)
return false;
Node<mT> p = this.head; //changed during class
Node<mT> q = l.head; //changed during class
//traverse each list comparing the data
while(p != null)
{
if(p.getData() != q.getData())
return false;
p = p.getLink();
q = q.getLink();
}
return true;
}
// returns the concatenation of two lists
// l should not be modified
// l should be concatenated to the end of *this
// the returned list should not exceed MAX_SIZE elements
// the last element of the new list is the current
public List <mT> Add(List <mT> l)
{
List<mT> t = new List<mT>(this); //changed during class
Node<mT> n = l.head;
//iterate through list l copying each element to the new list
while(n != null && !t.IsFull())
{
t.InsertAfter(n.getData());
n = n.getLink();
}
return t;
}
// returns a string representation of the entire list (e.g., 1 2 3 4 5)
// the string "NULL" should be returned for an empty list
public String toString()
{
// return "NULL" if the list is empty
if(this.IsEmpty())
return "NULL";
// otherwise go through the list and return each element
else
{
String s = "";
Node<mT> n = this.head;
while(n != null)
{
s += n.getData() + " ";
n = n.getLink();
}
return s;
}
}
}
Я не верю, что это что-то в файле StackTest. java, потому что я был не разрешено изменять его, поэтому я не считаю, что сейчас стоит публиковать, если кто-нибудь может сообщить мне, почему произошла ошибка, это было бы очень признательно. Напоминаем, что этот точный код работал отлично, без проблем всего пару дней go, а теперь проблема возникла, казалось бы, из ниоткуда