用java编写堆栈
用java编写堆栈
堆栈是最基本的数据结构之一。
相比于十年前,现今对编程的学习要简单的多,现今有很多成熟 的编程工具可用,而且有很多的辅助编程的工具可用,比如说chatgpt,它能回答我们众多的提问,我们只需要知道如何向它提问,就能完成超多的编程任务。这里要编写堆栈程序,我从向CSDN的c知道提问开始。
一、提第一个问题,获得java程序的基本框架
提问:用java编程创建一个窗口
回答:
import javax.swing.*;
public class MyWindow extends JFrame {
public MyWindow() {
// 设置窗口标题
setTitle("My Window");
// 设置窗口大小
setSize(400, 300);
// 设置窗口在屏幕中的位置
setLocationRelativeTo(null);
// 设置窗口关闭时的操作
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// 添加一个标签
JLabel label = new JLabel("Hello, World!");
add(label);
// 显示窗口
setVisible(true);
}
public static void main(String[] args) {
// 创建窗口
new MyWindow();
}
}
这个示例代码创建了一个名为"My Window"的窗口,大小为400x300,居中显示在屏幕上,并在窗口中添加了一个标签,标签上显示"Hello, World!"。窗口关闭时会退出程序。
从上面的操作,我们就知道,只要我们懂得编程的基本知识,就能够独立完成软件的制作。
二、提第二个问题,获得java如何使用堆栈这种数据结构
提问:用java编程,展示java如何使用堆栈
回答:
import java.util.Stack;
public class StackExample {
public static void main(String[] args) {
Stack<String> stack = new Stack<>();
// 压栈
stack.push("Java");
stack.push("is");
stack.push("fun");
// 出栈
while (!stack.empty()) {
System.out.print(stack.pop() + " ");
}
}
}
堆栈是一种基本的数据结构,在大型程序中经常使用,堆栈实际上是一种后进先出的队列,用于保存想保存的数据,例如保存函数调用时用到的多个参数、保存程序运行时暂时的状态等。
堆栈涉及的操作有压入push()和弹出pop(),即压栈和出栈。
三、从上面我们可以知道,java编程环境已经对堆栈进行了实现,我们可以直接使用。
当然,这里我们想了解堆栈的具体实现,我们就要自己尝试编写实现堆栈。
如果想向chatgpt类软件提问来获得堆栈的实现,可能不会得到满意的结果,因为java环境已经实现了它。
编写实现堆栈的类:
public class Stack{
public Stack(){
}
public void push(int topArg){
}
public int pop(){
}
}
上面只展示了函数列表,但堆栈的本质是存储一列数据,因此还缺点什么,于是就变成了下面的实现:
public class Stack{
private int[] stackMembers;//我们假定堆栈用于存储一列整数
private int indexOfTop;//存储栈顶索引
private int maxIndex;//存储栈的最大索引,个人的不良习惯,喜欢把数据弄的明明白白的,即使是有冗余
public Stack(int theSize){
maxIndex=theSize;
indexOfTop=0;
stackMembers=new Array(theSize);
}
public void push(int topArg){
}
public int pop(){
}
}
接下来就是实现push()和pop(),也就是简单的往数组stackMembers中存入数据和取出数据,当然也需要修改栈顶索引值。这就实现了一个简单的堆栈。
四、上面的堆栈有其缺陷,它的内部数组的大小是固定的,当数据量较小时会浪费空间,在数据量较大时可能又不够用,毕竟程序在使用的过程中会出现何种情况是无法预知的。
接下来换用另外一种方法实现大小实时变化的堆栈,这种实现方法会用到对象引用。
public class Stack{
public Stack(){
}
public void push(int topArg){
}
public int pop(){
}
}
依旧是考虑如何存储一列整数,我们主要思考push()和pop()操作,每次push()一个整数时还要保证能找回上一个整数,因此我们可以定义一个新类用于存储整数和前一个整数的引用,我将该类命名为intNode,实现如下:
public class Stack{
private intNode myintNode;
public Stack(){
}
public void push(int topArg){
}
public int pop(){
}
public class intNode{
public int theInt;//存储整型数据
public intNode theNode;//存储前一个intNode类型数据的引用,能引用到intNode也就是能引用到其中的整型数据
public intNode(int theIntL,intNode theNodeL){
theInt=theIntL;
theNode=theNodeL;
}
}
}
这里要添加一句,基础类型的数据按复制的方式赋值,而object类型的数据按引用存储。
接下来,实现Stack的push()和pop():
public class Stack{
private intNode myintNode;
public Stack(){
}
public void push(int topArg){
if(myintNode==null){
myintNode=new intNode(topArg,null);
}else{
myintNode=new intNode(topArg,myintNode);
}
}
public int pop(){
if(myintNode!=null){
int myInt=myintNode.theInt;
myintNode=myintNode.theNode;
return myInt;
}
}
public class intNode{
public int theInt;//存储整型数据
public intNode theNode;//存储前一个intNode类型数据的引用,能引用到intNode也就是能引用到其中的整型数据
public intNode(int theIntL,intNode theNodeL){
theInt=theIntL;
theNode=theNodeL;
}
}
}
至此,一个堆栈就大致成型了。
五、调试我们的堆栈类
提问:用java编程创建一个窗口,在窗口中添加一个Lable、一个 文本框和一个按钮,在该文本框中输入数据后,当点击按钮,则在Label上显示文本框中的数据
获得应答后,把我们的堆栈复制进去,然后测试我们的堆栈:
package GUI_test;
import java.awt.*;
import java.awt.event.*;
public class FrameDemo3 {
public static void main(String[] args) {
// 创建窗体对象
Stack myStack=new Stack();
Frame f = new Frame("Java GUI");
// 设置窗体的大小和位置
f.setBounds(400, 200, 400, 300);
// 设置布局管理器
f.setLayout(new FlowLayout());
// 创建Label、TextField和Button对象
Label label = new Label("请输入数据:");
TextField textField = new TextField(20);
Button button = new Button("popOut");
Button button1 = new Button("pushIn");
// 添加组件到窗体中
f.add(label);
f.add(textField);
f.add(button);
f.add(button1);
// 为按钮添加事件监听器
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// 获取文本框中的数据
String text = textField.getText();
// 在Label上显示文本框中的数据
label.setText(""+myStack.pop());
}
});
button1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// 获取文本框中的数据
String text = textField.getText();
myStack.push(Integer.parseInt(text));
}
});
// 事件监听机制(窗口关闭)
f.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
// 调用一个方法,设置让窗体可见
f.setVisible(true);
}
}
public class Stack{
private intNode myintNode;
public Stack(){
}
public void push(int topArg){
if(myintNode==null){
myintNode=new intNode(topArg,null);
}else{
myintNode=new intNode(topArg,myintNode);
}
return;
}
public int pop(){
int myInt=-1;
if(myintNode!=null){
myInt=myintNode.theInt;
myintNode=myintNode.theNode;
}
return myInt;
}
public class intNode{
public int theInt;
public intNode theNode;
public intNode(int theIntL,intNode theNodeL){
theInt=theIntL;
theNode=theNodeL;
}
}
}
得出结论,大致完成功能。