title: Java linked list used in Stack
body: <p>To improve a little bit myself in data structures i tried to design my own Stack. To do so I decided to use a linked list to let me use undefined length stacks (and because I come from C programming). My code is:</p>
  
  <pre><code>public class Main {
  
      public static void main(String[] args) {
          Stack stk = new Stack();
          obj buf_el;
          Scanner s=new Scanner(System.in);
          boolean stop=false;
      do{
          String buffer= s.next();
          System.out.println("Created first buff");
          if(buffer.equals("exit")) stop=true;
          if(buffer.equals("pop")){
              buf_el = stk.pop();
              System.out.println("Element Popped from stack:\t\t[#]\t--&gt;\t"+buf_el.field());
          }
          else if(buffer.equals("push")){
              obj pushed = new obj(s.next());
              stk.push(pushed);
              System.out.println("Element pushed in the stack:\t\t[#]\t&lt;--\t"+pushed.field());
          }
          else if(buffer.equals("stats")) stk.stats();
          else if(buffer.equals("all")) stk.traverse();
      }
      while(stop==false);
      }
  }
  
  class obj{
      private String field = new String();
      private   obj next = null;
      private   obj back = null;
      obj (String field){
          this.field=field;
      }
  
      public obj() {
  
      }
  
      void concat(obj next){
          this.next=next;
          this.next.back=this;
      }
      void del(){
          this.next=null;
          this.back.concat(null);
      }
      obj next(){
          return this.next;
      }
      obj back(){
          return this.back;
      }
      String field(){
          return this.field;
      }
  }
  
  class Stack{
      private obj head = null;
      private obj tail = null;
      private int pops=0;
      private int pushes=0;
      void push(obj el){
          pushes++;
          if(this.head==null | this.tail==null){
              this.head = el;
              this.tail = el;
          }
          else{
              head.concat(el);
          }
      }
      obj pop(){
          if(head==null &amp; tail==null) System.out.println("The stack is empty, Operation failed.");
          else{
              pops++;
              if(this.head==this.tail){
                  System.out.println("This is the last element; the stack is empty now");
                  obj ret=new obj(this.head.field());
                  this.head=null;
                  this.tail=null;
                  return ret;
              }
              else{
                  obj ret=new obj(this.head.field());
                  this.head=this.head.back();
                  return ret;
              }
          }
          return null;
      }
      void stats(){
          System.out.println("Number of pops\t||\t"+pops);
          System.out.println("Number of pushes\t||\t"+pushes);
          System.out.println("Length of the stack\t||\t"+(pushes-pops));
      }
      void traverse(){
          if(head==null &amp; tail==null) System.out.println("The stack is empty, Operation failed");
          obj cursor=null;
          System.out.println("    TAIL");
          System.out.println("\t|");
          System.out.println("\t|");
          System.out.println("\t|");
          System.out.println("\tV");
          do {
              cursor=tail;
              System.out.println(cursor.field());
              System.out.println("\t|");
              System.out.println("\t|");
              System.out.println("\t|");
              System.out.println("\tV");
              cursor=cursor.next();
          }
          while(cursor!=null);
          System.out.println("    HEAD");
      }
  }
  </code></pre>
  
  <p><a href="https://github.com/Matagyah/MyStack/blob/differentwayofimput/Main.java" rel="nofollow noreferrer">GitHub</a></p>
  
  <p>But it has a big problem: when i push a new element it is overwritten on the previous element. Where is the error? Probably I misunderstood something related to the "by reference" practices in Java. Moreover, what methods would you use to make such a programm?</p>
  
  <p>P.S. I know that there is a stack library, but I believe it would be good to code these things to improve my knowledge of the language.</p>