数据结构——线性表

博客涉及数据结构和线性表相关信息技术内容,虽未给出具体内容,但从标签可知围绕这些核心展开,数据结构是信息技术重要基础,线性表是其中关键部分。

线性表

虽然给定引用中未直接提及Java线性表算法与数据结构的测试相关内容,但可以基于线性表的实现来构建测试思路。 对于线性表的顺序存储,以顺序表为例,可测试其各项操作。以下是一个简单的顺序表测试代码示例: ```java import java.util.Arrays; // 假设的顺序表类 class SeqList { private Object[] data; private int length; private static final int DEFAULT_CAPACITY = 10; public SeqList() { data = new Object[DEFAULT_CAPACITY]; length = 0; } public void clear() { length = 0; } public boolean isEmpty() { return length == 0; } public int length() { return length; } public Object get(int i) throws Exception { if (i < 0 || i >= length) { throw new Exception("Index out of bounds"); } return data[i]; } public void insert(int i, Object x) throws Exception { if (i < 0 || i > length) { throw new Exception("Invalid insert position"); } if (length == data.length) { data = Arrays.copyOf(data, data.length * 2); } for (int j = length; j > i; j--) { data[j] = data[j - 1]; } data[i] = x; length++; } public void remove(int i) throws Exception { if (i < 0 || i >= length) { throw new Exception("Index out of bounds"); } for (int j = i; j < length - 1; j++) { data[j] = data[j + 1]; } length--; } public int indexOf(Object x) { for (int i = 0; i < length; i++) { if (data[i].equals(x)) { return i; } } return -1; } public void display() { for (int i = 0; i < length; i++) { System.out.print(data[i] + " "); } System.out.println(); } } // 测试类 class SeqListTest { public static void main(String[] args) { SeqList list = new SeqList(); try { // 测试插入 list.insert(0, 1); list.insert(1, 2); list.insert(2, 3); list.display(); // 测试获取元素 System.out.println("Element at index 1: " + list.get(1)); // 测试删除元素 list.remove(1); list.display(); // 测试查找元素 System.out.println("Index of 3: " + list.indexOf(3)); // 测试清空 list.clear(); System.out.println("Is list empty after clear? " + list.isEmpty()); } catch (Exception e) { e.printStackTrace(); } } } ``` 对于线性表的链式存储,以单链表为例,可参考引用[4]中的`PrimeLinkedList`类进行测试,以下是一个简单的测试代码示例: ```java class ListNode { int val; ListNode next; ListNode(int val) { this.val = val; } } class PrimeLinkedList { ListNode head; public boolean isPrime(int num) { if (num < 1) { return false; } for (int i = 2; i * i <= num; i++) { if (num % i == 0) { return false; } } return true; } public void insertPrime(int num) { if (!isPrime(num)) { System.out.println(num + "不是素数"); return; } ListNode newNode = new ListNode(num); if (head == null) { head = newNode; } else { ListNode current = head; while (current.next != null) { current = current.next; } current.next = newNode; } } public void printList() { ListNode current = head; while (current != null) { System.out.print(current.val + " "); current = current.next; } System.out.println(); } } class LinkedListTest { public static void main(String[] args) { PrimeLinkedList primeList = new PrimeLinkedList(); int[] numbers = {2, 3, 4, 5, 6, 7, 11, 13, 17, 19, 23}; for (int number : numbers) { primeList.insertPrime(number); } primeList.printList(); } } ``` 在上述测试中,分别对顺序表和单链表的插入、删除、查找、获取元素、清空等操作进行了测试,确保线性表的各项功能正常工作。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值