Created
March 1, 2021 17:09
-
-
Save akashkumarcs19/7af8f137ccfa43ffa2eab4d0414e1fa4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Queue1 { | |
static Node head; | |
Node beginning = null; | |
Node topOfQueue = null; | |
static class Node { | |
int data; | |
Node next; | |
public Node(int data) { | |
this.data = data; | |
next = null; | |
} | |
} | |
public boolean isEmpty(){ | |
if(beginning == null && topOfQueue == null) | |
return true; | |
else return false; | |
} | |
public void enqueue(Node newnode){ | |
if(isEmpty()){ | |
beginning = newnode; | |
topOfQueue = newnode; | |
} | |
else { | |
Node current = head; | |
while(current != null){ | |
current = current.next; | |
} | |
current.next = newnode; | |
newnode = current; | |
topOfQueue = newnode; | |
} | |
} | |
public void enqueueFront(Node newnode){ | |
if(isEmpty()){ | |
beginning = newnode; | |
topOfQueue = newnode; | |
} | |
else{ | |
newnode.next=beginning; | |
beginning = newnode; | |
} | |
} | |
public void enqueueRear(Node newnode){ | |
if(isEmpty()){ | |
topOfQueue = newnode; | |
beginning = newnode; | |
} | |
else{ | |
topOfQueue.next = newnode; | |
topOfQueue = newnode; | |
newnode.next = null; | |
} | |
} | |
public int dequeueFront(){ | |
int temp = 0; | |
if(isEmpty()){ | |
System.out.println("UnderFlow Condition"); | |
} | |
else { | |
temp = beginning.data; | |
beginning = beginning.next; | |
} | |
return temp; | |
} | |
public int dequeRear(){ | |
Node temp = beginning; | |
int k = 0; | |
if(isEmpty()){ | |
System.out.println("Underfloww"); | |
} | |
else{ | |
while(temp.next!=topOfQueue){ | |
temp=temp.next; | |
} | |
k = topOfQueue.data; | |
topOfQueue = temp; | |
topOfQueue.next =null; | |
} | |
return k; | |
} | |
public static void printList(){ | |
Queue1.Node current = head; | |
while(current!=null){ | |
System.out.println(current.data+" "); | |
current=current.next; | |
} | |
} | |
} | |
public class DoubleEndedQueue { | |
public static void main(String[] args) { | |
Queue1 obj = new Queue1(); | |
Queue1.Node obj1 = new Queue1.Node(5); | |
Queue1.printList(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment