Haifisch7734

Stos

Apr 3rd, 2014
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.93 KB | None | 0 0
  1. class Element {
  2.     int liczba;
  3.     Element next;
  4.  
  5.     public Element(int liczba, Element next) {
  6.         this.liczba = liczba;
  7.         this.next = next;
  8.     }
  9.  
  10.     public Element zwrocWskaznik() {
  11.         return next;
  12.     }
  13.  
  14.     public int zwrocLiczbe() {
  15.         return liczba;
  16.     }
  17. }
  18.  
  19. public class Stos {
  20.  
  21.     Element head;
  22.     int licznikStosu;
  23.  
  24.     public Stos() {
  25.         head = null;
  26.         licznikStosu = 0;
  27.     }
  28.  
  29.     public void push(int x) {
  30.         Element nowy = new Element(x, head);
  31.         head = nowy;
  32.         licznikStosu++;
  33.     }
  34.  
  35.     public int pop() {
  36.         if (head != null) {
  37.             int ret = head.zwrocLiczbe();
  38.             head = head.zwrocWskaznik();
  39.             return ret;
  40.         }
  41.         return 0xffff;
  42.     }
  43.  
  44.     public int zwrocLiczbeElementow() {
  45.         return licznikStosu;
  46.     }
  47.  
  48.     public static void main(String[] args) {
  49.         Stos head = new Stos();
  50.         head.push(1);
  51.         head.push(2);
  52.         head.push(3);
  53.         System.out.println(head.pop());
  54.         System.out.println(head.pop());
  55.         System.out.println(head.pop());
  56.     }
  57.  
  58. }
Advertisement
Add Comment
Please, Sign In to add comment