/**
 * Company
 * Copyright (C) 2014-2017 All Rights Reserved.
 */
package com.cwenao.trying.linkedlist;

import java.io.Serializable;

/**
 * @author cwenao
 * @version $Id LinkedListOperation.java, v 0.1 2017-08-06 23:08 cwenao Exp $$
 */
public class LinkedListOperation<T extends Comparable> implements Serializable {

    private LinkedNode<T> head;


    public LinkedNode<T> append(T data) {

        if (head == null) {
            head = new LinkedNode<T>(data);
            head.setNext(null);

            return head;
        }
        LinkedNode<T> node = new LinkedNode<T>(data);
        node.setNext(null);

        LinkedNode<T> tmp = head;
        while (tmp.getNext() != null) {
            tmp = tmp.getNext();
        }
        tmp.setNext(node);
        return node;
    }

    public void push(T data) {
        LinkedNode<T> node = new LinkedNode<T>(data);
        node.setNext(head);
        head = node;
    }

    public void insertAfter(LinkedNode<T> preNode, T data) {
        if (preNode == null) {
            return;
        }
        LinkedNode<T> node = new LinkedNode<T>(data);
        node.setNext(preNode.getNext());
        preNode.setNext(node);
    }

    public void deleteNode(T data) {
        LinkedNode<T> tmp, preNode = null;
        tmp = head;
        while (tmp != null && tmp.getData() != data) {
            preNode = tmp;
            tmp = tmp.getNext();
        }
        if (tmp == null) {
            return;
        }
        preNode.setNext(tmp.getNext());
    }

    private void printLinkedList() {
        LinkedNode<T> tmp = head;
        while (tmp != null) {
            System.out.println("This is the linked list node data: " + tmp.getData());
            tmp = tmp.getNext();
        }
    }

    public static void main(String[] args) {
        LinkedListOperation<Integer> linkedInsertion = new LinkedListOperation<>();
        linkedInsertion.append(22);
        linkedInsertion.append(90);


        linkedInsertion.push(30);


        linkedInsertion.insertAfter(linkedInsertion.head.getNext(), 8888);
        linkedInsertion.printLinkedList();

    }

}

@@ -349,17 +349,16 @@
  head;%0A%0A
-%0A
     publ
@@ -391,17 +391,16 @@
 data) %7B%0A
-%0A
         
@@ -919,32 +919,1144 @@
  = node;%0A    %7D%0A%0A
+    /**%0A     * the size of linked list%0A     * @return%0A     */%0A    public int size() %7B%0A        return size(head);%0A    %7D%0A%0A    private int size(LinkedNode%3CT%3E node) %7B%0A        if (node == null) %7B%0A            return 0;%0A        %7D%0A        return 1 + size(node.getNext());%0A    %7D%0A%0A    /**%0A     * %E5%88%A4%E6%96%AD%E6%98%AF%E5%90%A6%E5%AD%98%E5%9C%A8%E5%BD%93%E5%89%8D%E7%BB%99%E5%87%BA%E7%9A%84data%E7%9A%84node%0A     * @param data%0A     * @return%0A     */%0A    public boolean contains(T data) %7B%0A        return contains(head, data);%0A    %7D%0A%0A    private boolean contains(LinkedNode%3CT%3E node, T data) %7B%0A        if (node == null) %7B%0A            return false;%0A        %7D%0A        if (node.getData().compareTo(data) == 0) %7B%0A            return true;%0A        %7D%0A        return contains(node.getNext(), data);%0A    %7D%0A%0A    /**%0A     * %E6%9F%A5%E6%89%BE%E5%BD%93%E5%89%8D%E7%9A%84Node%E5%B9%B6%E8%BF%94%E5%9B%9E%0A     * @param data%0A     * @return%0A     */%0A    public LinkedNode%3CT%3E search(T data) %7B%0A        return search(head, data);%0A    %7D%0A%0A    private LinkedNode%3CT%3E search(LinkedNode%3CT%3E node, T data) %7B%0A        if (node == null) %7B%0A            return null;%0A        %7D%0A        if (node.getData().compareTo(data) == 0) %7B%0A            return node;%0A        %7D%0A        return search(node.getNext(), data);%0A    %7D%0A%0A
     public void 
@@ -2287,23 +2287,25 @@
 (node);%0A
-
     %7D%0A%0A
+%0A%0A
     publ

add the size() contains() search() functions