java.通过顺序存储结构和链式存储结构构建顺序表、链表、顺序栈、链栈(学生属性包括:序号、学号、姓名、性别、年龄、班级)
时间: 2023-07-04 18:04:31 浏览: 55
顺序存储结构构建顺序表:
```java
public class SeqList {
private Student[] students; // 存放学生的数组
private int size; // 数组中元素的个数
private int capacity; // 数组的容量
public SeqList(int capacity) {
this.capacity = capacity;
this.students = new Student[capacity];
this.size = 0;
}
// 添加学生到顺序表的末尾
public void add(Student student) {
if (size == capacity) {
System.out.println("顺序表已满,无法添加学生");
return;
}
students[size++] = student;
}
// 根据学号查找学生
public Student findByNo(String stuNo) {
for (int i = 0; i < size; i++) {
if (students[i].getStuNo().equals(stuNo)) {
return students[i];
}
}
return null;
}
// 删除指定学号的学生
public boolean deleteByNo(String stuNo) {
for (int i = 0; i < size; i++) {
if (students[i].getStuNo().equals(stuNo)) {
for (int j = i; j < size - 1; j++) {
students[j] = students[j + 1];
}
students[size - 1] = null;
size--;
return true;
}
}
return false;
}
}
```
链式存储结构构建链表:
```java
public class ListNode {
private Student student;
private ListNode next;
public ListNode(Student student) {
this.student = student;
this.next = null;
}
public Student getStudent() {
return student;
}
public void setStudent(Student student) {
this.student = student;
}
public ListNode getNext() {
return next;
}
public void setNext(ListNode next) {
this.next = next;
}
}
public class LinkedList {
private ListNode head; // 头结点
private int size; // 链表中元素的个数
public LinkedList() {
this.head = new ListNode(null);
this.size = 0;
}
// 添加学生到链表的末尾
public void add(Student student) {
ListNode node = new ListNode(student);
ListNode cur = head;
while (cur.getNext() != null) {
cur = cur.getNext();
}
cur.setNext(node);
size++;
}
// 根据学号查找学生
public Student findByNo(String stuNo) {
ListNode cur = head.getNext();
while (cur != null) {
if (cur.getStudent().getStuNo().equals(stuNo)) {
return cur.getStudent();
}
cur = cur.getNext();
}
return null;
}
// 删除指定学号的学生
public boolean deleteByNo(String stuNo) {
ListNode cur = head.getNext();
ListNode pre = head;
while (cur != null) {
if (cur.getStudent().getStuNo().equals(stuNo)) {
pre.setNext(cur.getNext());
size--;
return true;
}
pre = cur;
cur = cur.getNext();
}
return false;
}
}
```
顺序存储结构构建顺序栈:
```java
public class SeqStack {
private Student[] students; // 存放学生的数组
private int top; // 栈顶指针
private int capacity; // 栈的容量
public SeqStack(int capacity) {
this.capacity = capacity;
this.students = new Student[capacity];
this.top = -1;
}
// 入栈
public void push(Student student) {
if (top == capacity - 1) {
System.out.println("栈已满,无法添加学生");
return;
}
students[++top] = student;
}
// 出栈
public Student pop() {
if (top == -1) {
System.out.println("栈已空,无法弹出学生");
return null;
}
return students[top--];
}
// 查看栈顶元素
public Student peek() {
if (top == -1) {
System.out.println("栈已空");
return null;
}
return students[top];
}
// 判断栈是否为空
public boolean isEmpty() {
return top == -1;
}
}
```
链式存储结构构建链栈:
```java
public class LinkedStack {
private ListNode top; // 栈顶结点
private int size; // 栈中元素的个数
public LinkedStack() {
this.top = null;
this.size = 0;
}
// 入栈
public void push(Student student) {
ListNode node = new ListNode(student);
node.setNext(top);
top = node;
size++;
}
// 出栈
public Student pop() {
if (top == null) {
System.out.println("栈已空,无法弹出学生");
return null;
}
Student student = top.getStudent();
top = top.getNext();
size--;
return student;
}
// 查看栈顶元素
public Student peek() {
if (top == null) {
System.out.println("栈已空");
return null;
}
return top.getStudent();
}
// 判断栈是否为空
public boolean isEmpty() {
return top == null;
}
}
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)