data structures - stack and queues using doubly linked list in java -
i have problems code. read file in code , build 1 stack , 1 queues structure. code wasn't run correctly.
this node class 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; } }
** 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 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 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 , 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; } }
ok, have couple of problems. i'm going point out few , let work fix rest because looks assignment , don't want homework :).
first, when read file careful not ignore first element:
string line = reader.readline(); while (line != null) { system.out.println("read stack: " + line); // read 1 element y.push(line); line = reader.readline(); }
notice unlike solution first push y.push(line)
don't forget add whatever read line
. same goes queue file:
string lines = read.readline(); while (lines != null) { system.out.println("read queue: " + lines); // read 1 line k.insert(lines); lines = read.readline(); }
just add if it's not null
, read next line. missing on first element file.
another problem queues class (which way misspelled should replace o
q
). 1 not working because you forgot increment , decrement size when 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 @ end of insert i'm increasing size
list
method doesn't throw nullpointerexception
every time call it. same goes 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 notice check before (if(head.next==tail)
) throwing nullpointerexception
because @ beginning head
null
cannot access next
member. i've made small improvement list
method 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 return
if queue empty, otherwise attempt tail.getnext()
throw nullpointerexception
.
some important thoughts code in general please avoid weird naming. why queues? there 1 should queue. please avoid weird variable names. code not you, chances else might need read , gets hard know s, y, k, fwy , fwk
. why not naming them this:
scanner scanner = new scanner(system.in); stack stack = new stack(); queues queue = new queues(); filewriter stackfilewriter; filewriter queuefilewriter;
and same goes methods . why push
, pop
, top
methods start upper-case letter ? if don't agree standard java naming convention fine @ least consistent :).
try suggested improvements , see how program it's working. i'm sure there more problems it. if can't figure them out leave comment , you. luck!
Comments
Post a Comment