import java.util.*; public class LinkedList { // instance variables private Node head, tail; private int size; // methods public LinkedList() { head = tail = null; size = 0; } public int size() { return size; } public boolean isEmpty() { return size == 0; } public void addHead(Object d) { Node n = new Node(d, head); head = n; size++; if (tail == null) tail = head; } public void addTail(Object d) { Node n = new Node(d, null); if (tail == null) head = tail = n; else { tail.setNext(n); tail = n; } size++; } public Object removeHead() { if (head == null) throw new RuntimeException("Empty List"); Node n = head; head = head.getNext(); if (head == null) tail = head; size--; return n.getData(); } // LinkedList testing methods: public String toString() { String ans = ""; Node n = head; ans += "(H)-->"; while (n != null) { ans += n.getData(); ans += "-->"; n = n.getNext(); } return ans + "(T)"; } public static void main(String args[]) { LinkedList l = new LinkedList(); try { Scanner s = new Scanner(System.in); while (true) { System.out.println( l + " :cmds are H T R Q: "); String cmd = s.next(); if (cmd.charAt(0) == 'Q') throw new Exception(); if (cmd.charAt(0) == 'R') l.removeHead(); else { String entry = s.next(); if (cmd.charAt(0) == 'H') l.addHead(entry); if (cmd.charAt(0) == 'T') l.addTail(entry); } } } catch (Exception e) { } } }