【数据结构与算法】java链表操作

链表操作代码量少但是比较容易出错,是比较适合面试的地方。

  • 代码实现

/**
 * 源码名称:MyLinkList.java 
 * 日期:2014-09-05
 * 程序功能:java链表操作 
 * 版权:CopyRight@A2BGeek 
 * 作者:A2BGeek
 */
import java.util.Stack;

public class MyLinkList {
	class LinkNode {
		private T mValue;
		private LinkNode mNext;

		public LinkNode(T value) {
			// TODO Auto-generated constructor stub
			mValue = value;
		}

		public void setValue(T value) {
			mValue = value;
		}

		public T getValue() {
			return mValue;
		}

		public void setNext(LinkNode next) {
			mNext = next;
		}

		public LinkNode getNext() {
			return mNext;
		}
	}

	// head node
	LinkNode mHead = null;
	int mSize;

	// add
	public void add(int value) {
		LinkNode newNode = new LinkNode(value);
		newNode.setNext(null);
		if (mHead == null) {
			mHead = newNode;
		} else {
			LinkNode tmp = mHead;
			while (tmp.getNext() != null) {
				tmp = tmp.getNext();
			}
			tmp.setNext(newNode);
		}
		mSize++;
	}

	// insert
	public void insert(int index, int value) {
		LinkNode newNode = new LinkNode(value);
		if (mHead == null) {
			newNode.setNext(null);
			mHead = newNode;
			mSize++;
		} else {
			if (index > mSize - 1) {
				System.out.println("index out of bound");
			} else {
				int j = 0;
				LinkNode tmp = mHead;
				while (j < index) {
					tmp = tmp.getNext();
					j++;
				}
				newNode.setNext(tmp.getNext());
				tmp.setNext(newNode);
				mSize++;
			}
		}
	}

	// delete
	public void deleteByIndex(int index) {
		if (mHead == null) {
			System.out.println("linklist empty");
		} else {
			if (index > mSize - 1) {
				System.out.println("index out of bound");
			} else if (index == 0) {
				mHead = mHead.getNext();
			} else {
				int j = 0;
				LinkNode pretmp = mHead;
				while (j < index - 1) {
					pretmp = pretmp.getNext();
					j++;
				}
				pretmp.setNext(pretmp.getNext().getNext());
			}
		}
	}

	public void deleteByValue(int value) {
		if (mHead == null) {
			System.out.println("linklist empty");
		} else {
			LinkNode pretmp = null;
			LinkNode tmp = mHead;
			while (tmp != null) {
				if (tmp.getValue() != value) {
					pretmp = tmp;
					tmp = tmp.getNext();
				} else {
					break;
				}
			}
			if (tmp == null) {
				System.out.println("not found");
			} else if (pretmp == null) {
				mHead = tmp.getNext();
			} else {
				pretmp.setNext(tmp.getNext());
			}
		}

	}

	// reverse
	public void reverse() {
		LinkNode tmp = mHead;
		LinkNode pretmp = null;
		LinkNode nexttmp = null;
		while (tmp != null) {
			nexttmp = tmp.getNext();
			tmp.setNext(pretmp);
			pretmp = tmp;
			tmp = nexttmp;
		}
		mHead = pretmp;
	}

	// print_recursive
	public void printR(LinkNode start) {
		if (start == null) {
			return;
		} else {
			System.out.print(start.getValue() + " ");
			printR(start.getNext());
		}
	}

	// print_norecursive
	public void printNor(LinkNode start) {
		if (start == null) {
			System.out.println("linklist empty");
		} else {
			LinkNode tmp = start;
			while (tmp != null) {
				System.out.print(tmp.getValue() + " ");
				tmp = tmp.getNext();
			}
		}
	}

	// printreverse_recursive
	public void printRR(LinkNode start) {
		if (start == null) {
			return;
		} else {
			printRR(start.getNext());
			System.out.print(start.getValue() + " ");
		}
	}

	// printreverse_norecursive
	public void printRNor(LinkNode start) {
		if (start == null) {
			System.out.println("linklist empty");
		} else {
			Stack> stack = new Stack>();
			LinkNode tmp = start;
			while (tmp != null) {
				stack.push(tmp);
				tmp = tmp.getNext();
			}
			while (!stack.isEmpty()) {
				System.out.print(stack.pop().getValue() + " ");
			}
		}
	}

	public static void main(String[] args) {
		MyLinkList myLinkList = new MyLinkList();
		myLinkList.add(1);
		myLinkList.add(2);
		myLinkList.add(3);
		myLinkList.add(4);
		myLinkList.add(5);
		myLinkList.insert(3, 6);
		System.out.println("linklist size is " + myLinkList.mSize);
		myLinkList.printR(myLinkList.mHead);
		System.out.println();
		myLinkList.deleteByIndex(0);
		myLinkList.printNor(myLinkList.mHead);
		System.out.println();
		// myLinkList.reverse();
		// myLinkList.printRR(myLinkList.mHead);
		// System.out.println();
		// myLinkList.printRNor(myLinkList.mHead);
	}
}


你可能感兴趣的:(数据结构与算法)