Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class List<E> {
- private Node head;
- private int count;
- private Node tail;
- public boolean isEmpty() {
- return count <= 0;
- }
- public int getCount() {
- return this.count;
- }
- //Metodos
- public List() {
- this.head = null;
- this.count = 0;
- this.tail = null;
- }
- public void AddToHead(E item) {
- Node temp = new Node(item, this.head);
- if(this.count == 0) {
- this.tail = temp;
- }
- this.head = temp;
- ++this.count;
- }
- public void AddToTail(E item) {
- Node temp = new Node(item, null);
- if(this.count == 0) {
- this.head = temp;
- }
- else {
- this.tail.next = temp;
- }
- this.tail = temp;
- ++this.count;
- }
- public E RemoveFromHead() {
- if(this.count == 0) {
- throw new RuntimeException("La lista está vacia...");
- }
- E item = this.head.item;
- this.head = this.head.next;
- if(this.head == null) {
- this.tail = null;
- }
- --this.count;
- return item;
- }
- public E RemoveFromTail() {
- if(this.count == 0) {
- throw new RuntimeException("La lista está vacia...");
- }
- E item = this.tail.item;
- if(this.head.next == null) {
- this.head = this.tail = null;
- }
- else {
- Node skip = this.head;
- for( ; skip.next.next != null; skip = skip.next) { }
- this.tail = skip;
- this.tail.next = null;
- }
- --this.count;
- return item;
- }
- public void Mostrar() {
- for(Node skip = this.head; skip != null; skip = skip.next) {
- System.out.println(skip);
- }
- }
- private class Node {
- public E item;
- public Node next;
- //Getters & Setters
- public E getItem() {
- return item;
- }
- public void setItem(E item) {
- this.item = item;
- }
- public Node getNext() {
- return next;
- }
- public void setNext(Node next) {
- this.next = next;
- }
- //Metodos
- public Node() {
- this(null, null);
- }
- public Node(E item) {
- this(item, null);
- }
- public Node(E item, Node next) {
- this.item = item;
- this.next = next;
- }
- public Node(Node next) {
- this(null,next);
- }
- public String toString() {
- return this.item.toString();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement