package �µ�����;

public class LinkedList333<T>implements LinkedListADT3<T> {

    public LinearNode<T> head;
    public int nFangYiWen;

    public LinkedList333() {
        head = null;
        nFangYiWen = 0;
    }

    @Override
    //��ӡ����
    public String Print() {

        String result = "";
        LinearNode<T> temp = head;
        while (temp != null) {
            result += temp.getElement() + " ";
            temp = temp.next;
        }
        return result;

    }

    @Override
    //��������
    public int size() {
        return nFangYiWen;
    }

    @Override
    //ָ��λ�ò���
    public void Insert(int where, T m) {
        LinearNode<T> node = new LinearNode(m);
        LinearNode<T> temp1, temp2;

        if (where <= nFangYiWen + 1) {

            if (where == 1) {
                node.next = head;
                head = node;
            } else {
                temp1 = head;
                temp2 = head.next;
                for (int a = 1; a < where - 1; a++) {
                    temp1 = temp1.next;
                    temp2 = temp2.next;
                }
                if (temp2 != null) {
                    node.next = temp2;
                    temp1.next = node;
                } else if (temp2 == null) {
                    temp1.next = node;
                }
            }
            nFangYiWen++;
        }
    }

    @Override
    public void add(T t) {
        LinearNode<T> node = new LinearNode(t);
        LinearNode<T> temp;

        if (head == null) {
            head = node;
        } else {
            temp = head;
            while (temp.next != null) {
                temp = temp.next;
            }
            temp.next = node;
        }
        nFangYiWen++;
    }

    @Override
    //ɾ��ָ��λ��Ԫ��
    public void Delete(int where, T num) {
        LinearNode node = new LinearNode(num);
        LinearNode temp1, temp2;
        //ͷɾ��
        if ((where == 1) && (head.element.equals(num))) {
            head = head.next;
        } else {
            if (where <= nFangYiWen + 1) {
                temp1 = head;
                temp2 = head.next;
                for (int a = 1; a < where - 1; a++) {
                    temp1 = temp1.next;
                    temp2 = temp2.next;
                }
                //�м�ɾ
                if (temp2.element.equals(node.element)) {
                    if (temp2.next != null) {
                        temp1.next = temp2.next;
                    }
                    //βɾ
                    else {
                        temp1.next = null;
                    }
                } else {
                    System.out.println("ɾ������");
                }
            } else {
                System.out.println("ɾ������");
            }
        }
        nFangYiWen--;
    }


//    @Override
//    //����
//    public String Sort() {
//        String[] z = this.Print().split("\\s");
//        int[] xxx = new int[z.length];
//        for (int a = 0; a < z.length; a++) {
//            xxx[a] = Integer.parseInt(z[a]);
//        }
//        String result = "";
//        for (int i = 0; i < xxx.length - 1; i++) {
//            //��������
//            for (int j = 0; j < xxx.length - 1 - i; j++) {
//                //ÿ�α���������
//                if (xxx[j] > xxx[j + 1]) {
//                    int temp = xxx[j];
//                    xxx[j] = xxx[j + 1];
//                    xxx[j + 1] = temp;
//                }
//                }
//            String every = "";
//            for (int a = 0; a < xxx.length; a++) {
//                every += xxx[a] + " ";
//            }
//            result += "Ԫ�ص�����Ϊ:" + xxx.length + "\n" + "����Ԫ��Ϊ��" + every + "\n";
//        }
//        return result;
//    }

    public String SelectSort() {
        String result = "";
        String[] strings = this.Print().split(" ");
        int[] a = new int[nFangYiWen];

        for (int i = 0; i < a.length - 1; i++) {
            int y = Integer.parseInt(strings[i]);
            a[i] = y;
        }

        for (int i = 0; i < a.length - 1; i++) {
            for (int j = i + 1; j < a.length; j++) {
                if (a[i] < a[j]) {
                    int min = a[i];
                    a[i] = a[j];
                    a[j] = min;
                }
            }
            String all = "";
            for (int s = 0; s < a.length; s++) {
                all += a[s] + " ";
            }
            result += "Ԫ������Ϊ��" + a.length + "\n"+ "����Ԫ��Ϊ��" + all + "\n";
        }
        return result;
    }
}