코드에 몇 가지 문제점이 있습니다. 나는 파일에서 코드를 읽고 하나의 스택과 하나의 큐 구조를 만든다. 그러나 코드가 올바르게 실행되지 않았습니다. 자바에서 이중 연결리스트를 사용하는 스택 및 큐
이
내가 ** 더블 LinkedList의이입니다 스택 클래스를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;
}
}
어떤 특정 문제가 있습니까? 오류 메시지 또는 잘못된 출력이 표시됩니까? 코드의 어떤 부분이 예상대로 작동하지 않습니까? – DUman
문제는 스택 클래스와 큐 클래스의 add 메서드 및 완전히 사실이 아닌 메서드 제거입니다. 그 방법은 첫번째 색인을 추가하지 않는다. 또한 모든 파일을 쓰지 않는 파일도 있습니다. – mrsengineer