Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.company;
- public class Main {
- public static void main(String[] args) {
- // write your code here
- Stos stos = new Stos(10);
- stos.push(2);
- stos.peek();
- stos.push(3);
- stos.push(5);
- stos.peek();
- stos.pop();
- stos.peek();
- }
- public static class Stos{
- private int date[];
- public int top;
- public Stos(int size){
- top = 0;
- date = new int[size];
- }
- public void peek(){ // wyswietlenie pierwszego elementu w stosie
- System.out.println(date[top-1]);
- }
- public void pop(){ // zdjecie z stosu
- if(top>0){
- top--;
- }
- else{
- System.out.println("Stos jest pusty");
- }
- }
- public void push(int a){ // dodanie do stosu
- if(top+1 < date.length) {
- date[top] = a;
- top++;
- }
- else{
- System.out.println("Stos jest pełny");
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement