Java 如何将此链表存储在对象数组中?
我有一个叫做排序的类,它从用户那里获取一组整数,将它们存储在一个链表中,然后检查它们是否以递增的顺序存储 现在,它能够接收一个整数列表并存储它,然后检查它是否已排序。我需要能够存储两个单独的列表并同时检查它们,但我不确定如何存储它们 我假设我应该将其存储在一个对象数组中,但是如何将Java 如何将此链表存储在对象数组中?,java,linked-list,Java,Linked List,我有一个叫做排序的类,它从用户那里获取一组整数,将它们存储在一个链表中,然后检查它们是否以递增的顺序存储 现在,它能够接收一个整数列表并存储它,然后检查它是否已排序。我需要能够存储两个单独的列表并同时检查它们,但我不确定如何存储它们 我假设我应该将其存储在一个对象数组中,但是如何将IntNode类对象存储在存储数组中呢 这是排序类: import java.util.Scanner; public class Sorting { public static IntNode[] sto
IntNode
类对象存储在存储数组中呢
这是排序类:
import java.util.Scanner;
public class Sorting {
public static IntNode[] storage = new IntNode[2];
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
IntNode head;
head = new IntNode(0, null);
int headCounter = 0;
int searchNumber = 0;
int nextInt;
int storageNumber = 0;
System.out.println("Please enter a seqeunce of integer numbers. When you are finished, enter a negative number");
while (input.hasNextInt()) {
nextInt = input.nextInt();
if (nextInt >= 0) {
if (headCounter == 0) {
head = new IntNode(nextInt, null);
searchNumber = nextInt;
headCounter++;
storageNumber++;
} else {
IntNode selection = IntNode.listSearch(head, searchNumber);
selection.addNodeAfter(nextInt);
searchNumber = nextInt;
storageNumber++;
}
} else {
headCounter = 0;
}
}
for (IntNode cursor = head; cursor != null; cursor = cursor.getLink())
System.out.print(cursor.getData() + " ");
System.out.println(isSorted(head));
}
// Checks the linked list to see if the integers are sorted in increasing order
// @param IntNode head
// The head of the linked list being checked
// @return
// The method returns true if the linked list is sorted in increasing order, and false if the
// list is not sorted in an increasing order
public static boolean isSorted(IntNode head) {
int numCheckPrevious = 0;
for (IntNode cursor = head; cursor != null; cursor = cursor.getLink()) {
if (cursor.getData() > numCheckPrevious) {
numCheckPrevious = cursor.getData();
continue;
} else {
return false;
}
}
return true;
}
}
// File: IntNode.java
/******************************************************************************
* An IntNode provides a node for a linked list with
* integer data in each node.
*
* @note
* Lists of nodes can be made of any length, limited only by the amount of
* free memory in the heap. But beyond Integer.MAX_VALUE (2,147,483,647),
* the answer from listLength is incorrect because of arithmetic
* overflow.
******************************************************************************/
public class IntNode
{
// Invariant of the IntNode class:
// 1. The node's integer data is in the instance variable data.
// 2. For the final node of a list, the link part is null.
// Otherwise, the link part is a reference to the
// next node of the list.
private int data;
private IntNode link;
/**
* Initialize a node with a specified initial data and link to the next
* node. Note that the initialLink may be the null reference,
* which indicates that the new node has nothing after it.
* @param initialData
* the initial data of this new node
* @param initialLink
* a reference to the node after this new node--this reference may be null
* to indicate that there is no node after this new node.
* @postcondition
* This node contains the specified data and link to the next node.
**/
public IntNode(int initialData, IntNode initialLink)
{
data = initialData;
link = initialLink;
}
/**
* Modification method to add a new node after this node.
* @param item
* the data to place in the new node
* @postcondition
* A new node has been created and placed after this node.
* The data for the new node is item. Any other nodes
* that used to be after this node are now after the new node.
* @exception OutOfMemoryError
* Indicates that there is insufficient memory for a new
* IntNode.
**/
public void addNodeAfter(int item)
{
link = new IntNode(item, link);
}
/**
* Accessor method to get the data from this node.
* @param - none
* @return
* the data from this node
**/
public int getData( )
{
return data;
}
/**
* Accessor method to get a reference to the next node after this node.
* @param - none
* @return
* a reference to the node after this node (or the null reference if there
* is nothing after this node)
**/
public IntNode getLink( )
{
return link;
}
/**
* Copy a list.
* @param source
* the head of a linked list that will be copied (which may be
* an empty list in where source is null)
* @return
* The method has made a copy of the linked list starting at
* source. The return value is the head reference for the
* copy.
* @exception OutOfMemoryError
* Indicates that there is insufficient memory for the new list.
**/
public static IntNode listCopy(IntNode source)
{
IntNode copyHead;
IntNode copyTail;
// Handle the special case of the empty list.
if (source == null)
return null;
// Make the first node for the newly created list.
copyHead = new IntNode(source.data, null);
copyTail = copyHead;
// Make the rest of the nodes for the newly created list.
while (source.link != null)
{
source = source.link;
copyTail.addNodeAfter(source.data);
copyTail = copyTail.link;
}
// Return the head reference for the new list.
return copyHead; // * This node contains the specified data and link to the next node.
}
/**
* Copy a list, returning both a head and tail reference for the copy.
* @param source
* the head of a linked list that will be copied (which may be
* an empty list in where source is null)
* @return
* The method has made a copy of the linked list starting at
* source. The return value is an
* array where the [0] element is a head reference for the copy and the [1]
* element is a tail reference for the copy.
* @exception OutOfMemoryError
* Indicates that there is insufficient memory for the new list.
**/
public static IntNode[ ] listCopyWithTail(IntNode source)
{
IntNode copyHead;
IntNode copyTail;
IntNode[ ] answer = new IntNode[2];
// Handle the special case of the empty list.
if (source == null)
return answer; // The answer has two null references .
// Make the first node for the newly created list.
copyHead = new IntNode(source.data, null);
copyTail = copyHead;
// Make the rest of the nodes for the newly created list.
while (source.link != null)
{
source = source.link;
copyTail.addNodeAfter(source.data);
copyTail = copyTail.link;
}
// Return the head and tail references.
answer[0] = copyHead;
answer[1] = copyTail;
return answer;
}
/**
* Compute the number of nodes in a linked list.
* @param head
* the head reference for a linked list (which may be an empty list
* with a null head)
* @return
* the number of nodes in the list with the given head
* @note
* A wrong answer occurs for lists longer than Int.MAX_VALUE.
**/
public static int listLength(IntNode head)
{
IntNode cursor;
int answer;
answer = 0;
for (cursor = head; cursor != null; cursor = cursor.link)
answer++;
return answer;
}
/**
* Copy part of a list, providing a head and tail reference for the new copy.
* @param start/end
* references to two nodes of a linked list
* @param copyHead/copyTail
* the method sets these to refer to the head and tail node of the new
* list that is created
* @precondition
* start and end are non-null references to nodes
* on the same linked list,
* with the start node at or before the end node.
* @return
* The method has made a copy of the part of a linked list, from the
* specified start node to the specified end node. The return value is an
* array where the [0] component is a head reference for the copy and the
* [1] component is a tail reference for the copy.
* @exception IllegalArgumentException
* Indicates that start and end are not references
* to nodes on the same list.
* @exception NullPointerException
* Indicates that start is null.
* @exception OutOfMemoryError
* Indicates that there is insufficient memory for the new list.
**/
public static IntNode[ ] listPart(IntNode start, IntNode end)
{
IntNode copyHead;
IntNode copyTail;
IntNode cursor;
IntNode[ ] answer = new IntNode[2];
// Make the first node for the newly created list. Notice that this will
// cause a NullPointerException if start is null.
copyHead = new IntNode(start.data, null);
copyTail = copyHead;
cursor = start;
// Make the rest of the nodes for the newly created list.
while (cursor != end)
{
cursor = cursor.link;
if (cursor == null)
throw new IllegalArgumentException
("end node was not found on the list");
copyTail.addNodeAfter(cursor.data);
copyTail = copyTail.link;
}
// Return the head and tail references
answer[0] = copyHead;
answer[1] = copyTail;
return answer;
}
/**
* Find a node at a specified position in a linked list.
* @param head
* the head reference for a linked list (which may be an empty list in
* which case the head is null)
* @param position
* a node number
* @precondition
* position > 0.
* @return
* The return value is a reference to the node at the specified position in
* the list. (The head node is position 1, the next node is position 2, and
* so on.) If there is no such position (because the list is too short),
* then the null reference is returned.
* @exception IllegalArgumentException
* Indicates that position is not positive.
**/
public static IntNode listPosition(IntNode head, int position)
{
IntNode cursor;
int i;
if (position <= 0)
throw new IllegalArgumentException("position is not positive");
cursor = head;
for (i = 1; (i < position) && (cursor != null); i++)
cursor = cursor.link;
return cursor;
}
/**
* Search for a particular piece of data in a linked list.
* @param head
* the head reference for a linked list (which may be an empty list in
* which case the head is null)
* @param target
* a piece of data to search for
* @return
* The return value is a reference to the first node that contains the
* specified target. If there is no such node, the null reference is
* returned.
**/
public static IntNode listSearch(IntNode head, int target)
{
IntNode cursor;
for (cursor = head; cursor != null; cursor = cursor.link)
if (target == cursor.data)
return cursor;
return null;
}
/**
* Modification method to remove the node after this node.
* @param - none
* @precondition
* This node must not be the tail node of the list.
* @postcondition
* The node after this node has been removed from the linked list.
* If there were further nodes after that one, they are still
* present on the list.
* @exception NullPointerException
* Indicates that this was the tail node of the list, so there is nothing
* after it to remove.
**/
public void removeNodeAfter( )
{
link = link.link;
}
/**
* Modification method to set the data in this node.
* @param newData
* the new data to place in this node
* @postcondition
* The data of this node has been set to newData.
**/
public void setData(int newData)
{
data = newData;
}
/**
* Modification method to set the link to the next node after this node.
* @param newLink
* a reference to the node that should appear after this node in the linked
* list (or the null reference if there is no node after this node)
* @postcondition
* The link to the node after this node has been set to newLink.
* Any other node (that used to be in this link) is no longer connected to
* this node.
**/
public void setLink(IntNode newLink)
{
link = newLink;
}
}
下面是在链表中存储整数的IntNode类:
import java.util.Scanner;
public class Sorting {
public static IntNode[] storage = new IntNode[2];
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
IntNode head;
head = new IntNode(0, null);
int headCounter = 0;
int searchNumber = 0;
int nextInt;
int storageNumber = 0;
System.out.println("Please enter a seqeunce of integer numbers. When you are finished, enter a negative number");
while (input.hasNextInt()) {
nextInt = input.nextInt();
if (nextInt >= 0) {
if (headCounter == 0) {
head = new IntNode(nextInt, null);
searchNumber = nextInt;
headCounter++;
storageNumber++;
} else {
IntNode selection = IntNode.listSearch(head, searchNumber);
selection.addNodeAfter(nextInt);
searchNumber = nextInt;
storageNumber++;
}
} else {
headCounter = 0;
}
}
for (IntNode cursor = head; cursor != null; cursor = cursor.getLink())
System.out.print(cursor.getData() + " ");
System.out.println(isSorted(head));
}
// Checks the linked list to see if the integers are sorted in increasing order
// @param IntNode head
// The head of the linked list being checked
// @return
// The method returns true if the linked list is sorted in increasing order, and false if the
// list is not sorted in an increasing order
public static boolean isSorted(IntNode head) {
int numCheckPrevious = 0;
for (IntNode cursor = head; cursor != null; cursor = cursor.getLink()) {
if (cursor.getData() > numCheckPrevious) {
numCheckPrevious = cursor.getData();
continue;
} else {
return false;
}
}
return true;
}
}
// File: IntNode.java
/******************************************************************************
* An IntNode provides a node for a linked list with
* integer data in each node.
*
* @note
* Lists of nodes can be made of any length, limited only by the amount of
* free memory in the heap. But beyond Integer.MAX_VALUE (2,147,483,647),
* the answer from listLength is incorrect because of arithmetic
* overflow.
******************************************************************************/
public class IntNode
{
// Invariant of the IntNode class:
// 1. The node's integer data is in the instance variable data.
// 2. For the final node of a list, the link part is null.
// Otherwise, the link part is a reference to the
// next node of the list.
private int data;
private IntNode link;
/**
* Initialize a node with a specified initial data and link to the next
* node. Note that the initialLink may be the null reference,
* which indicates that the new node has nothing after it.
* @param initialData
* the initial data of this new node
* @param initialLink
* a reference to the node after this new node--this reference may be null
* to indicate that there is no node after this new node.
* @postcondition
* This node contains the specified data and link to the next node.
**/
public IntNode(int initialData, IntNode initialLink)
{
data = initialData;
link = initialLink;
}
/**
* Modification method to add a new node after this node.
* @param item
* the data to place in the new node
* @postcondition
* A new node has been created and placed after this node.
* The data for the new node is item. Any other nodes
* that used to be after this node are now after the new node.
* @exception OutOfMemoryError
* Indicates that there is insufficient memory for a new
* IntNode.
**/
public void addNodeAfter(int item)
{
link = new IntNode(item, link);
}
/**
* Accessor method to get the data from this node.
* @param - none
* @return
* the data from this node
**/
public int getData( )
{
return data;
}
/**
* Accessor method to get a reference to the next node after this node.
* @param - none
* @return
* a reference to the node after this node (or the null reference if there
* is nothing after this node)
**/
public IntNode getLink( )
{
return link;
}
/**
* Copy a list.
* @param source
* the head of a linked list that will be copied (which may be
* an empty list in where source is null)
* @return
* The method has made a copy of the linked list starting at
* source. The return value is the head reference for the
* copy.
* @exception OutOfMemoryError
* Indicates that there is insufficient memory for the new list.
**/
public static IntNode listCopy(IntNode source)
{
IntNode copyHead;
IntNode copyTail;
// Handle the special case of the empty list.
if (source == null)
return null;
// Make the first node for the newly created list.
copyHead = new IntNode(source.data, null);
copyTail = copyHead;
// Make the rest of the nodes for the newly created list.
while (source.link != null)
{
source = source.link;
copyTail.addNodeAfter(source.data);
copyTail = copyTail.link;
}
// Return the head reference for the new list.
return copyHead; // * This node contains the specified data and link to the next node.
}
/**
* Copy a list, returning both a head and tail reference for the copy.
* @param source
* the head of a linked list that will be copied (which may be
* an empty list in where source is null)
* @return
* The method has made a copy of the linked list starting at
* source. The return value is an
* array where the [0] element is a head reference for the copy and the [1]
* element is a tail reference for the copy.
* @exception OutOfMemoryError
* Indicates that there is insufficient memory for the new list.
**/
public static IntNode[ ] listCopyWithTail(IntNode source)
{
IntNode copyHead;
IntNode copyTail;
IntNode[ ] answer = new IntNode[2];
// Handle the special case of the empty list.
if (source == null)
return answer; // The answer has two null references .
// Make the first node for the newly created list.
copyHead = new IntNode(source.data, null);
copyTail = copyHead;
// Make the rest of the nodes for the newly created list.
while (source.link != null)
{
source = source.link;
copyTail.addNodeAfter(source.data);
copyTail = copyTail.link;
}
// Return the head and tail references.
answer[0] = copyHead;
answer[1] = copyTail;
return answer;
}
/**
* Compute the number of nodes in a linked list.
* @param head
* the head reference for a linked list (which may be an empty list
* with a null head)
* @return
* the number of nodes in the list with the given head
* @note
* A wrong answer occurs for lists longer than Int.MAX_VALUE.
**/
public static int listLength(IntNode head)
{
IntNode cursor;
int answer;
answer = 0;
for (cursor = head; cursor != null; cursor = cursor.link)
answer++;
return answer;
}
/**
* Copy part of a list, providing a head and tail reference for the new copy.
* @param start/end
* references to two nodes of a linked list
* @param copyHead/copyTail
* the method sets these to refer to the head and tail node of the new
* list that is created
* @precondition
* start and end are non-null references to nodes
* on the same linked list,
* with the start node at or before the end node.
* @return
* The method has made a copy of the part of a linked list, from the
* specified start node to the specified end node. The return value is an
* array where the [0] component is a head reference for the copy and the
* [1] component is a tail reference for the copy.
* @exception IllegalArgumentException
* Indicates that start and end are not references
* to nodes on the same list.
* @exception NullPointerException
* Indicates that start is null.
* @exception OutOfMemoryError
* Indicates that there is insufficient memory for the new list.
**/
public static IntNode[ ] listPart(IntNode start, IntNode end)
{
IntNode copyHead;
IntNode copyTail;
IntNode cursor;
IntNode[ ] answer = new IntNode[2];
// Make the first node for the newly created list. Notice that this will
// cause a NullPointerException if start is null.
copyHead = new IntNode(start.data, null);
copyTail = copyHead;
cursor = start;
// Make the rest of the nodes for the newly created list.
while (cursor != end)
{
cursor = cursor.link;
if (cursor == null)
throw new IllegalArgumentException
("end node was not found on the list");
copyTail.addNodeAfter(cursor.data);
copyTail = copyTail.link;
}
// Return the head and tail references
answer[0] = copyHead;
answer[1] = copyTail;
return answer;
}
/**
* Find a node at a specified position in a linked list.
* @param head
* the head reference for a linked list (which may be an empty list in
* which case the head is null)
* @param position
* a node number
* @precondition
* position > 0.
* @return
* The return value is a reference to the node at the specified position in
* the list. (The head node is position 1, the next node is position 2, and
* so on.) If there is no such position (because the list is too short),
* then the null reference is returned.
* @exception IllegalArgumentException
* Indicates that position is not positive.
**/
public static IntNode listPosition(IntNode head, int position)
{
IntNode cursor;
int i;
if (position <= 0)
throw new IllegalArgumentException("position is not positive");
cursor = head;
for (i = 1; (i < position) && (cursor != null); i++)
cursor = cursor.link;
return cursor;
}
/**
* Search for a particular piece of data in a linked list.
* @param head
* the head reference for a linked list (which may be an empty list in
* which case the head is null)
* @param target
* a piece of data to search for
* @return
* The return value is a reference to the first node that contains the
* specified target. If there is no such node, the null reference is
* returned.
**/
public static IntNode listSearch(IntNode head, int target)
{
IntNode cursor;
for (cursor = head; cursor != null; cursor = cursor.link)
if (target == cursor.data)
return cursor;
return null;
}
/**
* Modification method to remove the node after this node.
* @param - none
* @precondition
* This node must not be the tail node of the list.
* @postcondition
* The node after this node has been removed from the linked list.
* If there were further nodes after that one, they are still
* present on the list.
* @exception NullPointerException
* Indicates that this was the tail node of the list, so there is nothing
* after it to remove.
**/
public void removeNodeAfter( )
{
link = link.link;
}
/**
* Modification method to set the data in this node.
* @param newData
* the new data to place in this node
* @postcondition
* The data of this node has been set to newData.
**/
public void setData(int newData)
{
data = newData;
}
/**
* Modification method to set the link to the next node after this node.
* @param newLink
* a reference to the node that should appear after this node in the linked
* list (or the null reference if there is no node after this node)
* @postcondition
* The link to the node after this node has been set to newLink.
* Any other node (that used to be in this link) is no longer connected to
* this node.
**/
public void setLink(IntNode newLink)
{
link = newLink;
}
}
//文件:IntNode.java
/******************************************************************************
*IntNode为具有的链接列表提供节点
*每个节点中的整数数据。
*
*@注
*节点列表可以是任意长度的,仅受节点数量的限制
*释放堆中的内存。但超过Integer.MAX_值(2147483647),
*由于算术原因,listLength的答案不正确
*溢出。
******************************************************************************/
公共类IntNode
{
//IntNode类的不变量:
//1.节点的整数数据在实例变量数据中。
//2.对于列表的最后一个节点,链接部分为空。
//否则,链接部分是对
//列表的下一个节点。
私有int数据;
专用节点链路;
/**
*使用指定的初始数据初始化节点并链接到下一个节点
*注意initialLink可能是空引用,
*这表示新节点后面没有任何内容。
*@param initialData
*此新节点的初始数据
*@param initialLink
*在此新节点之后对该节点的引用--此引用可能为空
*指示此新节点之后没有节点。
*@后条件
*此节点包含指定的数据和到下一个节点的链接。
**/
公共IntNode(int initialData、IntNode initialLink)
{
数据=初始数据;
链接=初始链接;
}
/**
*在此节点之后添加新节点的修改方法。
*@param项目
*要放置在新节点中的数据
*@后条件
*已创建一个新节点并将其放置在此节点之后。
*新节点的数据为item。是否有其他节点
*以前位于此节点之后的节点现在位于新节点之后。
*@exception OutOfMemoryError
*表示内存不足,无法安装新的
*IntNode。
**/
public void addNodeAfter(int项)
{
链接=新的IntNode(项目,链接);
}
/**
*用于从此节点获取数据的访问器方法。
*@param-none
*@返回
*来自此节点的数据
**/
公共int getData()
{
返回数据;
}
/**
*访问器方法以获取对此节点之后的下一个节点的引用。
*@param-none
*@返回
*对该节点之后的节点的引用(如果存在,则为空引用)
*在此节点之后没有任何内容)
**/
公共IntNode getLink()
{
返回链接;
}
/**
*复制一份清单。
*@param源
*将被复制的链接列表的标题(可能是
*源为空时的空列表)
*@返回
*该方法已从开始创建链接列表的副本
*源。返回值是源的头引用
*抄袭。
*@exception OutOfMemoryError
*表示新列表的内存不足。
**/
公共静态IntNode listCopy(IntNode源)
{
节点打印头;
IntNode copyTail;
//处理空列表的特殊情况。
if(source==null)
返回null;
//为新创建的列表创建第一个节点。
copyHead=新的IntNode(source.data,null);
copyTail=复印头;
//为新创建的列表创建其余节点。
while(source.link!=null)
{
source=source.link;
copyTail.addNodeAfter(source.data);
copyTail=copyTail.link;
}
//返回新列表的标题引用。
return copyHead;//*此节点包含指定的数据并链接到下一个节点。
}
/**
*复制列表,同时返回副本的头引用和尾引用。
*@param源
*将被复制的链接列表的标题(可能是
*源为空时的空列表)
*@返回
*该方法已从开始创建链接列表的副本
*返回值是一个
*[0]元素是副本和[1]的头引用的数组
*元素是副本的尾部引用。
*@exception OutOfMemoryError
*表示新列表的内存不足。
**/
公共静态IntNode[]listCopyWithTail(IntNode源)
{
节点打印头;
IntNode copyTail;
IntNode[]应答=新的IntNode[2];
//处理空列表的特殊情况。
if(source==null)
return answer;//该答案有两个空引用。
//为新创建的列表创建第一个节点。
copyHead=新的IntNode(source.data,null);
copyTail=复印头;
//为新创建的列表创建其余节点。
while(source.link!=null)
{
source=source.link;
copyTail.addNodeAfter(source.data);
copyTail=copyTail.link;
}
//返回head和tail引用。
答案[0]=文案头;
答案[1]=复印件;
返回答案;
}
/**
*计算链表中的节点数。
*@param头
*链接列表(可能是空列表)的标题引用
*w
storage[0] = head1;
storage[1] = head2;