Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 65020da

Browse files
author
Ram swaroop
committed
stack interface + implementation
1 parent 756eb06 commit 65020da

File tree

3 files changed

+138
-13
lines changed

3 files changed

+138
-13
lines changed
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
package me.ramswaroop.common;
2+
3+
import me.ramswaroop.interfaces.Stack;
4+
5+
import java.util.NoSuchElementException;
6+
7+
/**
8+
* Created by IntelliJ IDEA.
9+
* User: ramswaroop
10+
* Date: 3/24/15
11+
* Time: 3:02 PM
12+
* To change this template go to Preferences | IDE Settings | File and Code Templates
13+
*/
14+
15+
/**
16+
* Stack implementation using
17+
* a singly linked list
18+
*
19+
* @param <E>
20+
*/
21+
public class LinkedStack<E> implements Stack<E> {
22+
23+
private Node top = null;
24+
25+
/**
26+
* Pushes an item onto the top of this stack.
27+
*
28+
* @param item
29+
*/
30+
@Override
31+
public void push(E item) {
32+
top = new Node(item, top);
33+
}
34+
35+
/**
36+
* Removes the object at the top of this stack and returns that object as the value of this function.
37+
*
38+
* @return
39+
*/
40+
@Override
41+
public E pop() {
42+
E item = peek();
43+
top = top.next;
44+
return item;
45+
}
46+
47+
/**
48+
* Looks at the object at the top of this stack without removing it from the stack.
49+
*
50+
* @return
51+
*/
52+
@Override
53+
public E peek() {
54+
if (top == null) {
55+
throw new NoSuchElementException();
56+
}
57+
return top.data;
58+
}
59+
60+
/**
61+
* Returns the number of items currently in the stack.
62+
*
63+
* @return
64+
*/
65+
@Override
66+
public int size() {
67+
int count = 0;
68+
for (Node node = top; node != null; node = top.next) {
69+
count++;
70+
}
71+
return count;
72+
}
73+
74+
/**
75+
* Tests if this stack is empty.
76+
*
77+
* @return
78+
*/
79+
@Override
80+
public boolean isEmpty() {
81+
return top == null;
82+
}
83+
84+
private class Node {
85+
E data;
86+
Node next;
87+
88+
Node(E data, Node next) {
89+
this.data = data;
90+
this.next = next;
91+
}
92+
}
93+
}

src/me/ramswaroop/common/Stack.java

Lines changed: 0 additions & 13 deletions
This file was deleted.
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package me.ramswaroop.interfaces;
2+
3+
/**
4+
* Created by IntelliJ IDEA.
5+
* User: ramswaroop
6+
* Date: 4/3/15
7+
* Time: 9:47 AM
8+
* To change this template go to Preferences | IDE Settings | File and Code Templates
9+
*/
10+
public interface Stack<E> {
11+
/**
12+
* Pushes an item onto the top of this stack.
13+
*
14+
* @param item
15+
*/
16+
public void push(E item);
17+
18+
/**
19+
* Removes the object at the top of this stack and returns that object as the value of this function.
20+
*
21+
* @return
22+
*/
23+
public E pop();
24+
25+
/**
26+
* Looks at the object at the top of this stack without removing it from the stack.
27+
*
28+
* @return
29+
*/
30+
public E peek();
31+
32+
/**
33+
* Returns the number of items currently in the stack.
34+
*
35+
* @return
36+
*/
37+
public int size();
38+
39+
/**
40+
* Tests if this stack is empty.
41+
*
42+
* @return
43+
*/
44+
public boolean isEmpty();
45+
}

0 commit comments

Comments
 (0)