Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit46df09a

Browse files
authored
Update Stack.java
Format code
1 parent27753bb commit46df09a

File tree

1 file changed

+19
-32
lines changed

1 file changed

+19
-32
lines changed

‎src/main/java/com/dataStructures/Stack.java

Lines changed: 19 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,17 @@
33
importjava.io.Serializable;
44
importjava.util.EmptyStackException;
55

6-
publicclassStack<E>implementsSerializable {
6+
publicclassStack<E>implementsSerializable {
77

88
/**
9-
*Inital capacityalloted to stack on object creation
9+
*Initial capacityallocated to stack on object creation
1010
*/
1111
privatefinalintINITIAL_CAPACITY =10;
1212

1313
/**
1414
* Increment in memory space once stack is out of space
1515
*/
16-
privatefinalintEXNTEDED_CAPACITY =10;
16+
privatefinalintEXTENDED_CAPACITY =10;
1717

1818

1919
/**
@@ -30,15 +30,14 @@ public class Stack<E> implements Serializable {
3030

3131
/**
3232
* Uninitialized array to hold stack elements.
33-
* WIll beintialized withinital capacity once the object is created
33+
* WIll beinitialized withinitial capacity once the object is created
3434
*/
3535
privateObject[]elements;
3636

3737
/**
38-
* No argument to create stack object withinital capacity
38+
* No argument to create stack object withinitial capacity
3939
*/
4040
publicStack() {
41-
4241
elements =newObject[INITIAL_CAPACITY];
4342
}
4443

@@ -47,16 +46,7 @@ public Stack() {
4746
*/
4847

4948
publicbooleanempty() {
50-
51-
if(null ==elements) {
52-
returntrue;
53-
}
54-
55-
if(size ==0){
56-
returntrue;
57-
}
58-
59-
returnfalse;
49+
returnelements ==null ||size ==0;
6050
}
6151

6252

@@ -65,21 +55,19 @@ public boolean empty() {
6555
*/
6656

6757
publicObjectpeek() {
58+
if (empty()) {
59+
thrownewEmptyStackException();
60+
}
6861

69-
if(empty()) {
70-
thrownewEmptyStackException();
71-
}
72-
73-
returnelements[tail];
62+
returnelements[tail];
7463
}
7564

7665
/**
7766
* Method to remove the top element from stack
7867
*/
7968

8069
publicObjectpop() {
81-
82-
if(empty()) {
70+
if (empty()) {
8371
thrownewEmptyStackException();
8472
}
8573

@@ -95,12 +83,12 @@ public Object pop() {
9583
publicObjectpush(Objecte) {
9684

9785
booleanisSuccess =false;
98-
if(tail < (INITIAL_CAPACITY -1)){
86+
if(tail < (INITIAL_CAPACITY -1)){
9987
tail++;
10088
elements[tail] =e;
101-
}else{
102-
Object[]extendedElements =newObject[INITIAL_CAPACITY +EXNTEDED_CAPACITY];
103-
System.arraycopy(elements,0,extendedElements,0, (tail+1));
89+
}else{
90+
Object[]extendedElements =newObject[INITIAL_CAPACITY +EXTENDED_CAPACITY];
91+
System.arraycopy(elements,0,extendedElements,0, (tail +1));
10492
elements =extendedElements;
10593
tail++;
10694
elements[tail] =e;
@@ -118,19 +106,19 @@ public int search(Object o) {
118106

119107
intindex = -1;
120108
booleanfound =false;
121-
if(empty()) {
109+
if(empty()) {
122110
return -1;
123111
}
124112

125-
for(inti=0;i<size();i++) {
126-
if(elements[i] ==o) {
113+
for(inti =0;i <size();i++) {
114+
if(elements[i] ==o) {
127115
index =i;
128116
found =true;
129117
break;
130118
}
131119
}
132120

133-
if(found) {
121+
if(found) {
134122
index =tail -index +1;
135123
}
136124

@@ -143,5 +131,4 @@ public int search(Object o) {
143131
publicintsize() {
144132
returnsize;
145133
}
146-
147134
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp