Advertisement
Guest User

Untitled

a guest
Feb 20th, 2020
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.17 KB | None | 0 0
  1. // A circular queue.
  2. class CircularQueue implements ICharQ {
  3. private char q[]; // this array holds the queue
  4. private int putloc, getloc; // the put and get indices
  5.  
  6. // Construct an empty queue given its size.
  7. public CircularQueue(int size) {
  8. q = new char[size+1]; // allocate memory for queue
  9. putloc = getloc = 0;
  10. }
  11.  
  12. // Put a characer into the queue.
  13. public void put(char ch) {
  14. /* Queue is full if either putloc is one less than
  15. getloc, or if putloc is at the end of the array
  16. and getloc is at the beginning. */
  17. if(putloc+1==getloc |
  18. ((putloc==q.length-1) & (getloc==0))) {
  19. System.out.println(" -- Queue is full.");
  20. return;
  21. }
  22.  
  23. q[putloc++] = ch;
  24. if(putloc==q.length) putloc = 0; // loop back
  25.  
  26. }
  27.  
  28. // Get a character from the queue.
  29. public char get() {
  30. if(getloc == putloc) {
  31. System.out.println(" -- Queue is empty.");
  32. return (char) 0;
  33. }
  34.  
  35. char ch = q[getloc++];
  36. if(getloc==q.length) getloc = 0; // loop back
  37. return ch;
  38. }
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement