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

Commitd678fae

Browse files
authored
Merge pull requestTheAlgorithms#1240 from leny-mi/Development
Fix maven link and correct Stack implementation
2 parents8b416b1 +2d4d7dd commitd678fae

File tree

2 files changed

+19
-30
lines changed

2 files changed

+19
-30
lines changed

‎build.gradle‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ plugins {
55
repositories {
66
mavenLocal()
77
maven {
8-
url='http://repo.maven.apache.org/maven2'
8+
url='https://repo.maven.apache.org/maven2'
99
}
1010
}
1111

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

Lines changed: 18 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -19,42 +19,38 @@ public class Stack<E> implements Serializable {
1919
/**
2020
* Position of tail in stack
2121
*/
22-
2322
privateinttail = -1;
2423

2524
/**
2625
* Size of stack at any given time
2726
*/
28-
2927
privateintsize;
3028

3129
/**
3230
* Uninitialized array to hold stack elements.
33-
*WIll be initialized with initial capacity once the object is created
31+
*Will be initialized with initial capacity once the object is created
3432
*/
35-
privateObject[]elements;
33+
privateE[]elements;
3634

3735
/**
3836
* No argument to create stack object with initial capacity
3937
*/
38+
@SuppressWarnings("unchecked")
4039
publicStack() {
41-
elements =newObject[INITIAL_CAPACITY];
40+
elements =(E[])newObject[INITIAL_CAPACITY];
4241
}
4342

4443
/**
4544
* Method to check if the given stack is empty or not
4645
*/
47-
4846
publicbooleanempty() {
4947
returnelements ==null ||size ==0;
5048
}
5149

52-
5350
/**
5451
* Method to check the element on head without removing it
5552
*/
56-
57-
publicObjectpeek() {
53+
publicEpeek() {
5854
if (empty()) {
5955
thrownewEmptyStackException();
6056
}
@@ -65,13 +61,12 @@ public Object peek() {
6561
/**
6662
* Method to remove the top element from stack
6763
*/
68-
69-
publicObjectpop() {
64+
publicEpop() {
7065
if (empty()) {
7166
thrownewEmptyStackException();
7267
}
7368

74-
ObjectremovedElement =elements[tail];
69+
EremovedElement =elements[tail];
7570
tail--;
7671
size--;
7772
returnremovedElement;
@@ -80,29 +75,23 @@ public Object pop() {
8075
/**
8176
* Method to add element to stack
8277
*/
83-
publicObjectpush(Objecte) {
84-
85-
booleanisSuccess =false;
86-
if (tail < (INITIAL_CAPACITY -1)) {
87-
tail++;
88-
elements[tail] =e;
89-
}else {
90-
Object[]extendedElements =newObject[INITIAL_CAPACITY +EXTENDED_CAPACITY];
91-
System.arraycopy(elements,0,extendedElements,0, (tail +1));
78+
@SuppressWarnings("unchecked")
79+
publicvoidpush(Ee) {
80+
81+
tail =tail +1;
82+
if (tail >=INITIAL_CAPACITY) {
83+
E[]extendedElements = (E[])newObject[INITIAL_CAPACITY +EXTENDED_CAPACITY];
84+
System.arraycopy(elements,0,extendedElements,0,tail);
9285
elements =extendedElements;
93-
tail++;
94-
elements[tail] =e;
9586
}
96-
size++;
97-
returne;
98-
87+
elements[tail] =e;
88+
size =size +1;
9989
}
10090

10191
/**
10292
* Method to search for an element in stack
10393
*/
104-
105-
publicintsearch(Objecto) {
94+
publicintsearch(Eo) {
10695

10796
intindex = -1;
10897
booleanfound =false;
@@ -111,7 +100,7 @@ public int search(Object o) {
111100
}
112101

113102
for (inti =0;i <size();i++) {
114-
if (elements[i] ==o) {
103+
if (elements[i].equals(o)) {
115104
index =i;
116105
found =true;
117106
break;

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp