Java 使用初始容量堆栈linkedlist
我正在使用Java中的LinkedList和LinearNode类进行堆栈,我遇到了一个问题:Java 使用初始容量堆栈linkedlist,java,list,linked-list,stack,capacity,Java,List,Linked List,Stack,Capacity,我正在使用Java中的LinkedList和LinearNode类进行堆栈,我遇到了一个问题: public BoundedStackADTLinkedImpl() { count = 0; top = null; } public BoundedStackADTLinkedImpl(int stackCapacity) { //I dont know how to do a builder using LinearNode with a initia
public BoundedStackADTLinkedImpl() {
count = 0;
top = null;
}
public BoundedStackADTLinkedImpl(int stackCapacity) {
//I dont know how to do a builder using LinearNode with a initial Capacity
}
public BoundedStackADTLinkedImpl(int stackCapacity, T... initialContent) {
//Here I have the same problem.
count = 0;
top = new LinearNode<T>();
for (T Vi : initialContent) {
push(Vi);
}
公共边界stackadtlinkedimpl(){
计数=0;
top=null;
}
公共边界堆栈ADTLINKEDIMPL(整数堆栈容量){
//我不知道如何使用具有初始容量的LinearNode进行构建
}
公共边界stackadtlinkedimpl(int stackCapacity,T…initialContent){
//这里我也有同样的问题。
计数=0;
top=新的LinearNode();
对于(T Vi:初始内容){
推(Vi);
}
public BoundedStackADTLinkedImpl() {
count = 0;
top = null;
}
public BoundedStackADTLinkedImpl(int stackCapacity) {
//I dont know how to do a builder using LinearNode with a initial Capacity
}
public BoundedStackADTLinkedImpl(int stackCapacity, T... initialContent) {
//Here I have the same problem.
count = 0;
top = new LinearNode<T>();
for (T Vi : initialContent) {
push(Vi);
}
谢谢!!!
链接列表
添加项目时分配内存。初始容量没有意义。每个项目都有指向下一个项目的指针
你为什么不使用有方法的方法呢
对于链接列表,您需要这样的功能。请记住,您不能设置初始容量,因为容量由列表中的元素数量决定,您不需要为它们预先创建空间
public class LinkedList<T>
{
Node<T> topNode = null;
public LinkedList()
{
}
public LinkedList( Node<T>... nodes )
{
for( Node<T> n : nodes )
{
push( n );
}
}
public void push(Node<T> node)
{
if ( topNode == null )
{
topNode = node;
return;
}
Node<T> n = topNode;
while( n.nextNode != null )
{
n = n.nextNode;
}
n.nextNode = node;
}
public Node<T> pop()
{
if ( topNode != null )
{
Node<T> n = topNode;
Node<T> p = n;
while( n.nextNode != null )
{
n = n.nextNode;
if ( n.nextNode != null )
{
p = n;
}
}
p.nextNode = null;
return n;
}
return null;
}
}
class Node <T>
{
Node<T> nextNode = null;
T value;
public Node( T val )
{
value = val;
}
}
公共类链接列表
{
Node-topNode=null;
公共链接列表()
{
}
公共链接列表(节点…节点)
{
用于(节点n:节点)
{
推(n);
}
}
公共无效推送(节点)
{
if(topNode==null)
{
topNode=节点;
返回;
}
节点n=顶部节点;
while(n.nextNode!=null)
{
n=n.nextNode;
}
n、 nextNode=节点;
}
公共节点pop()
{
if(topNode!=null)
{
节点n=顶部节点;
节点p=n;
while(n.nextNode!=null)
{
n=n.nextNode;
如果(n.nextNode!=null)
{
p=n;
}
}
p、 nextNode=null;
返回n;
}
返回null;
}
}
类节点
{
节点nextNode=null;
T值;
公共节点(T val)
{
值=val;
}
}
请多说一点您的问题。请提供一个最小的可测试代码。LinkedList只有您实际使用的节点,因此初始容量没有意义。可能重复感谢您的回答我只有一个接口,我想我必须这样做。