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

Commit5f22570

Browse files
author
asri71
committed
Adding Stack implementation along with test class
1 parent822c91c commit5f22570

File tree

2 files changed

+267
-0
lines changed

2 files changed

+267
-0
lines changed
Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
packagesrc.main.java.com.dataStructures;
2+
3+
importjava.io.Serializable;
4+
importjava.util.EmptyStackException;
5+
importjava.util.Vector;
6+
7+
publicclassStack<E>extendsVectorimplementsSerializable {
8+
9+
/**
10+
* Inital capacity alloted to stack on object creation
11+
*/
12+
privatefinalintINITIAL_CAPACITY =10;
13+
14+
/**
15+
* Increment in memory space once stack is out of space
16+
*/
17+
privatefinalintEXNTEDED_CAPACITY =10;
18+
19+
20+
/**
21+
* Position of tail in stack
22+
*/
23+
24+
privateinttail = -1;
25+
26+
/**
27+
* Size of stack at any given time
28+
*/
29+
30+
privateintsize;
31+
32+
/**
33+
* Uninitialized array to hold stack elements.
34+
* WIll be intialized with inital capacity once the object is created
35+
*/
36+
privateObject[]elements;
37+
38+
/**
39+
* No argument to create stack object with inital capacity
40+
*/
41+
publicStack() {
42+
43+
elements =newObject[INITIAL_CAPACITY];
44+
}
45+
46+
/**
47+
* Method to check if the given stack is empty or not
48+
*/
49+
50+
publicbooleanempty() {
51+
52+
if(null ==elements) {
53+
returntrue;
54+
}
55+
56+
if(size ==0){
57+
returntrue;
58+
}
59+
60+
returnfalse;
61+
}
62+
63+
64+
/**
65+
* Method to check the element on head without removing it
66+
*/
67+
68+
publicObjectpeek() {
69+
70+
if(empty()) {
71+
thrownewEmptyStackException();
72+
}
73+
74+
returnelements[tail];
75+
}
76+
77+
/**
78+
* Method to remove the top element from stack
79+
*/
80+
81+
publicObjectpop() {
82+
83+
if(empty()) {
84+
thrownewEmptyStackException();
85+
}
86+
87+
ObjectremovedElement =elements[tail];
88+
tail--;
89+
size--;
90+
returnremovedElement;
91+
}
92+
93+
/**
94+
* Method to add element to stack
95+
*/
96+
publicObjectpush(Objecte) {
97+
98+
booleanisSuccess =false;
99+
if(tail < (INITIAL_CAPACITY -1)){
100+
tail++;
101+
elements[tail] =e;
102+
}else{
103+
Object[]extendedElements =newObject[INITIAL_CAPACITY +EXNTEDED_CAPACITY];
104+
System.arraycopy(elements,0,extendedElements,0, (tail+1));
105+
elements =extendedElements;
106+
tail++;
107+
elements[tail] =e;
108+
}
109+
size++;
110+
returne;
111+
112+
}
113+
114+
/**
115+
* Method to search for an element in stack
116+
*/
117+
118+
publicintsearch(Objecto) {
119+
120+
intindex = -1;
121+
booleanfound =false;
122+
if(empty()) {
123+
return -1;
124+
}
125+
126+
for(inti=0;i<size();i++) {
127+
if(elements[i] ==o) {
128+
index =i;
129+
found =true;
130+
break;
131+
}
132+
}
133+
134+
if(found) {
135+
index =tail -index +1;
136+
}
137+
138+
returnindex;
139+
}
140+
141+
/**
142+
* Method to get size of stack
143+
*/
144+
publicintsize() {
145+
returnsize;
146+
}
147+
148+
}
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