-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
57 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package my.exercises.shortjob; | ||
|
||
public class MyQueueLinked { | ||
|
||
private Node head; | ||
public Node tail; | ||
private int count; | ||
|
||
public boolean isFull(){return false;} | ||
public boolean isEmpty(){return count == 0;} | ||
|
||
public boolean enqueue(Object item){ | ||
Node node = new Node(item); | ||
|
||
if(isEmpty()) | ||
head = node; | ||
else | ||
tail.setNext(node); | ||
tail= node; | ||
count++; | ||
return true; | ||
} | ||
public Object peek(){return !isEmpty() ? head.getItem() : null;} | ||
public Object dequeue(){ | ||
|
||
Object item = peek(); | ||
if(item != null){ | ||
head = head.getNext(); | ||
count--; | ||
} | ||
return item; | ||
} | ||
public void clear(){ | ||
for(; !isEmpty(); dequeue()); | ||
} | ||
public int size(){return count;} | ||
public String toString(){ | ||
StringBuffer sb = new StringBuffer(); | ||
|
||
for(Node node = head; node!= null; node= node.getNext()){ | ||
sb.append(node.getItem()); | ||
} | ||
|
||
|
||
return sb.toString(); | ||
} | ||
public static void main(String... args){ | ||
MyQueueLinked q = new MyQueueLinked(); | ||
|
||
q.enqueue(1); | ||
q.enqueue(2); | ||
q.enqueue(5); | ||
q.enqueue(7); | ||
System.out.println(q.dequeue()); | ||
System.out.print(q); | ||
} | ||
} |