File tree Expand file tree Collapse file tree 1 file changed +22
-18
lines changed
algorithms/data-structures Expand file tree Collapse file tree 1 file changed +22
-18
lines changed Original file line number Diff line number Diff line change 1
1
/**
2
- * @author Rashik Ansar and Luiz Guerra
3
- *
4
- *
5
- * Implemtaion of Stack data structure
6
- * Stack follows LIFO (Last In First Out) priniciple
7
- * For Insertion and Deletion its complexity is O(1)
8
- * For Accessing and Searching its complexity is O(n)
9
- */
2
+ * @author Rashik Ansar and Luiz Guerra
3
+ *
4
+ * Implemtaion of Stack data structure
5
+ * Stack follows LIFO (Last In First Out) priniciple
6
+ * For Insertion and Deletion its complexity is O(1)
7
+ * For Accessing and Searching its complexity is O(n)
8
+ *
9
+ * @author Jan Tabacki
10
+ * Fix in toString method, display all elements and get data property instead of element
11
+ * which does no exist.
12
+ */
10
13
11
14
class Stack {
12
15
/**
@@ -63,21 +66,21 @@ class Stack {
63
66
}
64
67
return this . first . data ;
65
68
}
66
-
69
+
67
70
/**
68
71
* @returns size of the Stack
69
72
*/
70
73
size ( ) {
71
74
return this . size ;
72
75
}
73
-
76
+
74
77
/**
75
78
* @returns if Stack is empty
76
79
*/
77
80
isEmpty ( ) {
78
81
return this . size == 0 ;
79
82
}
80
-
83
+
81
84
/**
82
85
* clears the Stack
83
86
*/
@@ -86,24 +89,25 @@ class Stack {
86
89
this . last = null ;
87
90
this . size = 0 ;
88
91
}
89
-
92
+
90
93
/**
91
94
* @returns the Stack
92
95
*/
93
96
toString ( ) {
94
- let str = "" ;
97
+ let str = "" ;
95
98
let aux = this . first ;
96
- for ( let i = 0 ; i < this . count ; i ++ )
97
- str += aux . element + " " ;
98
- aux = aux . next ;
99
+ while ( aux ) {
100
+ str += aux . data + " " ;
101
+ aux = aux . next ;
102
+ }
99
103
return str ;
100
104
}
101
-
105
+
102
106
}
103
107
104
108
class Node {
105
109
constructor ( data ) {
106
110
this . data = data ;
107
111
this . next = null ;
108
112
}
109
- }
113
+ }
You can’t perform that action at this time.
0 commit comments