File tree Expand file tree Collapse file tree 3 files changed +138
-13
lines changed Expand file tree Collapse file tree 3 files changed +138
-13
lines changed Original file line number Diff line number Diff line change
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
+ }
Load Diff This file was deleted.
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments