Repository navigation
Expand file tree
/
Copy pathLinkedStack.java
More file actions
81 lines (68 loc) · 2.08 KB
/
Copy pathLinkedStack.java
File metadata and controls
81 lines (68 loc) · 2.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/*Copyright (c) Dec 21, 2014 CareerMonk Publications and others.
* E-Mail : info@careermonk.com
* Creation Date : 2015-01-10 06:15:46
* Last modification : 2006-05-31
by : Narasimha Karumanchi
* File Name : StackWithLinkedList.java
* Book Title : Data Structures And Algorithms Made In Java
* Warranty : This software is provided "as is" without any
* warranty; without even the implied warranty of
* merchantability or fitness for a particular purpose.
*
*/
package chapter04stacks;
import chapter03linkedlists.*;
import java.util.EmptyStackException;
public class LinkedStack{
private int length; // indicates the size of the linked list
private ListNode top;
// Constructor: Creates an empty stack.
public LinkedStack() {
length = 0;
top = null;
}
// Adds the specified data to the top of this stack.
public void push (int data) {
ListNode temp = new ListNode (data);
temp.setNext(top);
top = temp;
length++;
}
// Removes the data at the top of this stack and returns a
// reference to it. Throws an EmptyStackException if the stack
// is empty.
public int pop() throws EmptyStackException{
if (isEmpty())
throw new EmptyStackException();
int result = top.getData();
top = top.getNext();
length--;
return result;
}
// Returns a reference to the data at the top of this stack.
// The data is not removed from the stack. Throws an
// EmptyStackException if the stack is empty.
public int peek() throws EmptyStackException{
if (isEmpty())
throw new EmptyStackException();
return top.getData();
}
// Returns true if this stack is empty and false otherwise.
public boolean isEmpty(){
return (length == 0);
}
// Returns the number of elements in the stack.
public int size(){
return length;
}
// Returns a string representation of this stack.
public String toString(){
String result = "";
ListNode current = top;
while (current != null){
result = result + current.toString() + "\n";
current = current.getNext();
}
return result;
}
}