您当前的位置: 首页 >  链表
  • 0浏览

    0关注

    674博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

链表的增,删,改,查实现

沙漠一只雕得儿得儿 发布时间:2016-11-15 10:56:34 ,浏览量:0

package 链表上;

import java.util.Stack;
/**
 * 初始化链表节点
 * @author buder_cp
 *
 * @param 
 */
class ListNode {
	public T value;
	public ListNode next;
	
	public ListNode(){}
	
	public ListNode(T value, ListNode next) {
		super();
		this.value = value;
		this.next = next;
	}
	
	public ListNode pre;
}

public class MiniList {
	private ListNode head = new ListNode(null, null);

	/**
	 * 数组转化成链表
	 */
	public void arrayToList(T[] array) {
		ListNode p = head;
		for (T t : array) {
			ListNode node = new ListNode(t, null);
			p.next = node;
			p = node;
		}
	}

	/**
	 * 打印链表
	 */
	public void printList() {
		ListNode p = head.next;
		while (p != null) {
			System.out.print(p.value + " ");
			p = p.next;
		}
		System.out.println();
	}

	/**
	 * 插入
	 */
	public void insert(int index, T value) {
		ListNode p = head;
		for (int i = 0; i < index; i++) {
			p = p.next;
		}
		ListNode node = new ListNode(value, null);
		node.next = p.next;
		p.next = node;
	}

	/**
	 * 删除
	 * 
	 * @param args
	 */
	public T remove(int index) {
		ListNode pre = head;
		for (int i = 0; i < index; i++) {
			pre = pre.next;
		}
		ListNode p = pre.next;
		pre.next = p.next;
		return p.value;
	}

	/**
	 * 查询
	 * 
	 * @param args
	 */
	public T get(int index) {
		ListNode p = head;
		for (int i = 0; i < index; i++) {
			p = p.next;
		}
		return p.value;
	}

	/**
	 * 修改
	 * 
	 * @param args
	 */
	public void set(int index, T value) {
		ListNode p = head;
		for (int i = 0; i             
关注
打赏
1657159701
查看更多评论
0.0406s