1
public class MyStack {

  private LinkedList<Integer> stack;

  public MyStack(int capacity){
      stack = new LinkedList<Integer>(capacity);
  }
}

Themelis
  • 4,048
  • 2
  • 21
  • 45
noynoy
  • 21
  • 3
  • 1
    I think, having capacity to the linked list does not make any sense, because it's underlying storage data structure is not an array like in case of arraylist, – TheCodeCache Apr 16 '20 at 05:53
  • by the way what is your use case, i mean what if MyStack's initial capacity gets full, for ex: if the initial capacity is 5, and if we add 6th element should it throw exception? – TheCodeCache Apr 16 '20 at 05:54
  • @Manoranjan, thank you for your reply. it is a homework to implement the stack. We are allowed to implement Array or Linked-list, but the professor holds the usage of capacity as mandatory. I want to back with Linked-list but wasn't able to initialize with capacity. – noynoy Apr 16 '20 at 06:28
  • 1
    I think the word "capacity" is being used in the assignment differently than we typically use it. Sounds to me like what the professor wants is for the stack to have a maximum allowable size, and for the program to issue an error (maybe throw an exception?) if an attempt is made to push an item that would exceed the stack's maximum size. – Jim Mischel Apr 16 '20 at 17:37

2 Answers2

3

A capacity for an ArrayList is a hint to say how big to make the initial backing array. The capacity does not limit how big the ArrayList will ultimately grow.

A LinkedList does not have a backing array or anything like it. There is no preallocation with a linked list, so the capacity of a LinkedList is a meaningless concept.

If you are looking for a way to limit the size of a list, read Define a fixed-size list in Java.


It is a homework to implement the stack. We are allowed to implement Array or Linked-list, but the professor holds the usage of capacity as mandatory. I want to back with Linked-list but wasn't able to initialize with capacity.

It is difficult to understand what is meant by that.

One possible interpretation is that your stack API is required to support a capacity parameter in the sense of ArrayList. For an implementation using linked lists, you would just ignore that parameter.

A second interpretation is that you are being asked to implement a stack with a limited size. In that case, your professor may be using the word "capacity" with a different meaning to what "capacity" means with ArrayList.

If you cannot work out what is required, I would advise you to ask your professor for clarification.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
-1

Typically stacks are implemented with ArrayLists or LinkedLists, but you can implement a stack using an array, and therefore set a capacity for the array/stack size. You cannot set a limit for a Linked Lists size when you declare it.

For implementation you should push elements into the first open position of the array. To pop an element implement a loop from the last index of the array until you find an index that contains an element.

You can also create a variable that keeps track of the top of the stack in your array

  • Welcome to SO, please refer to this for better presenting your question/answer: https://meta.stackoverflow.com/a/251362/3519504 – Sandeep Kumar May 14 '20 at 00:49