-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinkListStack.java
More file actions
147 lines (136 loc) · 3.43 KB
/
Copy pathLinkListStack.java
File metadata and controls
147 lines (136 loc) · 3.43 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/**
* A stack of integers implemented via an linked list.
*
* @author Sachith Ranaweera
* @version 1.0 2016-10-28
*/
public class LinkListStack
{
// instance field
private Node top;
private int size;
/* constructor */
/**
* Constructsa stack of integers with the specified maximum size.
pre condition: maximumSize > 0
*/
public LinkListStack()
{
top = null;
size = 0;
} // end of constructor LinkListStack()
/**
* Returs true if the stack is empty, otherwise false.
*
* @returns true if the stack is empty, otherwise false.
*/
public boolean isEmpty()
{
// checks the top element is null or not
if (top == null)
{
// if it is null
return true;
}
else
{
// if it is not null
return false;
} // end of if (top == null)
} // end of method isEmpty()
/**
* Returns the number of elements in the stack.
*
* @returns the number of elements in the stack.
*/
public int getSize()
{
return size;
} // end of getSize()
/**
* Pops the top simple object from the stack.
pre-condition: the stack is not empty
*
* @returns the top element that popped.
*/
public int pop()
{
if (isEmpty())
{
return 0;
}
else
{
Node temp = top;
top = temp.getNext();
size--;
return temp.getData();
} // end of if (isEmpty())
}// end of method pop()
/**
* Returns the top element of the linked list without disturbing
*/
public int peek()
{
if (isEmpty())
{
System.out.println("Underflow Exception");
return 0;
}
else
{
return top.getData();
} // end of if (isEmpty())
} // end of method peek()
/**
* Pushes the specified simple object onto the stack.
pre-condition: the stack is not full
*/
public void push(int change)
{
// create a temporary node
Node temp = new Node (change, null);
// cheks the top is null
if (top == null)
{
// top cahnge to temp
top = temp;
}
else
{
// else change top to temp, was top to second node
temp.setNext(top);
top = temp;
} // end of if (top == null)
size++;
}// end of method push()
/* Function to display the status of the stack */
public void display()
{
System.out.print("\nStack = ");
if (size == 0)
{
System.out.print("Empty\n");
return ;
}
Node ptr = top;
while (ptr != null)
{
System.out.print(ptr.getData()+" ");
ptr = ptr.getNext();
}
System.out.println();
}
/**
* Returns a string representation of this stack.
*
* @returns a string reprecentation of this stack.
*/
public String toString()
{
return
getClass().getName()
+ "["
+ "]";
} // end of methof toString()
} // end of class LinkListStack