Posts

Showing posts with the label Linear Queue

Linear Queue Linked List Implementation - All Operations

Image
  // Linked list node to maintain the queue class Node{     int data;     Node next;       // Cretaion of a queue node     // Time Complexity - O(1) , Space Complexity - O(1)     Node(int x){         this.data = x;         this.next = null;     } } public class Main {       // head and tail of the linked list     Node head;     Node tail;       // function to enqueue an element 'x' to the end of queue     // Time Complexity - O(1) , Space Complexity - O(1)     public void enQueue(int x){         // create a new linked list node         Node newNode = new Node(x);               // if linked list / queue is empty         if(head==null){             head=newN...

Linear Queue Implementation using Array - All Operations

Image
  public class Main {     // Array to implement Linear queue     int[] arr;     // variables to maintain indexes of start and end of queue     int startOFQueue;     int endOfQueue;       // constructor     // Creating a queue - Time Complexity - O(1), Space Complexity - O(n) , where n = size     Main(int size){         this.arr=new int[size];         this.startOFQueue=0;         this.endOfQueue=-1;     }       // function to initialize the queue array      //Time Complexity - O(n), Space Complexity - O(1)     public void initializeArray(){         if(this.startOFQueue==0 && this.endOfQueue==-1){             for(int i=0; i<arr.length; i++){                 this.arr[i...