// 在指定位置插入元素 public void insert(int i, T t) { if (i < 0 || i > N) { throw new IndexOutOfBoundsException("Index out of bounds"); } if (N == eles.length) { resize(eles.length * 2); // 动态扩容 } // 将 i 及以后的元素后移 for (int index = N; index > i; index--) { eles[index] = eles[index - 1]; } eles[i] = t; // 插入元素 N++; }
// 移除指定位置的元素 public T remove(int i) { if (i < 0 || i >= N) { throw new IndexOutOfBoundsException("Index out of bounds"); } T current = eles[i]; // 将 i 后面的元素前移 for (int index = i; index < N - 1; index++) { eles[index] = eles[index + 1]; } eles[--N] = null; // 避免对象游离 if (N > 0 && N == eles.length / 4) { resize(eles.length / 2); // 动态缩小 } return current; }
// 返回指定元素的索引,找不到返回 -1 public int index(T t) { for (int i = 0; i < N; i++) { if (eles[i].equals(t)) { return i; } } return -1; }
// 动态调整数组大小 private void resize(int newSize) { T[] temp = (T[]) new Object[newSize]; for (int i = 0; i < N; i++) { temp[i] = eles[i]; } eles = temp; } }
// 在指定位置插入元素 public void insert(int i, T t) { if (i < 0 || i > N) { throw new IndexOutOfBoundsException("Index out of bounds"); } if (N == eles.length) { resize(eles.length * 2); // 动态扩容 } // 将 i 及以后的元素后移 for (int index = N; index > i; index--) { eles[index] = eles[index - 1]; } eles[i] = t; // 插入元素 N++; }
// 移除指定位置的元素 public T remove(int i) { if (i < 0 || i >= N) { throw new IndexOutOfBoundsException("Index out of bounds"); } T current = eles[i]; // 将 i 后面的元素前移 for (int index = i; index < N - 1; index++) { eles[index] = eles[index + 1]; } eles[--N] = null; // 避免对象游离 if (N > 0 && N == eles.length / 4) { resize(eles.length / 2); // 动态缩小 } return current; }
// 返回指定元素的索引,找不到返回 -1 public int index(T t) { for (int i = 0; i < N; i++) { if (eles[i].equals(t)) { return i; } } return -1; }
// 动态调整数组大小 private void resize(int newSize) { T[] temp = (T[]) new Object[newSize]; for (int i = 0; i < N; i++) { temp[i] = eles[i]; } eles = temp; }
public static void main(String[] args) { Test<String> list = new Test<>(5); list.insert("qwq"); list.insert("qwqwq"); System.out.println(list); }
@Override public String toString() { return "Test{" + "eles=" + Arrays.toString(eles) + ", N=" + N + '}'; } }
容量的扩容实现,其实就是一个动态的扩展的方法
1 2 3 4 5 6
private void resize(int newSize) { T[] temp = (T[]) new Object[newSize]; for (int i = 0; i < N; i++) { temp[i] = eles[i]; } eles = temp;
然后是需要使用的时候直接调用这个方法就可以
链式表
逻辑上相邻,物理上并不是相邻的
是通过指针将数据联系起来的
存储的元素不一定是连续的
元素节点存放的数据元素(date)以及相邻元素的地址信息(next)
LinkedList就是基于链表实现的
更改数据快,但是查询比较慢
1 2 3 4 5 6 7 8 9 10 11 12
import org.w3c.dom.Node;
public class BigStar<T> { public T item; public Node next; public Node(T item,Node next){ this.item = item; this.next = next;
// 获取指定位置的元素 public T get(int i) { if (i < 0 || i >= N) { throw new IndexOutOfBoundsException("Index out of bounds"); } Node<T> current = head; for (int index = 0; index < i; index++) { current = current.next; } return current.item; }
// 在链表末尾插入元素 public void insert(T t) { if (head == null) { head = new Node<>(t, null); } else { Node<T> current = head; while (current.next != null) { current = current.next; } current.next = new Node<>(t, null); } N++; }
// 在指定位置插入元素 public void insert(int i, T t) { if (i < 0 || i > N) { throw new IndexOutOfBoundsException("Index out of bounds"); } if (i == 0) { head = new Node<>(t, head); } else { Node<T> current = head; for (int index = 0; index < i - 1; index++) { current = current.next; } current.next = new Node<>(t, current.next); } N++; }
// 删除指定位置的元素 public T remove(int i) { if (i < 0 || i >= N) { throw new IndexOutOfBoundsException("Index out of bounds"); } T removedItem; if (i == 0) { removedItem = head.item; head = head.next; } else { Node<T> current = head; for (int index = 0; index < i - 1; index++) { current = current.next; } removedItem = current.next.item; current.next = current.next.next; } N--; return removedItem; }
// 查找元素的索引,若不存在则返回 -1 public int index(T t) { Node<T> current = head; for (int i = 0; i < N; i++) { if (current.item.equals(t)) { return i; } current = current.next; } return -1; }
// 内部节点类 private static class Node<T> { T item; // 节点数据 Node<T> next; // 下一个节点引用
// 实现 Iterable 接口的 iterator 方法,返回一个链表的迭代器 @Override public Iterator<T> iterator() { return new LinkedListIterator(); }
// 内部迭代器类,实现 Iterator 接口 private class LinkedListIterator implements Iterator<T> { private Node<T> current = head;
// 判断是否还有下一个元素 @Override public boolean hasNext() { return current != null; }
// 获取下一个元素 @Override public T next() { if (!hasNext()) { throw new NoSuchElementException(); } T item = current.item; current = current.next; return item; }
// 移除元素操作,在链表中不常用,可以不实现 @Override public void remove() { throw new UnsupportedOperationException(); } } }
public T remove(int index) { if (index < 0 || index >= size) { throw new IndexOutOfBoundsException("Index out of bounds"); } Node<T> removedNode; if (index == 0) { removedNode = head; head = head.next; } else { Node<T> current = head; for (int i = 0; i < index - 1; i++) { current = current.next; } removedNode = current.next; current.next = removedNode.next; } size--; return removedNode.data; }
public T get(int index) { if (index < 0 || index >= size) { throw new IndexOutOfBoundsException("Index out of bounds"); } Node<T> current = head; for (int i = 0; i < index; i++) { current = current.next; } return current.data; }
public boolean isEmpty() { return size == 0; }
public int size() { return size; }
public void clear() { head = null; size = 0; }
public void printList() { Node<T> current = head; while (current != null) { System.out.print(current.data + " "); current = current.next; } System.out.println(); }
public static void main(String[] args) { SimLinked<Integer> list = new SimLinked<>(); list.add(1); list.add(2); list.add(3); list.printList(); // 输出:1 2 3
// Add element at the end of the list public void add(T data) { Node<T> newNode = new Node<>(data); if (head == null) { head = newNode; tail = newNode; } else { tail.next = newNode; newNode.prev = tail; tail = newNode; } size++; }
// Add element at a specific index public void add(int index, T data) { if (index < 0 || index > size) { throw new IndexOutOfBoundsException("Index out of bounds"); } Node<T> newNode = new Node<>(data); if (index == 0) { if (head == null) { head = newNode; tail = newNode; } else { newNode.next = head; head.prev = newNode; head = newNode; } } else if (index == size) { tail.next = newNode; newNode.prev = tail; tail = newNode; } else { Node<T> current = head; for (int i = 0; i < index; i++) { current = current.next; } newNode.next = current; newNode.prev = current.prev; current.prev.next = newNode; current.prev = newNode; } size++; }
// Remove element at a specific index public T remove(int index) { if (index < 0 || index >= size) { throw new IndexOutOfBoundsException("Index out of bounds"); } Node<T> removedNode; if (index == 0) { removedNode = head; if (head == tail) { head = null; tail = null; } else { head = head.next; head.prev = null; } } else if (index == size - 1) { removedNode = tail; tail = tail.prev; tail.next = null; } else { Node<T> current = head; for (int i = 0; i < index; i++) { current = current.next; } removedNode = current; current.prev.next = current.next; current.next.prev = current.prev; } size--; return removedNode.data; }
// Get element at a specific index public T get(int index) { if (index < 0 || index >= size) { throw new IndexOutOfBoundsException("Index out of bounds"); } Node<T> current = head; for (int i = 0; i < index; i++) { current = current.next; } return current.data; }
// Check if the list is empty public boolean isEmpty() { return size == 0; }
// Get the size of the list public int size() { return size; }
// Clear the list public void clear() { head = null; tail = null; size = 0; }
// Print the elements of the list in forward order public void printListForward() { Node<T> current = head; while (current != null) { System.out.print(current.data + " "); current = current.next; } System.out.println(); }
// Print the elements of the list in reverse order public void printListBackward() { Node<T> current = tail; while (current != null) { System.out.print(current.data + " "); current = current.prev; } System.out.println(); } }
public class Test { private int maxStack; private int[] stack; private int top = -1; public ArrayStack(int maxStack){ this.maxStack = maxStack; } public boolean isfull(){ this.top = this.maxStack -1; } public boolean isempty(){ return this.top == -1; } public void push(int val){ if(isfull()){ throw new RuntimeException("满栈"); } top++; stack[top] = val; } public int pop(){ if(isempty()){ throw new RuntimeException("空栈"); } int value = stack[top]; top--; return value; } public void list(){ if (isempty()){ throw new RuntimeException("空"); } for (int i =0;i<stack.length;i++){ System.out.println(stack[i]); } } }
public class Stack<T> { private T[] stackArray; private int maxSize; private int top;
public Stack(int size){ this.maxSize = size; this.stackArray = (T[]) new Object[maxSize];//强转类型 this.top= -1; } public void push(T value){ if (isFull()){ throw new StackOverflowError("full!"); } stackArray[++top] = value; } public T pop(){ if (isEmpty()){ throw new IllegalStateException("empty!"); } return stackArray[top--]; } public T peek(){ if (isEmpty()){ throw new IllegalStateException("empty!"); } return stackArray[top]; } public boolean isEmpty() { return top == -1; // 如果栈顶指针为 -1,则栈为空 } public boolean isFull() { return top == maxSize - 1; } public int size(){ return top + 1; }
或者是用java中的集合进行模拟栈的操作
1 2 3 4 5 6 7 8 9 10 11 12
public class Test { public static void main(String[] args) { LinkedList<String> queue = new LinkedList<>(); queue.addFirst("yi"); queue.addFirst("er"); queue.addFirst("san"); queue.addFirst("si"); queue.removeFirst(); queue.removeFirst(); System.out.println(queue); } }
public class Calculator { private static boolean isOperator(char c) { return c == '+' || c == '-' || c == '*' || c == '/'; }
private static int precedence(char op) { if (op == '+' || op == '-') return 1; if (op == '*' || op == '/') return 2; return 0; }
// 这里增加了一个操作符参数 private static int applyOperation(int a, int b, char op) { switch (op) { case '+': return a + b; case '-': return a - b; case '*': return a * b; case '/': return a / b; default: return 0; } }
public static int evaluate(String expression) { Stack<Integer> values = new Stack<>(); Stack<Character> ops = new Stack<>();
for (int i = 0; i < expression.length(); i++) { char c = expression.charAt(i);
if (c == ' ') continue;
// 如果是数字,处理数字部分 if (Character.isDigit(c)) { StringBuilder sb = new StringBuilder(); while (i < expression.length() && Character.isDigit(expression.charAt(i))) { sb.append(expression.charAt(i++)); } values.push(Integer.parseInt(sb.toString())); i--; // 调整索引位置 } // 左括号直接入栈 else if (c == '(') { ops.push(c); } // 右括号处理,直到遇到左括号 else if (c == ')') { while (ops.peek() != '(') { int b = values.pop(); int a = values.pop(); values.push(applyOperation(a, b, ops.pop())); } ops.pop(); // 去掉左括号 } // 如果是操作符 else if (isOperator(c)) { while (!ops.isEmpty() && precedence(ops.peek()) >= precedence(c)) { int b = values.pop(); int a = values.pop(); values.push(applyOperation(a, b, ops.pop())); } ops.push(c); } }
// 处理剩余的操作符 while (!ops.isEmpty()) { int b = values.pop(); int a = values.pop(); values.push(applyOperation(a, b, ops.pop())); }