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

Commit1093a49

Browse files
authored
Merge pull requestTheAlgorithms#717 from AnkitAtBrillio/local
Adding Stack implementation along with test class
2 parents822c91c +46df09a commit1093a49

File tree

2 files changed

+253
-0
lines changed

2 files changed

+253
-0
lines changed
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
packagesrc.main.java.com.dataStructures;
2+
3+
importjava.io.Serializable;
4+
importjava.util.EmptyStackException;
5+
6+
publicclassStack<E>implementsSerializable {
7+
8+
/**
9+
* Initial capacity allocated to stack on object creation
10+
*/
11+
privatefinalintINITIAL_CAPACITY =10;
12+
13+
/**
14+
* Increment in memory space once stack is out of space
15+
*/
16+
privatefinalintEXTENDED_CAPACITY =10;
17+
18+
19+
/**
20+
* Position of tail in stack
21+
*/
22+
23+
privateinttail = -1;
24+
25+
/**
26+
* Size of stack at any given time
27+
*/
28+
29+
privateintsize;
30+
31+
/**
32+
* Uninitialized array to hold stack elements.
33+
* WIll be initialized with initial capacity once the object is created
34+
*/
35+
privateObject[]elements;
36+
37+
/**
38+
* No argument to create stack object with initial capacity
39+
*/
40+
publicStack() {
41+
elements =newObject[INITIAL_CAPACITY];
42+
}
43+
44+
/**
45+
* Method to check if the given stack is empty or not
46+
*/
47+
48+
publicbooleanempty() {
49+
returnelements ==null ||size ==0;
50+
}
51+
52+
53+
/**
54+
* Method to check the element on head without removing it
55+
*/
56+
57+
publicObjectpeek() {
58+
if (empty()) {
59+
thrownewEmptyStackException();
60+
}
61+
62+
returnelements[tail];
63+
}
64+
65+
/**
66+
* Method to remove the top element from stack
67+
*/
68+
69+
publicObjectpop() {
70+
if (empty()) {
71+
thrownewEmptyStackException();
72+
}
73+
74+
ObjectremovedElement =elements[tail];
75+
tail--;
76+
size--;
77+
returnremovedElement;
78+
}
79+
80+
/**
81+
* Method to add element to stack
82+
*/
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));
92+
elements =extendedElements;
93+
tail++;
94+
elements[tail] =e;
95+
}
96+
size++;
97+
returne;
98+
99+
}
100+
101+
/**
102+
* Method to search for an element in stack
103+
*/
104+
105+
publicintsearch(Objecto) {
106+
107+
intindex = -1;
108+
booleanfound =false;
109+
if (empty()) {
110+
return -1;
111+
}
112+
113+
for (inti =0;i <size();i++) {
114+
if (elements[i] ==o) {
115+
index =i;
116+
found =true;
117+
break;
118+
}
119+
}
120+
121+
if (found) {
122+
index =tail -index +1;
123+
}
124+
125+
returnindex;
126+
}
127+
128+
/**
129+
* Method to get size of stack
130+
*/
131+
publicintsize() {
132+
returnsize;
133+
}
134+
}
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
packagesrc.test.java.com.dataStructures;
2+
3+
importorg.junit.Assert;
4+
importorg.junit.Test;
5+
importsrc.main.java.com.dataStructures.Stack;
6+
7+
importjava.util.EmptyStackException;
8+
9+
publicclassStackTest {
10+
11+
@Test
12+
publicvoidtestEmpty() {
13+
14+
Stack<Integer>myStack =newStack<>();
15+
booleanisEmpty =myStack.empty();
16+
Assert.assertTrue(isEmpty);
17+
18+
myStack.push(10);
19+
isEmpty =myStack.empty();
20+
Assert.assertFalse(isEmpty);
21+
}
22+
23+
@Test(expected =EmptyStackException.class)
24+
publicvoidtestPeekWithoutElements() {
25+
26+
Stack<Integer>myStack =newStack<>();
27+
myStack.peek();
28+
}
29+
30+
@Test
31+
publicvoidtestPeekWithElements() {
32+
33+
Stack<Integer>myStack =newStack<>();
34+
myStack.push(10);
35+
myStack.push(20);
36+
myStack.push(30);
37+
myStack.push(40);
38+
39+
Assert.assertEquals(40,myStack.peek());
40+
}
41+
42+
@Test(expected =EmptyStackException.class)
43+
publicvoidtestPopWithoutElements() {
44+
45+
Stack<Integer>myStack =newStack<>();
46+
myStack.pop();
47+
48+
}
49+
50+
@Test
51+
publicvoidtestPopWithElements() {
52+
53+
Stack<Integer>myStack =newStack<>();
54+
myStack.push(10);
55+
myStack.push(20);
56+
myStack.push(30);
57+
myStack.push(40);
58+
myStack.push(50);
59+
60+
Assert.assertEquals(50,myStack.pop());
61+
62+
}
63+
64+
@Test
65+
publicvoidtestPushWithinInitialCapacity() {
66+
67+
Stack<Integer>myStack =newStack<>();
68+
myStack.push(10);
69+
myStack.push(20);
70+
myStack.push(30);
71+
myStack.push(40);
72+
myStack.push(50);
73+
myStack.push(60);
74+
myStack.push(70);
75+
myStack.push(80);
76+
myStack.push(90);
77+
myStack.push(100);
78+
Assert.assertEquals(10,myStack.size());
79+
}
80+
81+
@Test
82+
publicvoidtestPushOutsideInitialCapacity() {
83+
84+
Stack<Integer>myStack =newStack<>();
85+
myStack.push(10);
86+
myStack.push(20);
87+
myStack.push(30);
88+
myStack.push(40);
89+
myStack.push(50);
90+
myStack.push(60);
91+
myStack.push(70);
92+
myStack.push(80);
93+
myStack.push(90);
94+
myStack.push(100);
95+
myStack.push(110);
96+
Assert.assertEquals(11,myStack.size());
97+
}
98+
99+
@Test
100+
publicvoidtestSearchWithObjectUnavailable() {
101+
102+
Stack<Integer>myStack =newStack<>();
103+
myStack.push(10);
104+
myStack.push(20);
105+
myStack.push(30);
106+
Assert.assertEquals(-1,myStack.search(50));
107+
}
108+
109+
@Test
110+
publicvoidtestSearchWithObjectAvailable() {
111+
112+
Stack<Integer>myStack =newStack<>();
113+
myStack.push(10);
114+
myStack.push(20);
115+
myStack.push(30);
116+
Assert.assertEquals(3,myStack.search(10));
117+
118+
}
119+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp