Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package Element;
- /**
- *
- * @author MaxSylverWolf
- */
- public class Stos extends Element implements IStos {
- private Element element = null;
- Stos(int x, Element n) {
- super(x, n);
- }
- public Element getElement() {
- return element;
- }
- @Override
- public void push(int i) {
- Element pierwszy = new Element(i,element);
- element = pierwszy;
- }
- @Override
- public int pop() {
- if(element!=null) {
- int temp = element.dane;
- element = element.next;
- return temp;
- } return -1;
- }
- @Override
- public int peek() {
- return element.getDane();
- }
- @Override
- public boolean isEmpty() {
- if(element!=null) {
- return false;
- } else return true;
- }
- @Override
- public void print(){
- if(element!=null) {
- Element pierwszy = element;
- while(pierwszy!=null) {
- System.out.println("Element stosu: "+pierwszy.getDane()+"");
- pierwszy=pierwszy.getNext();
- }
- }
- }
- @Override
- public void clear() {
- while(element!=null) {
- pop();
- }
- }
- }
- //Main
- public static void main(String[] args) {
- Stos stosy = new Stos(5,null);
- System.out.println("Wprowadzenie elementów na stos!");
- stosy.push(5);
- stosy.push(4);
- stosy.push(7);
- stosy.push(9);
- stosy.push(5);
- stosy.push(1);
- stosy.push(15);
- System.out.println("Wydrukowanie elementów stosu:");
- stosy.print();
- System.out.println("Zdejmowanie elementów ze stosu, wynik po zdjęciu elementów!");
- stosy.pop();
- stosy.pop();
- stosy.print();
- System.out.println("Czy stos jest pusty? "+stosy.isEmpty());
- System.out.println("Wartość z wierzchołka stosu to: "+stosy.peek());
- System.out.println("Usunięcie wszystkich elementów ze stosu!");
- stosy.clear();
- }
- //Element
- public class Element {
- int dane;
- Element next;
- public Element (int x, Element n)
- {
- dane=x;
- next=n;
- }
- public Element getNext()
- {
- return next;
- }
- public int getDane()
- {
- return dane;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement