java1.通过顺序存储结构和链式存储结构构建顺序表、链表、顺序栈、链栈(学生属性包括:序号、学号、姓名、性别、年龄、班级)
时间: 2023-07-04 22:04:29 浏览: 94
数据结构:定义一个包含学生信息(学号、姓名、成绩)的顺序表_数据结构
5星 · 资源好评率100%
顺序存储结构实现顺序表和顺序栈:
```java
// 学生类
class Student {
private int number; // 序号
private String id; // 学号
private String name; // 姓名
private String gender; // 性别
private int age; // 年龄
private String clazz; // 班级
public Student(int number, String id, String name, String gender, int age, String clazz) {
this.number = number;
this.id = id;
this.name = name;
this.gender = gender;
this.age = age;
this.clazz = clazz;
}
public int getNumber() {
return number;
}
public String getId() {
return id;
}
public String getName() {
return name;
}
public String getGender() {
return gender;
}
public int getAge() {
return age;
}
public String getClazz() {
return clazz;
}
}
// 顺序表类
class SeqList {
private Student[] data; // 数据域
private int length; // 数据长度
// 初始化操作
public SeqList(int maxSize) {
data = new Student[maxSize];
length = 0;
}
// 插入操作
public boolean insert(Student student) {
if (length == data.length) {
System.out.println("插入失败,表已满!");
return false;
}
data[length++] = student;
return true;
}
// 删除操作
public boolean delete(int index) {
if (index < 0 || index >= length) {
System.out.println("删除失败,索引越界!");
return false;
}
for (int i = index; i < length - 1; i++) {
data[i] = data[i + 1];
}
length--;
return true;
}
// 查找操作
public Student find(String id) {
for (int i = 0; i < length; i++) {
if (data[i].getId().equals(id)) {
return data[i];
}
}
return null;
}
}
// 顺序栈类
class SeqStack {
private Student[] data; // 数据域
private int top; // 栈顶指针
// 初始化操作
public SeqStack(int maxSize) {
data = new Student[maxSize];
top = -1;
}
// 入栈操作
public boolean push(Student student) {
if (top == data.length - 1) {
System.out.println("入栈失败,栈已满!");
return false;
}
data[++top] = student;
return true;
}
// 出栈操作
public Student pop() {
if (top == -1) {
System.out.println("出栈失败,栈已空!");
return null;
}
return data[top--];
}
// 获取栈顶元素
public Student getTop() {
if (top == -1) {
System.out.println("获取栈顶元素失败,栈已空!");
return null;
}
return data[top];
}
}
```
链式存储结构实现链表和链栈:
```java
// 链表节点类
class ListNode {
private Student data; // 数据域
private ListNode next; // 指针域
public ListNode(Student data) {
this.data = data;
next = null;
}
public Student getData() {
return data;
}
public void setData(Student data) {
this.data = data;
}
public ListNode getNext() {
return next;
}
public void setNext(ListNode next) {
this.next = next;
}
}
// 链表类
class LinkedList {
private ListNode head; // 头节点
// 初始化操作
public LinkedList() {
head = new ListNode(null);
}
// 插入操作
public void insert(Student student) {
ListNode p = head;
while (p.getNext() != null) {
p = p.getNext();
}
p.setNext(new ListNode(student));
}
// 删除操作
public boolean delete(String id) {
ListNode p = head.getNext();
ListNode pre = head;
while (p != null) {
if (p.getData().getId().equals(id)) {
pre.setNext(p.getNext());
return true;
}
pre = p;
p = p.getNext();
}
return false;
}
// 查找操作
public Student find(String id) {
ListNode p = head.getNext();
while (p != null) {
if (p.getData().getId().equals(id)) {
return p.getData();
}
p = p.getNext();
}
return null;
}
}
// 链栈类
class LinkStack {
private ListNode top; // 栈顶指针
// 初始化操作
public LinkStack() {
top = null;
}
// 入栈操作
public void push(Student student) {
ListNode newNode = new ListNode(student);
newNode.setNext(top);
top = newNode;
}
// 出栈操作
public Student pop() {
if (top == null) {
System.out.println("出栈失败,栈已空!");
return null;
}
Student data = top.getData();
top = top.getNext();
return data;
}
// 获取栈顶元素
public Student getTop() {
if (top == null) {
System.out.println("获取栈顶元素失败,栈已空!");
return null;
}
return top.getData();
}
}
```
阅读全文