单链表和双链表


1、单链表

一、定义自己的单链表:

public interface MyList {
	// 获取链表的长度
	public int size();

	// 添加元素
	public void add(String value);

	// 得到指定位置的元素
	public Node get(int index);

	// 在指定位置添加元素
	public void add(String value, int index);

	// 移除指定位置的元素
	public void remove(int index);
}

二、定义节点:

public class Node {
  
	public String date ;
	public Node next;
	
	/**
	 * 构造方法得到数据和下一个节点
	 * @param date
	 * @param next
	 */
	public Node(String date, Node next) {
		super();
		this.date = date;
		this.next = next;
	}
	/**
	 * 得到数据和下一个节点
	 * @return
	 */
	public String getDate() {
		return date;
	}
	public void setDate(String date) {
		this.date = date;
	}
	public Node getNext() {
		return next;
	}
	public void setNext(Node next) {
		this.next = next;
	}
	
	
	
}


三、实现自己的单链表

public class MyLinkedList implements MyList {

	public Node head;

	/**
	 * 获取链表的长度
	 */
	@Override
	public int size() {
		int count = 0;
		if (head == null) {
			return count;
		} else {
			count++;
			Node node = head.getNext();
			while (node != null) {
				count++;
				node = node.getNext();
			}
		}
		return count;
	}

	/**
	 * 得到指定位置上的元素
	 */
	@Override
	public Node get(int index) {
		// 如果索引小于零的时候就输出异常
		if (index < 0) {
			try {
				throw new Exception("索引不能为负数" + index);
			} catch (Exception e) {
				e.printStackTrace();
				return null;
			}
		}
		// 定义一个计数器
		int count = 0;
		// 当索引值等于零的时候,直接返回头节点;实现下标从零开始
		if (count == index) {
			return head;
		}
		// 判断下标是否越界   长度是从1开始;索引是从0开始;索引是客户输入的
		if (index < size()) {
			Node node = head.getNext();
			while (node != null) {
				count++;
				if (count == index) {
					return node;
				}
				node = node.getNext();
			}
		} else {
			try {
				throw new Exception("下标越界");
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return null;
	}

	/**
	 * 在链表上添加元素
	 */
	@Override
	public void add(String value) {
		Node node = new Node(value, null);
		if (size() == 0) {
			head = node;
		} else {
			Node node1 = get(size() - 1);
			node1.setNext(node);
		}

	}

	/**
	 * 在指定位置添加元素
	 * 
	 */
	@Override
	public void add(String value, int index) {
		// 链表添加节点
				// 创建一个新的节点
				Node node = new Node(value, null);
				if(head==null){
					head=node;
					return;
				}
				//先拿到当前索引位置的节点
				if(index==0){
					node.setNext(head);
					head = node;
					return;
				}
				Node nodeCurrent = get(index);
				Node nodeParent = get(index-1);
				nodeParent.setNext(node);
				node.setNext(nodeCurrent);
	}

	/**
	 * 移除指定位置的元素
	 */
	@Override
	public void remove(int index) {
	int count = size();
<span style="white-space:pre">		</span>Node temp = get(index);
<span style="white-space:pre">		</span>if (temp != null) {
<span style="white-space:pre">			</span>temp = head.getNext();
<span style="white-space:pre">			</span>Node next = temp.getNext();
<span style="white-space:pre">			</span>head.setNext(next);
<span style="white-space:pre">			</span>count--;
<span style="white-space:pre">		</span>}

	}

}
四、测试自己的链表:
public class Test {
   
	public static void main(String[] args) {
		MyLinkedList list = new MyLinkedList();
		list.add("节点1");
		list.add("节点2");
		list.add("节点3");
		list.add("节点4");
		System.out.println(list.size());
		System.out.println(list.get(0).getDate());
		System.out.println(list.get(1).getDate());
		System.out.println(list.get(2).getDate());
		System.out.println(list.get(3).getDate());
                list.remove(1);
<span style="white-space:pre">		</span>System.out.println(list.size());
<span style="white-space:pre">		</span>System.out.println(list.get(0).getDate());
<span style="white-space:pre">		</span>System.out.println(list.get(1).getDate());
<span style="white-space:pre">		</span>System.out.println(list.get(2).getDate());
	}
}

2、双链表的实现
public class SxtLinkedList {

	public Note first;
	public Note last;
	public int size;

	
	//删除指定位置的结点
	public void remove(int index) {

		Note temp = Note(index);
		if (temp != null) {
			Note up = temp.previous;
			Note down = temp.next;
			up.next = down;
			down.previous = up;
			size--;
		}     
	}
   //得到一个结点
	private Note Note(int index) {
		Note temp = null;
		if (first != null) {
			temp = first;
			for (int i = 0; i < index; i++) {
				temp = temp.next;
			}
		}
		return temp;
	}
    //增加元素
	public void add(Object obj) {
		Note n = new Note();
		if (first == null) {
			n.setPrevious(null);
			n.setObj(obj);
			n.setNext(null);

			first = n;
			last = n;
		} else {
			n.setPrevious(last);
			n.setObj(obj);
			n.setNext(null);
			last.setNext(n);

			last = n;
		}
		size++;
	}

	public int size() {
		return size;
	}

	public static void main(String[] args) {
		SxtLinkedList list = new SxtLinkedList();
		list.add("aaa");
		list.add("bbb");
		list.add("ccc");
		System.out.println(list.size());
		list.remove(1);
		System.out.println(list.size());
	}
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值