Advertisement
Guest User

Untitled

a guest
Nov 20th, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.15 KB | None | 0 0
  1. //Rokas Mikalauskas
  2.  
  3. public class MyStaticQueue implements MyQueue {
  4.  
  5. //--------------------------------------------------
  6. // Attributes
  7. //--------------------------------------------------
  8. private int items[];
  9. private int numItems;
  10. private int maxItems;
  11. //-------------------------------------------------------------------
  12. // Basic Operation --> Check if myQueue is empty: myCreateEmpty
  13. //-------------------------------------------------------------------
  14. public MyStaticQueue(int m){
  15. items = new int[m];
  16. this.numItems = 0;
  17. this.maxItems = m;
  18. }
  19.  
  20. //-------------------------------------------------------------------
  21. // Basic Operation --> Check if MyQueue is empty: isEmpty
  22. //-------------------------------------------------------------------
  23. public boolean isEmpty(){
  24. boolean isEmpty = true;
  25. if(numItems <= 0) {//add this?
  26. return isEmpty;
  27. }else {
  28. isEmpty = false;
  29. return isEmpty;
  30. }
  31. }
  32.  
  33. //-------------------------------------------------------------------
  34. // Basic Operation (Partial) --> Get first element from front of MyQueue: first
  35. //-------------------------------------------------------------------
  36. public int first(){
  37. if(isEmpty()) {
  38. System.out.println("Its' empty");
  39. return -1;
  40. }
  41. return items[0];
  42. }
  43.  
  44. //-------------------------------------------------------------------
  45. // Basic Operation (Partial) --> Add element to back of MyQueue: add
  46. //-------------------------------------------------------------------
  47. public void add(int element){
  48. if(numItems < maxItems) {
  49. items[numItems] = element;
  50. numItems++;
  51. }else {
  52. System.out.println("Can't add, it's full");
  53. }
  54. }
  55.  
  56. //-------------------------------------------------------------------
  57. // Basic Operation (Partial) --> Remove element from front of MyQueue: remove
  58. //-------------------------------------------------------------------
  59. public void remove(){
  60. if(numItems > 0) {
  61. int i = 0;
  62. for(int x = 0; x < items.length-1; x++) {
  63. int temp = items[i+1];
  64. items[i] = temp;
  65. i++;
  66. }
  67. numItems--;
  68. }else {
  69. System.out.println("Can't remove, it's empty");
  70. }
  71. }
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement