Skip to content

Instantly share code, notes, and snippets.

@neer2808
Forked from akashkumarcs19/DoubleEndedQueue.java
Created February 26, 2021 02:18
Show Gist options
  • Save neer2808/e170a098e7072238d7626e296f770c51 to your computer and use it in GitHub Desktop.
Save neer2808/e170a098e7072238d7626e296f770c51 to your computer and use it in GitHub Desktop.
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 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 class DoubleEndedQueue {
public static void main(String[] args) {
Queue1 quee = new Queue1();
quee.beginning=new Node(15);
}
}
@neer2808
Copy link
Author

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

class DoubleEndedQueue {
public static void main(String[] args) {
Queue1 quee = new Queue1();
quee.beginning = new Queue1.Node(15);
System.out.println(quee.dequeueFront());

}
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment