java中使用双链表的堆栈和队列
我有一些关于代码的问题。我在代码中读取文件并构建一个堆栈和一个队列结构。但是代码没有正确运行 这是节点类 我使用了双链接列表java中使用双链表的堆栈和队列,java,data-structures,Java,Data Structures,我有一些关于代码的问题。我在代码中读取文件并构建一个堆栈和一个队列结构。但是代码没有正确运行 这是节点类 我使用了双链接列表 public class Node { String data; Node next; Node prev; public Node(String data,Node next, Node prev){ this.next=next; this.data=data; this.prev=prev; } public Node(){ } p
public class Node
{
String data;
Node next;
Node prev;
public Node(String data,Node next, Node prev){
this.next=next;
this.data=data;
this.prev=prev;
}
public Node(){
}
public String getData(){
return data;
}
public void setData(String data){
this.data=data;
}
public Node getNext(){
return next;
}
public void setNext(Node next){
this.next=next;
}
public Node getPrev(){
return prev;
}
public void setPrev(Node prev){
this.prev=prev;
}
}
**这是堆栈类**
public class Stack {
Node head = null;
Node tail = null;
int size=0;
public int getSize() {
return size;
}
public boolean isEmpty()
{
return head == null;
}
public void Push(String data) {
tail = head;
head = new Node(data,null,null);
head.data=data;
head.next= tail;
head.prev = null;
if(tail != null) {
tail.prev=head;
}
size++;
}
public void Pop() {
if (!isEmpty()) {
head = head.next; // delete first node
size--;
} else {
System.out.println("İs Empty");
}
}
public void Top() {
Node tmp = head;
while (tmp != null) {
System.out.println(tmp.getData());
tmp = tmp.getNext();
}
}
}
这是队列类
public class Oueues {
Node head ;
Node tail;
int size=0;
public Oueues(){
this.head=null;
this.tail=null;
}
public boolean isEmpty()
{
return head == tail;
}
public int getSize()
{
return size;
}
public void insert(String data){
Node tmp = new Node(data,null,null);
tmp.data=data;
tmp.next=null;
if(head==null){
head=tail=tmp;
head.prev=null;
}
else{
tail.next=tmp;
tmp.prev=tail;
tail=tmp;
}
}
public String remove(){
if(head.next==tail)
return null;// list empty
Node tmp=head.next;
head.next=tmp.next;
tmp.next.prev=head;
list();
return tmp.data;
}
public void list(){
System.out.println("Queues");
if(size==0){
System.out.println("İs Empty");
}
Node tmp=head;
while(tmp !=tail.getNext()){
System.out.println(tmp.getVeri()+" ");
tmp= tmp.getNext();
}
System.out.println();
}
}
这是队列类
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
public class OGrenci {
public static void main(String[] args) throws IOException {
Scanner s = new Scanner(System.in);
Stack y = new Stack();
Oueues k = new Oueues();
FileWriter fwy;
FileWriter fwk;
File stack = new File("stack.txt");
if (!stack.exists()) {
stack.createNewFile();
} else {
System.out.println("already exists ");
}
BufferedReader reader = null;
reader = new BufferedReader(new FileReader(stack));
String line = reader.readLine();
while (line != null) {
y.Push(line = reader.readLine());
System.out.println(line);
}
File queue = new File("queue.txt");
if (!queue.exists()) {
queue.createNewFile();
} else {
System.out.println("already exists ");
}
BufferedReader read = null;
read = new BufferedReader(new FileReader(queue));
String lines = read.readLine();
while (lines != null) {
lines = read.readLine();
k.insert(lines);
System.out.println(lines);
}
int choice;
System.out.println("1. Stack out- queue add");
System.out.println("2. Stack add- queue out");
System.out.println("3. Stack and queue ");
System.out.println("4. File writer");
choice = s.nextInt();
switch (choice) {
case 1:
k.insert(s.next());
k.list();
y.pop();
break;
case 2:
y.Push(s.next());
y.Top();
k.remove();
break;
case 3:
y.Top();
k.list();
break;
case 4:
fwy = new FileWriter(stack);
Node no = y.head;
while (no.next != null) {
fwy.write("\n" + no.data);
no = no.next;
}
fwy.flush();
fwy.close();
fwk = new FileWriter(queue);
Node noo = k.head;
while (noo.next != null) {
fwk.write("\n" + noo.data);
noo = noo.next;
}
fwk.flush();
fwk.close();
break;
}
}
好的,你有几个问题。我要指出一些,让你来解决其余的问题,因为这看起来像是一项作业,我不想为你做家庭作业:) 首先,在读取文件时,请注意不要忽略第一个元素:
String line = reader.readLine();
while (line != null)
{
System.out.println("Read from stack: " + line);
// we already read one element
y.Push(line);
line = reader.readLine();
}
请注意,与您的解决方案不同,我首先执行pushy.push(line)
,这样我们就不会忘记添加已读入行的内容。队列文件也是如此:
String lines = read.readLine();
while (lines != null)
{
System.out.println("Read from queue: " + lines);
// we already read one line
k.insert(lines);
lines = read.readLine();
}
如果它不是null
就添加它,然后读取下一行。您始终缺少文件中的第一个元素
另一个问题是Queues类(顺便说一下,它拼写错误,您应该将O
替换为Q
)。这一个无法正常工作,因为插入或删除时忘记了增加和减少大小
public void insert(String data){
Node tmp = new Node(data,null,null);
tmp.data=data;
tmp.next=null;
if(head==null){
head=tail=tmp;
head.prev=null;
}
else{
tail.next=tmp;
tmp.prev=tail;
tail=tmp;
}
size++;
}
请注意,在insert的末尾,我增加了大小
,这样列表
方法不会在每次调用它时抛出NullPointerException
。remove
方法也是如此:
public String remove(){
if(head == null)
return null;// list empty
Node tmp=head.next;
head.next=tmp.next;
tmp.next.prev=head;
size--;
list();
return tmp.data;
}
还请注意,您之前的检查(if(head.next==tail)
)也抛出了NullPointerException
,因为在开始时head
总是null
,因此您无法访问下一个成员。
最后,我还对list
方法做了一点改进,以便我们更早地返回:
public void list(){
System.out.println("Queues");
if(size==0){
System.out.println("İs Empty");
return;
}
Node tmp=head;
while(tmp != tail.getNext()){
System.out.println(tmp.getData() + " ");
tmp= tmp.getNext();
}
System.out.println();
}
请注意,如果队列为空,则返回,否则我们将尝试执行tail.getNext()
,它将始终抛出NullPointerException
关于代码总体的一些重要想法
请避免使用奇怪的名称。为什么要排队?只有一个,所以应该是队列。
请避免使用奇怪的变量名。您的代码不仅仅是为您编写的,可能还有其他人需要阅读,并且很难知道它是谁s、y、k、fwy和fwk
。为什么不这样命名呢:
Scanner scanner = new Scanner(System.in);
Stack stack = new Stack();
Queues queue = new Queues();
FileWriter stackFileWriter;
FileWriter queueFileWriter;
方法也是如此。为什么Push
、Pop
和Top
是唯一以大写字母开头的方法?如果您不同意标准Java命名约定,这很好,但至少要保持一致:)
尝试建议的改进,看看你的程序是如何工作的我几乎可以肯定它有更多的问题。如果你自己无法理解,请留下评论,我会帮助你。祝你好运 你的具体问题是什么?您是否收到错误消息或不正确的输出?代码的哪一部分不能像您预期的那样工作?问题是堆栈类和队列类的add方法和remove方法不完全正确。该方法不添加第一个索引。另外,不要把所有数据都写进文件中。谢谢你的帮助。我是一名学生,我最需要帮助,因为我正在学习过程中。你真是太好了。我非常感激。我纠正了我的错误,我能仔细地理解语法。好的,学习如何对代码进行单元测试也是一个好主意。避免在不测试代码是否有效的情况下编写大量代码。使用所谓的“编写一点代码,测试一点”技术,例如,在您添加一个方法之后,您立即编写一个单元测试,以确保它按照您想要的方式工作。此外,如果您对回复感到满意,请将答案标记为已接受。