论坛首页 入门技术论坛

Java数据结构和算法--链表

浏览 10373 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2007-10-03  
(1)简单链表
package ChapterFive;

class Link<E> {

	public E data;

	public Link<E> next;

	public Link(E data) {
		this.data = data;
	}
}

class LinkList<E> {

	public Link<E> first;
	//链表中数据项的个数
	public int size;

	public LinkList() {
		first = null;
		size = 0;
	}
	//在表头插入新的数据
	public void insertFirst(E value) {
		Link<E> link = new Link<E>(value);
		link.next = first;
		first = link;
		size++;
	}
	//判断链表是否为空
	public boolean isEmpty() {
		return size == 0;
	}
	//删除表头
	public Link<E> deleteFirst() {
		Link<E> temp = first;
		first = first.next;
		size--;
		return temp;
	}
	//输出链表中的所有数据
	public void display() {
		Link<E> curr = first;
		while (curr != null) {
			System.out.print(curr.data + " ");
			curr = curr.next;
		}
		System.out.println();
	}
	//返回链表中数据项的个数
	public int size() {
		return size;
	}
	//获取从头至尾的第i个数据项
	public Link<E> get(int i) {
		if (i > size() - 1 || i < 0)
			try {
				throw new IndexOutOfBoundsException();
			} catch (Exception e) {
				e.printStackTrace();
			}
		Link<E> curr = first;
		for (int n = 0; n < size(); n++) {
			if (n == i)
				return curr;
			else
				curr = curr.next;
		}
		return null;
	}
	//输出从头至尾的第i个数据项
	public void remove(int i) {
		if (i == 0)
			deleteFirst();
		else if (i == size() - 1)
			get(i - 1).next = null;
		else {
			get(i - 1).next = get(i + 1);
		}
		size--;
	}
}

public class Link_list {
	public static void main(String[] args) {
		LinkList<Long> ll = new LinkList<Long>();
		for (int i = 0; i < 10; i++) {
			Long value = (long) (Math.random() * 100);
			ll.insertFirst(value);
		}
		ll.display();
		while (!ll.isEmpty()) {
			ll.deleteFirst();
			ll.display();
		}
		System.out.println("Ok");
	}
}

(2)链栈
package ChapterFive;

class LinkStack<E> {

	LinkList<E> linkList;

	int size;

	public LinkStack() {
		size = 0;
		linkList = new LinkList<E>();
	}
	//入栈
	public void push(E value) {
		linkList.insertFirst(value);
		size++;
	}
	//出栈
	public Link<E> pop() {
		size--;
		return linkList.deleteFirst();
	}
	//返回栈顶元素
	public Link<E> top() {
		return linkList.first;
	}
	//判断栈是否为空
	public boolean isEmpty() {
		return size == 0;
	}
	//显示栈中的全部数据
	public void display() {
		linkList.display();
	}
}

public class Link_stack {
	public static void main(String[] args) {
		LinkStack<Long> ls = new LinkStack<Long>();
		for (int i = 0; i < 10; i++) {
			Long value = new Long((long) (Math.random() * 100));
			ls.push(value);
		}
		while (!ls.isEmpty()) {
			ls.pop();
			ls.display();
		}
		System.out.println("Ok");
	}
}

(3)有序表
package ChapterFive;

class SortedLink {

	public Link<Long> first;

	int size;

	public SortedLink() {
		first = null;
		size = 0;
	}
	//向有序链表中插入数据
	public void insert(long value) {
		Link<Long> newLink = new Link<Long>(value);
		Link<Long> previous = null;
		Link<Long> curr = first;
		while (curr != null && (value > curr.data)) {
			previous = curr;
			curr = curr.next;
		}
		if (previous == null)// 链表为空(在表头插入)
			first = newLink;
		else
			previous.next = newLink;//插入新的节点
		newLink.next = curr;
		size++;
	}
	//删除第一个节点
	public Link<Long> remove() {
		Link<Long> temp = first;
		first = first.next;
		size--;
		return temp;
	}
	//判断链表是否为空
	public boolean isEmpty() {
		return size == 0;
	}
	//输出链表的所有数据
	public void display() {
		Link<Long> curr = first;
		while (curr != null) {
			System.out.print(curr.data + " ");
			curr = curr.next;
		}
		System.out.println();
	}
}

public class SortedLinkApp {
	public static void main(String[] args) {
		SortedLink sl = new SortedLink();
		for (int i = 0; i < 10; i++) {
			sl.insert((long) (Math.random() * 100));
		}
		while (!sl.isEmpty()) {
			sl.remove();
			sl.display();
		}
	}
}

(4)双向链表
package ChapterFive;

class DoubleLink<E> {

	public Link<E> first;

	public Link<E> last;

	int size;

	@SuppressWarnings("hiding")
	class Link<E> {
		public E data;

		public Link<E> next;// 链表的下一项

		public Link<E> previous;// 链表的前一项

		public Link(E value) {
			this.data = value;
		}
	}

	public DoubleLink() {
		first = null;
		last = null;
		size = 0;
	}

	// 在链表的首部插入一项
	public void insertFirst(E value) {
		Link<E> newLink = new Link<E>(value);
		if (isEmpty())// 如果链表为空则first == last
			last = newLink;
		else
			first.previous = newLink;// 确定原first与newLink的前后关系
		newLink.next = first;
		first = newLink;// 设置新的first值
		size++;
	}

	// 在链表的尾部插入一项
	public void insertLast(E value) {
		Link<E> newLink = new Link<E>(value);
		if (isEmpty())// 如果链表为空则last == first
			first = newLink;
		else {
			last.next = newLink;// 确定原last与newLink的前后关系
			newLink.previous = last;
		}
		last = newLink;// 设置新的last值
		size++;
	}

	// 删除双向链表的表头
	public Link<E> deleteFirst() {
		Link<E> temp = first;
		if (first.next == null)// 链表中只有一项数据
			last = null;
		else
			first.next.previous = null;// 销毁原链表的头部
		first = first.next;
		size--;
		return temp;
	}

	// 删除链表的最后一项
	public Link<E> deleteLast() {
		Link<E> temp = last;
		if (first.next == null)// 链表中只有一项数据
			first = null;
		else
			last.previous.next = null;// 销毁原链表的尾部
		last = last.previous;
		size--;
		return temp;
	}

	// 判断链表是否为空
	public boolean isEmpty() {
		return size == 0;
	}

	// 输出链表中的所有数据项
	public void display() {
		Link<E> curr = first;
		while (curr != null) {
			System.out.print(curr.data + " ");
			curr = curr.next;
		}
		System.out.println();
	}
}

public class DoubleLinkApp {
	public static void main(String[] args) {
		DoubleLink<Integer> dl = new DoubleLink<Integer>();
		for (int i = 0; i < 5; i++) {
			dl.insertFirst((int) (Math.random() * 100));
		}
		for (int i = 0; i < 5; i++) {
			dl.insertLast((int) (Math.random() * 100));
		}
		dl.display();
		while (!dl.isEmpty()) {
			dl.deleteFirst();
			dl.deleteLast();
			dl.display();
		}
		System.out.println("Ok");
	}
}
论坛首页 入门技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics