题目一:顺序表_删除x~y间值 问题描述:已知线性表采用顺序存储结构SeqList,编写算法删除值在x~y之间的所有结点:
SeqDelTest.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 package top.kfufys.demo01;public class SeqDelTest { public static void main (String[] args) { top.kfufys.demo01.SeqList list = new SeqList (10 ); list.add(1 ); list.add(5 ); list.add(10 ); list.add(15 ); list.add(20 ); list.add(25 ); System.out.println("Original list: " + list); int x = 10 ; int y = 20 ; list.removeInRange(x, y); System.out.println("List after removing elements in range " + x + " to " + y + ": " + list); } }
SeqList.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 package top.kfufys.demo01;public class SeqList { private int [] data; private int size; public SeqList (int capacity) { this .data = new int [capacity]; this .size = 0 ; } public void add (int value) { if (size == data.length) { resize(); } data[size++] = value; } public void removeInRange (int x, int y) { if (x > y) { throw new IllegalArgumentException ("x should not be greater than y" ); } int newSize = 0 ; for (int i = 0 ; i < size; i++) { if (data[i] < x || data[i] > y) { data[newSize++] = data[i]; } } size = newSize; } public int size () { return size; } public int get (int index) { if (index < 0 || index >= size) { throw new IndexOutOfBoundsException ("Index out of bounds" ); } return data[index]; } private void resize () { int [] newData = new int [data.length * 2 ]; System.arraycopy(data, 0 , newData, 0 , size); data = newData; } @Override public String toString () { StringBuilder sb = new StringBuilder (); sb.append("[" ); for (int i = 0 ; i < size; i++) { sb.append(data[i]); if (i < size - 1 ) { sb.append(", " ); } } sb.append("]" ); return sb.toString(); } }
运行结果展示:
题目二:顺序栈实现_进制转换 问题描述:已知栈使用顺序存储结构Seqstack,编写算法conversion实现进制转换:
ConversionTest.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 package top.kfufys.demo02;public class ConversionTest { public static void main (String[] args) { int number = 228 ; int base = 2 ; System.out.println("十进制 " + number + " 转换为 " + base + " 进制: " + Conversion.conversion(number, base)); base = 8 ; System.out.println("十进制 " + number + " 转换为 " + base + " 进制: " + Conversion.conversion(number, base)); base = 16 ; System.out.println("十进制 " + number + " 转换为 " + base + " 进制: " + Conversion.conversion(number, base)); } }
Conversion.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 package top.kfufys.demo02;public class Conversion { public static String conversion (int number, int base) { if (base < 2 || base > 16 ) { throw new IllegalArgumentException ("进制必须在2到16之间" ); } SeqStack<Character> stack = new SeqStack <>(); String digits = "0123456789ABCDEF" ; do { int remainder = number % base; stack.push(digits.charAt(remainder)); number /= base; } while (number > 0 ); StringBuilder result = new StringBuilder (); while (!stack.isEmpty()) { result.append(stack.pop()); } return result.toString(); } }
SeqStack.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 package top.kfufys.demo02;import java.util.EmptyStackException;public class SeqStack <T> { private static final int DEFAULT_CAPACITY = 10 ; private T[] elements; private int top; @SuppressWarnings("unchecked") public SeqStack () { elements = (T[]) new Object [DEFAULT_CAPACITY]; top = -1 ; } public boolean isEmpty () { return top == -1 ; } public void push (T item) { if (top == elements.length - 1 ) { resize(elements.length * 2 ); } elements[++top] = item; } public T pop () { if (isEmpty()) { throw new EmptyStackException (); } T item = elements[top]; elements[top--] = null ; if (top > 0 && top == elements.length / 4 ) { resize(elements.length / 2 ); } return item; } public T peek () { if (isEmpty()) { throw new EmptyStackException (); } return elements[top]; } @SuppressWarnings("unchecked") private void resize (int newCapacity) { T[] newElements = (T[]) new Object [newCapacity]; System.arraycopy(elements, 0 , newElements, 0 , top + 1 ); elements = newElements; } }
运行结果展示:
题目三:顺序表_删除最小值结点 问题描述:已知线性表采用顺序存储结构SeqList,编写算法,删除值最小的结点:
TestSeqList.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 package top.kfufys.demo03;public class TestSeqList { public static void main (String[] args) { SeqList seqList = new SeqList (); seqList.add(5 ); seqList.add(3 ); seqList.add(8 ); seqList.add(1 ); seqList.add(7 ); System.out.println("删除最小元素之前:" ); seqList.printList(); seqList.removeMin(); System.out.println("删除最小元素之后:" ); seqList.printList(); } }
SeqList.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 package top.kfufys.demo03;public class SeqList { private int [] data; private int size; public SeqList () { this .data = new int [10 ]; this .size = 0 ; } public void add (int value) { if (size == data.length) { resize(); } data[size++] = value; } public void removeInRange (int x, int y) { if (x > y) { throw new IllegalArgumentException ("x should not be greater than y" ); } int newSize = 0 ; for (int i = 0 ; i < size; i++) { if (data[i] < x || data[i] > y) { data[newSize++] = data[i]; } } size = newSize; } public int size () { return size; } public int get (int index) { if (index < 0 || index >= size) { throw new IndexOutOfBoundsException ("Index out of bounds" ); } return data[index]; } private void resize () { int [] newData = new int [data.length * 2 ]; System.arraycopy(data, 0 , newData, 0 , size); data = newData; } @Override public String toString () { StringBuilder sb = new StringBuilder (); sb.append("[" ); for (int i = 0 ; i < size; i++) { sb.append(data[i]); if (i < size - 1 ) { sb.append(", " ); } } sb.append("]" ); return sb.toString(); } public void removeMin () { if (size == 0 ) { return ; } int minIndex = 0 ; int minValue = data[0 ]; for (int i = 1 ; i < size; i++) { if (data[i] < minValue) { minValue = data[i]; minIndex = i; } } remove(minIndex); } public void remove (int index) { if (index < 0 || index >= size) { throw new IndexOutOfBoundsException ("索引: " + index + ", 大小: " + size); } for (int i = index; i < size - 1 ; i++) { data[i] = data[i + 1 ]; } data[--size] = 0 ; } public void printList () { System.out.println(this .toString()); } }
运行结果展示:
题目四:稀疏矩阵转置 问题描述:编写算法TransMatrix实现稀疏矩阵的转置:
SparseMatrixTransposeTest.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 package top.kfufys.demo04;public class SparseMatrixTransposeTest { public static void main (String[] args) { Triple[] matrix = { new Triple (0 , 1 , 1 ), new Triple (0 , 3 , 2 ), new Triple (1 , 2 , 3 ), new Triple (2 , 3 , 4 ) }; System.out.println("Original Sparse Matrix:" ); TransMatrix.printMatrix(matrix); Triple[] transposedMatrix = TransMatrix.transpose(matrix); System.out.println("Transposed Sparse Matrix:" ); TransMatrix.printMatrix(transposedMatrix); } }
Triple.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 package top.kfufys.demo04;public class Triple { int row; int col; int value; public Triple (int row, int col, int value) { this .row = row; this .col = col; this .value = value; } }
TransMatrix.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 package top.kfufys.demo04;import top.kfufys.demo04.Triple;public class TransMatrix { public static Triple[] transpose(Triple[] matrix) { int numCols = 0 ; for (Triple triple : matrix) { if (triple.col > numCols) { numCols = triple.col; } } numCols++; Triple[] result = new Triple [matrix.length]; int [] colSizes = new int [numCols]; int [] colIndices = new int [numCols]; for (Triple triple : matrix) { colSizes[triple.col]++; } colIndices[0 ] = 0 ; for (int i = 1 ; i < numCols; i++) { colIndices[i] = colIndices[i - 1 ] + colSizes[i - 1 ]; } for (Triple triple : matrix) { int index = colIndices[triple.col]; result[index] = new Triple (triple.col, triple.row, triple.value); colIndices[triple.col]++; } return result; } public static void printMatrix (Triple[] matrix) { for (Triple triple : matrix) { System.out.println(triple.row + " " + triple.col + " " + triple.value); } } }
运行结果展示:
题目五:递归建立二叉树 问题描述:编写算法,使用递归方式建立二叉树:
BinaryTreeBuilder.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 package top.kfufys.demo05;public class BinaryTreeBuilder { public static void main (String[] args) { TreeNode root = BinaryTree.buildTree(); System.out.println("构建的二叉树的中序遍历结果:" ); BinaryTree.inorderTraversal(root); } }
TreeNode.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 package top.kfufys.demo05;public class TreeNode { int val; TreeNode left; TreeNode right; public TreeNode (int val) { this .val = val; this .left = null ; this .right = null ; } }
BinaryTree.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 package top.kfufys.demo05;import java.util.Scanner;public class BinaryTree { private static Scanner scanner = new Scanner (System.in); public static TreeNode buildTree () { System.out.print("请输入节点值(输入 -1 表示空节点): " ); int value = getValidInput(); if (value == -1 ) { return null ; } TreeNode root = new TreeNode (value); System.out.print("请输入节点 " + value + " 的左子节点值(输入 -1 表示空节点): " ); root.left = buildTree(); System.out.print("请输入节点 " + value + " 的右子节点值(输入 -1 表示空节点): " ); root.right = buildTree(); return root; } private static int getValidInput () { while (!scanner.hasNextInt()) { System.out.println("输入错误,请输入有效的整数。" ); scanner.next(); } return scanner.nextInt(); } public static void inorderTraversal (TreeNode root) { if (root == null ) { return ; } inorderTraversal(root.left); System.out.print(root.val + " " ); inorderTraversal(root.right); } }
运行结果展示:
- - Done - -