-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathLinkedStack.java
70 lines (63 loc) · 1.7 KB
/
LinkedStack.java
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
import java.util.*;
public class LinkedStack
{
private int length; //indicates the size of the linked list
private StackNode top; //acting like a head of linked list
public LinkedStack()
{
length=0;
top=null;
}
//adds a specified data to the top of this stack
public void push(int data)
{
StackNode temp = new StackNode(data);
temp.setNext(top);
top=temp;
length++;
}
//removes the data at the top of this stack and returns a reference to it.throws an empty stack exception 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 eleements in the stack
public int size()
{
return length;
}
//returns the string representation of the stack
public String toString()
{
String result="";
StackNode current=top;
while(current!=null)
{
result=result+current.toString()+"\n";
current=current.getNext();
}
return result;
}
}