Question: I know that Stack Linked List is O(1) constant time and Stack Array is O(n) but I don't understand stand why its showing the Stack

I know that Stack Linked List is O(1) constant time and Stack Array is O(n) but I don't understand stand why its showing the Stack Array is quicker in nanoseconds than the Stack Linked List. Am I understanding this wrong or did I make a mistake ? Please give me detailed explanation.

StackLinkedList stacklink = new StackLinkedList();

long startTime1 = System.nanoTime();

stacklink.push(5);

stacklink.push(3);

stacklink.push(7);

stacklink.push(8);

stacklink.push(23);

stacklink.push(19);

long estimatedTime1 = System.nanoTime() - startTime1;

System.out.println("Stack Linked List Implementation took: " + estimatedTime1 + " nanoseconds");

stacklink.displayStack();

Output:

Stack Linked List Implementation took: 369282 nanoseconds

Stack st = new Stack();

long startTime1 = System.nanoTime();

st.push(5);

st.push(3);

st.push(7);

st.push(8);

st.push(23);

st.push(19);

long estimatedTime1 = System.nanoTime() - startTime1;

System.out.println("Stack Array Implementation took: " + estimatedTime1 + " nanoseconds");

19

23

8

7

3

5

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!