+ 1
JAVA Stack data structure , need experts's proposal......
Well,ill show two code,first is Original stack code,second was self created stack code.The thing that i doesn't fully understand is the different between both code,i knew there was a gap where the original code got determine whether the stack was null or not,but instead of that,can any experts/pro explain the original code for me in details? i would appreciate a reply. https://code.sololearn.com/cd2k5o2X2m09/?ref=app https://code.sololearn.com/ccWvLV4I80X3/?ref=app
5 Réponses
+ 1
Your "original stack code" does not make any sense to me. You define some StackNode and a lot of methods, but in the end you are not even using them, the main program only uses the standard java.util.Stack
Rather than analysing all the faults of a bad code, I would recommend to ponder about a good one that you can find in the community lessons on Data Structures
https://www.sololearn.com/learn/760/?ref=app
Click 'try it yourself' to see the full code
+ 1
original Java 11 Stack is this (minimized):
package java.util;
public class Stack<E> extends Vector<E> {
public Stack() { }
public E push(E item) {
addElement(item);
return item; }
public synchronized E pop() {
E obj;
int len = size();
obj = peek();
removeElementAt(len - 1);
return obj; }
public synchronized E peek() {
int len = size();
if (len == 0)
throw new EmptyStackException();
return elementAt(len - 1); }
public boolean empty() {
return size() == 0; }
public synchronized int search(Object o) {
int i = lastIndexOf(o);
if (i >= 0) {
return size() - i; }
return -1; }
private static final long serialVersionUID = 1224463164541339165L;
}
+ 1
back to the class STACKdatastructureORI():
It is similar to 'Linked List'
Chain of objects type StackNode, each stores int value and reference to next node object
root -> node3 -> node2 ->node1 ...
int3 int2 int1
root
push(1) root->node1
push(2) root->node2->node1
pop() root->node1 return 2
peek() root->node1 return 1
pop() root return 1
peek() root return Integer.MIN_VALUE
pop() root return Integer.MIN_VALUE
0
Tibor Santa could you explain how it work in step by step please......
https://www.sololearn.com/learn/760/?ref=app
0
zemiak Why it was so complex 😂