定义一个泛型类MyStack,实现栈的基本操作:push、pop、peek、isEmpty。
时间: 2023-10-20 17:21:24 浏览: 104
```
public class MyStack<T> {
private List<T> stack;
public MyStack() {
stack = new ArrayList<>();
}
public void push(T element) {
stack.add(element);
}
public T pop() {
if (isEmpty()) {
throw new EmptyStackException();
}
return stack.remove(stack.size() - 1);
}
public T peek() {
if (isEmpty()) {
throw new EmptyStackException();
}
return stack.get(stack.size() - 1);
}
public boolean isEmpty() {
return stack.isEmpty();
}
}
```
相关问题
定义一个泛型类MyStack,实现栈的基本操作:push、pop、peek、isEmpty。 定义一个泛型类MyQueue,实现队列的基本操作:add、remove、peek、isEmpty。 定义一个泛型类MyArrayList,实现数组列表的基本操作:add、remove、get、size。
好的,我可以为您解答这个问题。
以下是定义泛型类MyStack的代码:
```java
public class MyStack<T> {
private ArrayList<T> list;
public MyStack() {
list = new ArrayList<>();
}
public void push(T item) {
list.add(item);
}
public T pop() {
if (isEmpty()) {
throw new EmptyStackException();
}
return list.remove(list.size() - 1);
}
public T peek() {
if (isEmpty()) {
throw new EmptyStackException();
}
return list.get(list.size() - 1);
}
public boolean isEmpty() {
return list.isEmpty();
}
}
```
以下是定义泛型类MyQueue的代码:
```java
public class MyQueue<T> {
private LinkedList<T> list;
public MyQueue() {
list = new LinkedList<>();
}
public void add(T item) {
list.addLast(item);
}
public T remove() {
if (isEmpty()) {
throw new NoSuchElementException();
}
return list.removeFirst();
}
public T peek() {
if (isEmpty()) {
throw new NoSuchElementException();
}
return list.getFirst();
}
public boolean isEmpty() {
return list.isEmpty();
}
}
```
以下是定义泛型类MyArrayList的代码:
```java
public class MyArrayList<T> {
private T[] array;
private int size;
public MyArrayList() {
array = (T[]) new Object[10];
size = 0;
}
public void add(T item) {
if (size == array.length) {
resize();
}
array[size++] = item;
}
public T remove(int index) {
if (index < 0 || index >= size) {
throw new IndexOutOfBoundsException();
}
T item = array[index];
for (int i = index; i < size - 1; i++) {
array[i] = array[i + 1];
}
array[--size] = null;
return item;
}
public T get(int index) {
if (index < 0 || index >= size) {
throw new IndexOutOfBoundsException();
}
return array[index];
}
public int size() {
return size;
}
private void resize() {
T[] newArray = (T[]) new Object[array.length * 2];
System.arraycopy(array, 0, newArray, 0, size);
array = newArray;
}
}
```
以上是定义泛型类MyStack、MyQueue、MyArrayList的代码,它们分别实现了栈、队列、数组列表的基本操作。
定义一个泛型类MyQueue,实现队列的基本操作:add、remove、peek、isEmpty
。```java
public class MyQueue<T> {
private LinkedList<T> list;
public MyQueue() {
list = new LinkedList<T>();
}
public void add(T element) {
list.addLast(element);
}
public T remove() {
if (isEmpty()) {
throw new NoSuchElementException("Queue is empty.");
}
return list.removeFirst();
}
public T peek() {
if (isEmpty()) {
throw new NoSuchElementException("Queue is empty.");
}
return list.getFirst();
}
public boolean isEmpty() {
return list.isEmpty();
}
}
```
其中,LinkedList是Java集合框架中的一个双向链表实现,可以高效地支持队列的操作。泛型参数T表示队列中元素的类型,可以是任意类型。在构造函数中,创建一个空的LinkedList对象,表示一个空队列。add方法利用LinkedList的addLast方法实现入队操作;remove方法利用LinkedList的removeFirst方法实现出队操作,并在队列为空时抛出NoSuchElementException异常;peek方法利用LinkedList的getFirst方法获取队首元素,但不从队列中删除,也在队列为空时抛出NoSuchElementException异常;isEmpty方法利用LinkedList的isEmpty方法判断队列是否为空。
相关推荐
![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_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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)