Facebook
From Preslav, 5 Months ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 200
  1. public class Stack {
  2.  private Node first;
  3.  private int num;
  4.  public Stack(){
  5.   this.first=null;
  6.   num=0;
  7.  }
  8.  public Stack(int v,String ime,double us){
  9.   first = new Node(ime,us,v);
  10.   num=1;
  11.  }
  12.  public void push_front(int v,String ime,double us){
  13.   if(this.first==null){
  14.    this.first = new Node(ime,us,v);
  15.    num++;
  16.    return;
  17.   }
  18.   first = this.first.push_front(ime,us,v);
  19.   num++;
  20.  }
  21.  public boolean isEmpty(){
  22.   if(this.first==null)return true;
  23.   return false;
  24.  }
  25.  public void pop_front(){
  26.   if(isEmpty())throw new NullPointerException();
  27.   this.first = this.first.pop_front();
  28.   num--;
  29.  }
  30.  public void pop_front1(){
  31.   if(isEmpty())throw new NullPointerException();
  32.   this.first = this.first.getLink();
  33.   num--;
  34.  }
  35.  public void push_front1(int v,String ime,double us){
  36.   Node temp = new Node(ime,us,v);
  37.   temp.setLink(first);
  38.   first = temp;
  39.   num++;
  40.  }
  41.  public String toString(){
  42.   String s="{ ";
  43.   Node t =this.first;
  44.   while(t!=null){
  45.    s+=t.toString()+" \n";
  46.    t=t.getLink();
  47.   }
  48.   s+="}";
  49.   return s;
  50.  }
  51. }
  52.  
  53.  
  54.  
  55.  
  56.  
  57.  
  58.  
  59.  
  60. public class Node {
  61.  private String ime;
  62.  private double uspeh;
  63.  private int godini;
  64.  private Node link;
  65.  
  66.  public Node(String ime,double uspeh ,int v){
  67.   this.ime = ime;
  68.   this.uspeh = uspeh;
  69.   godini=v;
  70.   link=null;
  71.  }
  72.  private Node(String ime,double uspeh,int v,Node n){
  73.   this.ime = ime;
  74.   this.uspeh = uspeh;
  75.   godini=v;
  76.   link=n;
  77.  }
  78.  
  79.  public String getIme() {
  80.   return ime;
  81.  }
  82.  public void setIme(String ime) {
  83.   this.ime = ime;
  84.  }
  85.  public double getUspeh() {
  86.   return uspeh;
  87.  }
  88.  public void setUspeh(double uspeh) {
  89.   this.uspeh = uspeh;
  90.  }
  91.  public int getGodini() {
  92.   return godini;
  93.  }
  94.  public void setGodini(int godini) {
  95.   this.godini = godini;
  96.  }
  97.  public Node push_front(String ime,double us,int v){
  98.   return new Node(ime,us,v,this);
  99.  }
  100.  @Override
  101.  public String toString() {
  102.   return "Node [ime=" + ime + ", uspeh=" + uspeh + ", godini=" + godini + ", link=" + link + "]";
  103.  }
  104.  public Node pop_front(){
  105.   return link;
  106.  }
  107.  public Node getLink() {
  108.   return link;
  109.  }
  110.  public void setLink(Node link) {
  111.   this.link = link;
  112.  }
  113.  
  114. }
  115.  
  116.  
  117.  
  118.  
  119.  
  120. public static void main(String[] args) {
  121.   Stack kys = new Stack(2,"gosho",3.50);
  122.   kys.push_front(41, "simeon", 5.43);
  123.   kys.push_front(6, "nikola", 4.62);
  124.   System.out.println(kys.toString());
  125.  }