Posts

Showing posts from January, 2022

deque in data structure java

Image
Double-Ended Queue in Data Structure java public class dequeue_ex { int front =- 1 , rear =- 1 ; int size = 5 ; int [] myarr = new int [ size ] ; public void enqueueFront ( int data){ if (( front == 0 && rear ==( size - 1 )) || front == rear + 1 ){ System. out .println( "overflow" ) ; } else if ( front ==- 1 && rear ==- 1 ){ front = rear = 0 ; myarr [ front ]=data ; } else if ( front == 0 ){ front = size - 1 ; myarr [ front ]=data ; } else { front -- ; myarr [ front ]=data ; } } public void enqueuRear ( int data){ if (( front == 0 && rear ==( size - 1 )) || front == rear + 1 ){ System. out .println( "overflow" ) ; } else if ( front ==- 1 && rear ==- 1 ){ front = rear = 0 ; myarr [ rear ]=data ; } ...

circular queue implementation java

Image
  circular queue implementation java public class circ_queue { int front =- 1 , rear =- 1 ; int size = 5 ; int [] arr = new int [ size ] ; public void enqueue ( int data){ if ( front ==( rear + 1 )% size ){ System. out .println( "overflow" ) ; } else { if ( front ==- 1 && rear ==- 1 ){ front ++ ; rear ++ ; arr [ rear ]=data ; } else { rear =( rear + 1 )% size ; arr [ rear ]=data ; } } } public void dequeue (){ if ( front ==- 1 && rear ==- 1 ){ System. out .println( "underflow" ) ; } else if ( front == rear ){ front =- 1 ; rear =- 1 ; } else { front =( front + 1 )% size ; } } public void display (){ if ( front ==- 1 && rear ==- 1 ){ System. ...

Queue implementation using linkedlist in java

Image
  Queue implementation using linkedlist in java public class queue_ll { class Node{ int data ; Node next ; Node ( int data){ this . data =data ; this . next = null; } } Node front = null; Node rear = null; public void enqueue ( int data){ Node toadd= new Node(data) ; if ( front == null ){ front =toadd ; rear =toadd ; } else { rear . next =toadd ; rear =toadd ; } } public void dequeue (){ if ( front == null ){ System. out .println( "underflow" ) ; } else { front = front . next ; } } public void display (){ Node temp= front ; while (temp!= null ){ System. out .println(temp. data ) ; temp=temp. next ; } } public static void main (String[] args) { queue_ll qll= new queue_ll() ; ...

Queue Implementation using array in Java

Image
 Queue implementation in Java hindi public class queue_ary { int front =- 1 , rear =- 1 ; int size = 5 ; int [] arr = new int [ size ] ; public void enqueue ( int data){ if ( rear ==( size - 1 )){ System. out .println( "overflow" ) ; } else { if ( front ==- 1 && rear ==- 1 ){ front ++ ; rear ++ ; arr [ rear ]=data ; } else { rear ++ ; arr [ rear ]=data ; } } } public void dequeue (){ if ( front ==- 1 && rear ==- 1 ){ System. out .println( "empty" ) ; } else { for ( int i= 0 ; i< rear ; i++){ arr [i]= arr [i+ 1 ] ; } rear -- ; } } public void display (){ if ( front ==- 1 && rear ==- 1 ){ System. out .println( "empty" ) ; ...

stack implementation using linked list in java

Image
  stack implementation using linked list in java public class linkstack { class Node{ int data ; Node next ; Node ( int data){ this . data =data ; this . next = null; } } Node head = null; int size ; public void push ( int data){ Node newnode= new Node(data) ; size ++ ; if ( head == null ){ head =newnode ; } else { newnode. next = head ; head =newnode ; } } public void pop (){ if ( head == null ){ System. out .println( "underflow" ) ; } else { size -- ; head = head . next ; } } public void display (){ Node temp= head ; if (temp== null ){ System. out .println( "empty" ) ; } while (temp!= null ){ System. out .println(temp. data ) ; temp=temp. next ; } ...

Implementation of stack using Array in java

Image
 stack array implementation in java public class stacks { int size ; int arr [] ; int top ; stacks ( int size){ this . size =size ; this . arr = new int [size] ; this . top =- 1 ; } public void push ( int data){ if ( top ==( size - 1 )){ System. out .println( "overflow" ) ; } else { top ++ ; arr [ top ]=data ; System. out .println( "push elem " + data) ; } } public void display (){ int temp= top ; for ( int i=temp ; temp>= 0 ; temp--){ System. out .println( arr [temp]) ; } } public void peek (){ if ( top ==- 1 ){ System. out .println( "empty stack" ) ; } else { System. out .println( " peek is " + arr [ top ]) ; } } public void pop (){ if ( top ==- 1 ){ System. out .println( "empty...

circular doubly linked list deletion at begining

Image
 first node delete circular doubly linked list public class cdll { class Node { int data ; Node prev ; Node next ; Node ( int data) { this . data = data ; this . next = null; this . prev = null; } } Node head = null; Node tail = null; public void toadd ( int data){ Node newnode= new Node(data) ; if ( head == null ){ head =newnode ; tail =newnode ; newnode. next = head ; newnode. prev = tail ; } else { tail . next =newnode ; newnode. prev = tail ; tail =newnode ; tail . next = head ; head . prev = tail ; } } public void dis (){ Node temp= head ; if ( head == null ){ System. out .println( "empty" ) ; } else { do { System. out .println(temp. data ) ; ...

circular doubly linked list deletion at end

Image
 circular doubly linked list deletion last node public class cdll { class Node { int data ; Node prev ; Node next ; Node ( int data) { this . data = data ; this . next = null; this . prev = null; } } Node head = null; Node tail = null; public void toadd ( int data){ Node newnode= new Node(data) ; if ( head == null ){ head =newnode ; tail =newnode ; newnode. next = head ; newnode. prev = tail ; } else { tail . next =newnode ; newnode. prev = tail ; tail =newnode ; tail . next = head ; head . prev = tail ; } } public void dis (){ Node temp= head ; if ( head == null ){ System. out .println( "empty" ) ; } else { do { System. out .println(temp. data ) ; ...

circular doubly linked list insertion at begining in java

Image
  circular doubly linked list insertion at begining in java public class cdll { class Node { int data ; Node prev ; Node next ; Node ( int data) { this . data = data ; this . next = null; this . prev = null; } } Node head = null; Node tail = null; public void toadd ( int data){ Node newnode= new Node(data) ; if ( head == null ){ head =newnode ; tail =newnode ; newnode. next = head ; newnode. prev = tail ; } else { tail . next =newnode ; newnode. prev = tail ; tail =newnode ; tail . next = head ; head . prev = tail ; } } public void dis (){ Node temp= head ; if ( head == null ){ System. out .println( "empty" ) ; } else { do { System. out .println(temp. data )...

circular doubly linked list insertion at the end in java

Image
  circular doubly linked list insertion at the end in java public class cdll { class Node { int data ; Node prev ; Node next ; Node ( int data) { this . data = data ; this . next = null; this . prev = null; } } Node head = null; Node tail = null; public void toadd ( int data){ Node newnode= new Node(data) ; if ( head == null ){ head =newnode ; tail =newnode ; newnode. next = head ; newnode. prev = tail ; } else { tail . next =newnode ; newnode. prev = tail ; tail =newnode ; tail . next = head ; head . prev = tail ; } } public void dis (){ Node temp= head ; if ( head == null ){ System. out .println( "empty" ) ; } else { do { System. out .println(temp. data ) ...

circular doubly linked list creation and traversal in java

Image
 circular doubly linked list creation and traversal in java public class cdll { class Node { int data ; Node prev ; Node next ; Node ( int data) { this . data = data ; this . next = null; this . prev = null; } } Node head = null; Node tail = null; public void toadd ( int data){ Node newnode= new Node(data) ; if ( head == null ){ head =newnode ; tail =newnode ; newnode. next = head ; newnode. prev = tail ; } else { tail . next =newnode ; newnode. prev = tail ; tail =newnode ; tail . next = head ; head . prev = tail ; } } public void dis (){ Node temp= head ; if ( head == null ){ System. out .println( "empty" ) ; } else { do { System. out .println(temp. data )...

circular linked list deletion at end in java

Image
 singly circular linked list deletion at last position import java.util.Scanner ; public class cll { class Node{ int data ; Node next ; Node ( int data){ this . data =data ; this . next = null; } } Node head = null; Node tail = null; public void toadd ( int data){ Node newnode= new Node(data) ; if ( head == null ){ head =newnode ; tail =newnode ; tail . next = head ; } else { tail . next =newnode ; tail =newnode ; tail . next = head ; } } public void dis (){ Node temp= head ; if ( head == null ){ System. out .println( "empty" ) ; } else { do { System. out .println(temp. data ) ; temp=temp. next ; } while (temp!= head ) ; } }      public void dellast (){ Nod...

circular linked list deletion at begining in java

Image
 circular linked list deletion at  first position import java.util.Scanner ; public class cll { class Node{ int data ; Node next ; Node ( int data){ this . data =data ; this . next = null; } } Node head = null; Node tail = null; public void toadd ( int data){ Node newnode= new Node(data) ; if ( head == null ){ head =newnode ; tail =newnode ; tail . next = head ; } else { tail . next =newnode ; tail =newnode ; tail . next = head ; } } public void dis (){ Node temp= head ; if ( head == null ){ System. out .println( "empty" ) ; } else { do { System. out .println(temp. data ) ; temp=temp. next ; } while (temp!= head ) ; } }      public void delfirst (){ No...