Meryem Mutluer Meryem Mutluer - 1 month ago 10
Java Question

stack and queues using doubly linked list in java

I have some problems that code. I read file in code and build one stack and one queues structure. But the code wasn't run correctly.

This is Node class
I used Double 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;
}
}


** this is stack class. **

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();
}
}
}


This is Queues Class

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();
}





}


This is Queues Class

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;
}
}

Answer

Ok, so you have a couple of problems. I'm going to point out a few and let you work to fix the rest because this looks like an assignment and I don't want to do your homework for you :).

First, when you read from the file be careful not to ignore the first element:

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();
    }

Notice that unlike your solution I first do the push y.Push(line) so that we don't forget to add whatever is already read into line. Same goes for the queue file:

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();
    }

Just add it if it's not null and then read the next line. You were always missing on the first element from the file.

Another problem is the Queues class (which by the way is misspelled you should replace O with Q). This one is not working properly because you forgot to increment and decrement the size when you insert or remove.

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++;
}

Notice that at the end of insert I'm increasing the size so that the list method doesn't throw a NullPointerException every time we call it. Same goes for the remove method:

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;
}

Please also notice that your check before (if(head.next==tail)) was also throwing NullPointerException because at the beginning the head is always null so you cannot access the next member. Finally I've made a small improvement to the list method as well so that we return earlier:

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();
}

Notice the return if the Queue is empty, otherwise we will attempt to do tail.getNext() which will always throw a NullPointerException.

Some important thoughts about the code in general Please avoid weird naming. Why Queues? There is just one so it should be Queue. Please avoid weird variable names. Your code is not just for you, chances are someone else might need to read and it gets hard to know who it is s, y, k, fwy and fwk. Why not naming them like this:

Scanner scanner = new Scanner(System.in);
Stack stack = new Stack();
Queues queue = new Queues();
FileWriter stackFileWriter;
FileWriter queueFileWriter;

And the same goes for methods . Why Push, Pop and Top are the only methods that start with upper-case letter ? If you don't agree with the standard Java naming convention that is fine but at least be consistent :).

Try the suggested improvements and see how your program it's working. I'm almost sure there are more problems with it. If you can't figure them out yourself leave a comment and I will help you. Good luck!