-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinkedListCirculerQueue.java
More file actions
182 lines (173 loc) · 4.59 KB
/
Copy pathLinkedListCirculerQueue.java
File metadata and controls
182 lines (173 loc) · 4.59 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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/**
* A circular queue of simple objects implemented via a linked list.
*
* @author Sachith Raneweera
* @version 1.0 2016-10-28
*/
public class LinkedListCirculerQueue
{
// instance field
private Node top;
private int size;
/* constructors */
/**
* Inizialize the instance fields decleared.
pre-condition: maximumSize > 0
*/
public LinkedListCirculerQueue()
{
top = null;
size = 0;
} // end of LinkedListCircularQueue()
/**
* Returns true if the queue is empty, otherwise false.
*
* @returns true if the queue is empty, otherwise false.
*/
public boolean isEmpty()
{
// checks the node top pointer is null or not
if (top == null)
{
return true;
}
else
{
return false;
} // end of if (top == null)
} // end of method isEmpty()
/**
* Returns the number of elements on this queue.
*
* @returns the number of elements on this queue.
*/
public int queueSize()
{
return size;
} // end of method queueuSIze()
/**
* Without dequeuing, returns the first element of this queue.
pre-condition: the queue is not empty
*/
public Node peek()
{
if (top != null)
{
// create a temporary variable to store head
Node temp = top;
// go to the second last element of the list
while (temp.getNext() != null)
{
temp = temp.getNext();
} // end of while (temp != null)
// get the last element
temp = temp.getNext();
// reutrn the last element
return temp;
}
else
{
return null;
}
} // end of method peek()
/**
* Removes the integer at the front of the queue.
* pre-condition: the queue is not empty.
*
* @return the integer at the front of the queue.
*/
public Node dequeue()
{
// cheks the queue is not empty
if (!isEmpty())
{
Node temp = top;
Node prev = top;
while (temp != null)
{
if (prev.getNext() == null)
{
prev.setNext(null);
}
else
{
prev.setNext(prev.getNext().getNext());
}
prev = temp;
temp = temp.getNext();
}
}
else
{
// if its not full return null
return null;
} // end of if (!isEmpty())
return null;
} // end of method dequeue()
/**
* Adds the specified integer to the rear of the queue.
*/
public void enqueue(int value)
{
Node newNode = new Node (value, null);
if (top != null)
{
Node temp = top;
// goes to the send last element
while (temp.getNext() != null)
{
temp = temp.getNext();
} // end of while (temp != null)
// add node
temp.setNext(newNode);
}
else
{
top = newNode;
}
} // end of method enqueue(Node value)
/**
* Returns the nodes.
*
* @returns the nodes
*/
public String displayElements()
{
if (top != null)
{
// decleration of the string and node
String elements = "";
Node temp = top;
int counter = 0;
// goes to the end of the list
while (temp != null)
{
counter++;
// add the element value to the string
elements += "Node " + counter + ": " +temp.getData();
temp = temp.getNext();
} // end of while (temp != null)
/*
// adds the last value data
temp = temp.getNext();
elements += "Node " + (counter + 1) + ": " + temp.getData();
*/
// returns the string
return elements;
}
else
{
return null;
}
} // end of displayElements()
/**
* Returns a string representation of this queue.
*/
public String toString()
{
return
getClass().getName()
+ "[List: " + this.displayElements()
+ "]";
} // end of methof toString()
} // end of class LinkedListCirculerQueue