在非递减的有序单链表中插入一个值为x的数据元素,并使单链表仍保持有序的操作

来源:互联网 发布:单片机最小系统仿真图 编辑:程序博客网 时间:2024/05/16 07:18

// 在非递减的有序单链表中插入一个值为x的数据元素,并使单链表仍保持有序的操作

public void insert(Object  x) {
    Node p = head.next;
    Node q = head;// q用来记录p的前驱结点
    //int temp;
    Object temp;
    while (p != null) {
             //temp = ((Integer) p.data).intValue();
             temp = p.data;
             if (((Comparable)temp).compareTo(x) < 0) {
                   q = p;
                   p = p.next;
             } else
                   break;
    }
    Node s = new Node(x); // 生成新结点
    s.next=p;// 将s结点插入到单链表的q结点与p结点之间
    q.next=s;
}

阅读全文
0 0
原创粉丝点击