java - Adding and removing elements to a double ended queue -
i have create double ended queue can add,remove , peek elements both sides (i.e, front , rear)
i got methods adding,removing , peeking elements @ 'head' unable figure out methods same thing 'tail' of queue
here code far:-
public class dequeue { private node rear; private node front; private int counter; class node { private string item; private node link; } public node() { rear = front = null; counter = 0; } public void hadd(string o) { node temp = new node(); temp.item =o; temp.link =null; if (rear==null) front = rear = temp; else { rear.link = temp; rear = temp; } counter++; } public boolean isempty() { if(counter==0) return true; return false; } public object hpeek() { return rear.item; } public object hremove() { if (isempty()) return null; else { object temp=front.item; front = front.link; if (front == null) rear = null; counter--; return temp; } } public int size() { return counter; } public void tadd(string o) { node temp = new node(); temp.item =o; temp.link =null; if (front==null) front=rear=temp; else { front.link=temp; front=temp; } counter++; } public object tpeek() { return front.item; } public object tremove() { if (isempty()) return null; else { object temp=rear.item; rear=rear.link; if (rear==null) front=null; counter--; return temp; } } public string tostring() { stringbuilder result=new stringbuilder(); node curr=front; while(curr!=null) { result.append(curr.item+" \n"); curr = curr.link; } return result.tostring(); } }
Comments
Post a Comment