栈 队列 链表
栈:先进后出
/** * 栈 * * @author sun * */ public class MyStack { private int maxSize; private char[] data; private int top; public MyStack(int maxSize) { this.maxSize = maxSize; this.data = new char[maxSize]; this.top = -1; } public void push(char c) { data[++top] = c; } public char pop() { return data[top--]; } public boolean isEmpty() { return top == -1; } }
队列:先进先出
public class Queue { // 数组 private long[] arr; // 最大空间 private int maxSize; // 有效元素大小 private int elems; // 队头 private int font; // 队尾 private int end; public Queue(int maxSize) { this.maxSize = maxSize; arr = new long[maxSize]; elems = 0; font = 0; end = -1; } // 插入数据 public void insert(long value) { if(end == maxSize - 1) { end = -1; } arr[++end] = value; elems++; } // 移除数据 public long remove() { long tmp = arr[font++]; if(font == maxSize) { font = 0; } elems--; return tmp; } // 是否为空 public boolean isEmpty() { return (elems == 0); } // 是否满了 public boolean isFull() { return (elems == maxSize); } // 返回有效元素大小 public int size() { return elems; } }
循环队列:当我们一直使用对头front,对尾end进行添加,删除元素时,会发现数组很容易满,这时,我们将对头front,对尾end再指向初始化位置。
public class MyQueue { // 数组 private long[] arr; // 最大空间 private int maxSize; // 有效元素大小 private int elems; // 队头 private int font; // 队尾 private int end; public MyQueue(int maxSize) { this.maxSize = maxSize; arr = new long[maxSize]; elems = 0; font = 0; end = -1; } // 插入数据 public void insert(long value) { if(end == maxSize - 1) { end = -1; } arr[++end] = value; elems++; } // 移除数据 public long remove() { long tmp = arr[font++]; if(font == maxSize) { font = 0; } elems--; return tmp; } // 是否为空 public boolean isEmpty() { return (elems == 0); } // 是否满了 public boolean isFull() { return (elems == maxSize); } // 返回有效元素大小 public int size() { return elems; } }
优先级队列
public class PriorityQueue { // 数组 private long[] arr; // 最大空间 private int maxSize; // 有效元素大小 private int elems; public PriorityQueue(int maxSize) { this.maxSize = maxSize; arr = new long[maxSize]; elems = 0; } // 插入数据 public void insert(long value) { int i; for (i = 0; i < elems; i++) { if(value < arr[i]) { break; } } for(int j = elems; j > i;j--){ arr[j] = arr[j - 1]; } arr[i] = value; elems++; } // 移除数据 public long remove() { long value = arr[elems - 1]; elems--; return value; } // 是否为空 public boolean isEmpty() { return (elems == 0); } // 是否满了 public boolean isFull() { return (elems == maxSize); } // 返回有效元素大小 public int size() { return elems; } }
链表
/** * 链结点 * * @author sun * */ public class Link { private Long data; private Link next; public Link(Long data) { this.data = data; } public Long getData() { return data; } public void setData(Long data) { this.data = data; } public Link getNext() { return next; } public void setNext(Link next) { this.next = next; } }
public class LinkList { private Link first; public LinkList() { first = null; } public void insert(long value) { Link lnk = new Link(value); if (first == null)// 是否只有一个链结点 { first = lnk; } else { lnk.setNext(first); first = lnk; } } public void displayList() { Link current = first; while (current != null) { System.out.print(current.getData() + " "); current = current.getNext(); } } /** * 关键字找链结点 * * @param key * @return */ public Link find(long key) { Link current = first; while (current.getData() != key) { if (current.getNext() == null) { return null; } else { current = current.getNext(); } } return current; } /** * 插入结点到指定位置 * * @param value * @param pos */ public void insert(long value, int pos) { if (pos == 0) { insert(value); } else { Link current = first; for (int i = 0; i < pos - 1; i++) { current = current.getNext(); } Link lnk = new Link(value); lnk.setNext(current.getNext()); current.setNext(lnk); } } /** * 删除首结点 * * @return 该结点值 */ public long deleteFirst() { Link temp = first; first = first.getNext(); return temp.getData(); } /** * 删除结点 * * @param key */ public void delete(long key) { Link current = first; Link previous = first; while (current.getData() != key) { if (current.getNext() == null) { return; } else { previous = current; current = current.getNext(); } } if (current == first) { first = first.getNext(); } else { previous.setNext(current.getNext()); } } /** * 链表是否为空 * * @return */ public boolean isEmpty() { return (first == null); } }