Advertisement
Master_Tiroman

Цикличная очередь

Aug 26th, 2019
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.65 KB | None | 0 0
  1. package com.example.lib;
  2.  
  3. import java.util.Scanner;
  4.  
  5. class Queue{
  6. char q[];
  7. int putloc,getloc;
  8.  
  9. Queue(int size){
  10. q=new char[size];
  11. putloc=getloc=0;
  12. }
  13.  
  14. void put(char ch){
  15. if(putloc==q.length){
  16. System.out.println("- Очередь заполнена!");
  17. return;
  18. }
  19. q[putloc++]=ch;
  20. }
  21.  
  22. char get(){
  23. if(getloc==putloc){
  24. System.out.println("- Очередь пуста!");
  25. getloc=putloc=0;
  26. return (char) 0 ;
  27. }
  28. return q[getloc++];
  29. }
  30. }
  31.  
  32.  
  33.  
  34.  
  35. class QDemo {
  36. public static void main(String args[])
  37. throws java.io.IOException {
  38. Queue bigQ=new Queue(100);
  39. Queue minQ=new Queue(5);
  40. int i;
  41. char ch;
  42.  
  43. System.out.println("Использование bigQ для размещения букв алфавита: ");
  44. for(i=0;i<26;i++){
  45. bigQ.put((char)('A'+i));
  46. }
  47. System.out.print("Демонстрация очереди bigQ: ");
  48. for(i=0;i<27;i++){
  49. ch=bigQ.get();
  50. System.out.print(ch);
  51. }System.out.println();
  52.  
  53. System.out.println("Использование minQ для демонстрации ошибок: ");
  54. for(i=0;i<6;i++){
  55. System.out.println("Попытка сохранения "+(char)('Z'-i));
  56. minQ.put((char)('Z'-i));
  57. }
  58. System.out.print("Демонстрация очереди minQ: ");
  59. for(i=0;i<6;i++){
  60. ch=minQ.get();
  61. System.out.print(ch);
  62. }
  63.  
  64. }
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement