Advertisement
Guest User

Untitled

a guest
Jun 16th, 2019
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.11 KB | None | 0 0
  1. package com.company;
  2.  
  3. public class Main {
  4.  
  5. public static void main(String[] args) {
  6. // write your code here
  7. Stos stos = new Stos(10);
  8. stos.push(2);
  9. stos.peek();
  10. stos.push(3);
  11. stos.push(5);
  12. stos.peek();
  13. stos.pop();
  14. stos.peek();
  15. }
  16.  
  17. public static class Stos{
  18.  
  19. private int date[];
  20. public int top;
  21.  
  22. public Stos(int size){
  23. top = 0;
  24. date = new int[size];
  25. }
  26.  
  27. public void peek(){ // wyswietlenie pierwszego elementu w stosie
  28. System.out.println(date[top-1]);
  29. }
  30.  
  31. public void pop(){ // zdjecie z stosu
  32. if(top>0){
  33. top--;
  34. }
  35. else{
  36. System.out.println("Stos jest pusty");
  37. }
  38. }
  39.  
  40. public void push(int a){ // dodanie do stosu
  41. if(top+1 < date.length) {
  42. date[top] = a;
  43. top++;
  44. }
  45. else{
  46. System.out.println("Stos jest pełny");
  47. }
  48. }
  49. }
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement