Skip to main content

Posts

Showing posts with the label DSA

Implementation of Dequeue(Double ended queue) in Java

 Implementation of Dequeue(Double ended queue) in Java class dequeue { //double ended queue int DQ[],f=-1,r=-1,size; dequeue(int size) { this.size=size; DQ = new DQ[size]; } void addFront(int data) { if(f==-1 && r==-1) { f=0;r=0; } else if(f==0) { System.out.println("Queue overflow"); } else{ f=f-1; } DQ[f]=data; } void deleteFront() { if(f==-1 && r==-1) { System.out.println("Queue underflow"); } int v=DQ[f]; System.out.println("Removed:"+v); f=f+1; } void addRear(int data) { if(f==-1 && rear==-1) { f=0; r=0; } else if(rear==(size-1)) { System.out.println("Queue Overflow"); } else { r=r+1; } DQ[r]=data; } void removeRear() { if(f==-1 && r==-1) { System.out.println("Queue is underflow"); } if(f==r) { System.out.println("Removed element is:"+DQ[r]); f=-1; r=-1; } else { System.out.println("Removed element is:"+DQ[r]); r=r-1; } } void display() { if(f==-1 && r==-1) System.out.prin...

Program in Java: Reverse of a string using recursion

Program in Java: Reverse of a string using recursion   import java.util.Scanner; class reverse {     String rev(String s,int l) //s string and l is length of the string     {         if(s.length()==0)             return "";         return s.charAt(l-1)+rev(s.substring(0,l-1),l-1);     }     public static void main(String args[])     {         Scanner sc=new Scanner(System.in);         System.out.println("Enter a word:");         String str = sc.next();         reverse ob = new reverse();         int n = str.length();         String r = ob.rev(str,n);         System.out.println("The reverse:"+r);     } }

Linked List Implementation Using C

 Linked List Implementation Using C #include<stdio.h> #include<stdlib.h> struct node{     int data;     struct node *next; }; struct node *addEnd(struct node *head,int data) {     struct node *temp;     temp = (struct node*)malloc(sizeof(struct node));     temp->data = data;     temp->next = NULL;     if(head==NULL)     {         head=temp;         head->next=NULL;     }     else{         struct node *ptr = head;         while(ptr->next!=NULL)         {             ptr=ptr->next;         }         ptr->next=temp;     }     return head; } struct node *addBegin(struct node *head,int data) {     struct node *temp;     temp = (struct node*)malloc(sizeo...

Program in Java: To merge two linked list

Program in Java: To merge two linked list import java.util.Scanner; class node { int data; node link; node() { data=0; link=null; } void AddEnd(int data) { node temp = new node(); temp.data=data; if(this.data==0 && this.link==null) { //if the linkedlist is empty this.data=data; return; } else{ node ptr=this; while(ptr.link!=null) { ptr=ptr.link; } ptr.link=temp; } } void merge(node l) //node ptr is the linked list from which your are calling the merge function { //node l is the second linked list which stores takes another linked list object as parameter   //finally it combines the two list and stores in the memory   node ptr=this; //the memory   if(ptr==null) //if the ptr is null then make the node l and the new node chain   {   ptr=l;   }   else   {   while(ptr.link!=null)   {   ptr=ptr.link;   }   ptr...

Program in Java: To add a node at the end of a linked list

 void addEnd(int data)     {         Node temp = new Node();         temp.data=data;         if(this.link==null && this.data==0)          { //if the entire linked list is empty             this.data=data;         }         else         {             Node ptr=this;             while(ptr.link!=null)             {                 ptr=ptr.link;             }             ptr.link=temp;         }         temp=null;     }

Program in Java: To add a node at the beginning of a linkedlist

 Node addBegin(int data)     {         Node temp = new Node();         temp.data=data;         if(this.link==null && this.data==0)          { //if the entire linked list is empty             this.data=data;             return this;         }         else         {             Node ptr=this;             temp.link=ptr;             ptr=temp;             return ptr;         }

Write a linked list program in java to add elements from the end of the list

 /**  * Write a linked list program in java to add elements from the end of the list    */ class Node {     int data;     Node link;     Node()     {         data=0;         link=null;     }     void addEnd(int data)     {         Node temp = new Node();         temp.data=data;         if(this.link==null && this.data==0)          { //if the entire linked list is empty             this.data=data;         }         else         {             Node ptr=this;             while(ptr.link!=null)             {                 ptr=ptr.link;       ...

Program in Java Binary Search Numbers

Program in Java to implement binary search on numbers import java.util.Scanner; class BinarySearch {  public static void main(String args[])  {   Scanner sc = new Scanner(System.in);   System.out.println("Enter the number of elements: ");   int n = sc.nextInt();   int A[] = new int[n]; //array initialized   System.out.println("Enter the values into the array in ascending or descending: ");   for(int i=0;i<n;i++)   {    A[i]=sc.nextInt(); //accepting the values into the array   }   System.out.println("Enter the value you want to find: ");   int v = sc.nextInt();   int start=0,end=A.length-1; // to store the starting and ending index of the array   int pos=-1; //to store the index of the element from the array   int flag=0; //to store the presence of the element of the array   while(start<=end)   {    int mid = (start+end)/2;    if(A[mid]>v)     end=mid-1; ...

Program in Java Selection Sort Ascending order Numbers

Program in Java to implement selection sort (Ascending order) import java.util.Scanner; class SelectionSortNumber {  public static void main(String args[])  {   Scanner sc = new Scanner(System.in);   System.out.println("Enter the number of elements: ");   int n = sc.nextInt();   int A[] = new int[n]; //array initialized   System.out.println("Enter the values into the array:");   for(int i=0;i<n;i++)   {    A[i]=sc.nextInt(); //accepting the values into the array   }   //selection sort   for(int i=0;i<n-1;i++)   {    int m=A[i];    int pos=i;    for(int j=i+1;j<n;j++)    {     if(m>A[j])     {      m=A[j]; //find the smallest number index position      pos=j;     }    }    int t = A[pos];    A[pos]=A[i]; //store the smallest value in the location    A[i]=t; // in...

Program in Java Linear Search

Program in Java to implement Linear Search Algorithm import java.util.Scanner; class LinearSearch {  public static void main(String args[])  {   Scanner sc = new Scanner(System.in);   System.out.println("Enter the number of elements: ");   int n = sc.nextInt();   int A[] = new int[n]; //array initialized   System.out.println("Enter the values into the array: ");   for(int i=0;i<n;i++)   {    A[i]=sc.nextInt(); //accepting the values into the array   }   System.out.println("Enter the value you want to find: ");   int v = sc.nextInt();   int pos=-1; //to store the index position of the value present in the array   int flag=0; //flag value when 0 indicates the search value is not found   //flag value when 1 indicates the search value is found   for(int i=0;i<n;i++)   {    if(A[i]==v) //A[i] is compared to the value of v    {    //when value of v is found in th...