Stack DS

 /* 

Classes and objects

Stack data structures 

*/


import java.io.*;

import java.util.*;


class StkDS

{

int tos, capacity;

int [] Stk;


public StkDS(int c)

{

tos = -1;

capacity = c;

Stk = new int[capacity];

}

int size()

{

return tos+1;

}

boolean isFull()

{

return((tos+1) == capacity);

}

boolean isEmpty()

{

return (tos==-1);

}

void push(int x)

{

if(!isFull())

Stk[++tos]=x;

else

System.out.print("\nFull stack. Cant insert element.");

}

void pop()

{

if(!isEmpty())

tos--;

else

System.out.print("\nEmpty stack. Cant delete element.");

}

int topandpop()

{

if(!isEmpty())

return(Stk[tos--]);

else

{

System.out.print("\nEmpty stack. Cant delete element.");

return -1;

}

}

void peek()

{

if(!isEmpty())

System.out.print("\nElement on top of stack is "+Stk[tos]); 

else

System.out.print("\nEmpty stack. No element present.");

}

void fnDisplay()

{

if(!isEmpty())

{

System.out.print("\nElements in stack : ");

for(int i=0;i<=tos;i++)

System.out.print(Stk[i]+" ");

}

else

System.out.println("\nEmpty stack. No elements present. ");

}

}


class StackDS

{

public static void main(String []as)throws IOException

{

Scanner sc = new Scanner(System.in);

System.out.print("\nEnter stack size : ");

int cap = sc.nextInt();

StkDS s = new StkDS(cap);

int choice;

do

{

System.out.print("\nMenu: 1. Push\t2. Pop\t3. TopandPop\t4. Peek\t5. Exit");

System.out.print("\nEnter choice : ");

choice = sc.nextInt();

switch(choice)

{

case 1: System.out.print("\nEnter element : ");

int ele=sc.nextInt();

s.push(ele);

s.fnDisplay();break;

case 2: s.pop();s.fnDisplay();break;

case 3: int val=s.topandpop();

System.out.println("\nDeleted element : "+val);

s.fnDisplay();break;

case 4: s.peek();s.fnDisplay();break;

case 5: System.exit(0);

}

}while(true);

}

}

Selection Sort

 

// 4. Selection Sort

import java.io.*;

import java.util.*;

 

class SelectionSort

{

     public static void main(String [] as) throws IOException

     {

          Scanner sc = new Scanner(System.in);

          int i, j, pos, arrlen, tmp;

         

          System.out.print("Enter array length : ");

          arrlen = sc.nextInt();

         

 

          System.out.println("Enter array elements : ");

          int arr[] = new int[arrlen];

          for(i=0;i<arrlen;i++)

              arr[i]=sc.nextInt();

         

          // Sorting

          for(i=0;i<arrlen-1;i++)

          {

              pos=i;

              for(j=i+1;j<arrlen;j++)

              {

                   if(arr[j]<arr[pos])

                        pos=j;

              }

              tmp=arr[pos];

              arr[pos] = arr[i];

              arr[i] = tmp;

          }

             

          System.out.print("Sorted array : ");

          for(i=0;i<arrlen;i++)

              System.out.print(arr[i]+"  ");

         

     }

}

Insertion sort

 

// 3. Insertion sort

import java.io.*;

import java.util.*;

 

class InsertionSort

{

     public static void main(String [] as) throws IOException

     {

          Scanner sc = new Scanner(System.in);

          int i, j, arrlen, tmp;

         

          System.out.print("Enter array length : ");

          arrlen = sc.nextInt();

         

          System.out.println("Enter array elements : ");

          int arr[] = new int[arrlen];

          for(i=0;i<arrlen;i++)

              arr[i]=sc.nextInt();

         

          // Sorting

          for(i=1;i<arrlen;i++)

          {

              tmp = arr[i];

              for(j=i;j>0;j--)

              {

                   if(tmp>arr[j-1])

                        break;

             

                   arr[j] = arr[j-1];

              }

              arr[j] = tmp;

          }

             

          System.out.print("Sorted array : ");

          for(i=0;i<arrlen;i++)

              System.out.print(arr[i]+"  ");

         

     }

}


Binary search

 

// 2. Binary search

import java.io.*;

import java.util.*;

 

class BinarySearch

{

     public static void main(String [] as) throws IOException

     {

          Scanner sc = new Scanner(System.in);

          int i, ele, arrlen, pos=-1;

          int top, bottom, mid;

         

          System.out.print("Enter array length : ");

          arrlen = sc.nextInt();

         

          System.out.println("Enter array elements : ");

          int arr[] = new int[arrlen];

          for(i=0;i<arrlen;i++)

              arr[i]=sc.nextInt();

         

          System.out.print("Enter element to search : ");

          ele = sc.nextInt();

         

          // Sort the input array

          Arrays.sort(arr);

         

          System.out.print("Entered array (sorted) : ");

          for(i=0;i<arrlen;i++)

              System.out.print(arr[i]+"  ");

         

          bottom = 0;

          top = arrlen-1;

 

          while(true)

          {

              if (bottom > top)

              {

                   System.out.println("\nElement not found in array.");

                   break;

              }

                  

              mid = (int)(bottom+top)/2;

 

              if(arr[mid] == ele)

              {

                   System.out.println("\nElement present in array.");

                   break;

              }

             

              else if (arr[mid] < ele)

                   bottom = mid + 1;

 

              else

                   top = mid - 1;

          }

     }

}

Sequential search

 

// 1. Sequential search

import java.io.*;

import java.util.*;

 

class SequentialSearch

{

     public static void main(String [] as) throws IOException

     {

          Scanner sc = new Scanner(System.in);

          int i, ele, arrlen, pos=-1;

         

          System.out.print("Enter array length : ");

          arrlen = sc.nextInt();

         

          System.out.println("Enter array elements : ");

          int arr[] = new int[arrlen];

          for(i=0;i<arrlen;i++)

              arr[i]=sc.nextInt();

         

          System.out.print("Enter element to search : ");

          ele = sc.nextInt();

         

          for(i=0;i<arrlen;i++)

          {

              if(arr[i]==ele)

              {

                   pos = i+1;

                   break;

              }

          }

         

          System.out.print("Entered array is : ");

          for(i=0;i<arrlen;i++)

              System.out.print(arr[i]+"  ");

         

          if(pos!=-1)

              System.out.print("\nElement found at position "+pos);

          else

              System.out.println("\nElement not found in array");

     }

}


Queue DS

 /* 

Classes and objects

Queue data structures 

*/


import java.io.*;

import java.util.*;


class QueDS

{

int front, rear, capacity;

int [] Que;


public QueDS(int c)

{

front = -1;

rear = -1;

capacity = c;

Que = new int[capacity];

}

int size()

{

if(isEmpty())

return 0;

return rear-front+1;

}

boolean isFull()

{

return(rear==capacity-1);

}

boolean isEmpty()

{

return ((front==-1) || (front>rear));

}

void enqueue(int x)

{

if(front==-1)

front=0;

if(!isFull())

Que[++rear]=x;

else

System.out.print("\nFull Queue. Cant insert element.");

}

void dequeue()

{

if(!isEmpty())

front++;

else

System.out.print("\nEmpty queue. Cant delete element.");

}

void fnDisplay()

{

if(!isEmpty())

{

System.out.print("\nElements in Queue : ");

for(int i=front;i<=rear;i++)

System.out.print(Que[i]+" ");

}

else

System.out.println("\nEmpty Queue. No elements present. ");

}

}


class QueueDS

{

public static void main(String []as)throws IOException

{

Scanner sc = new Scanner(System.in);

System.out.print("\nEnter queue size : ");

int cap = sc.nextInt();

QueDS q = new QueDS(cap);

int choice;

do

{

System.out.print("\nMenu: 1. Enqueue\t2. Dequeue\t3. Exit");

System.out.print("\nEnter choice : ");

choice = sc.nextInt();

switch(choice)

{

case 1: System.out.print("\nEnter element : ");

                    int ele=sc.nextInt();

                    q.enqueue(ele);

                    q.fnDisplay();

                                            break;

case 2: q.dequeue();

                                            q.fnDisplay();

                                            break;

case 3: System.exit(0);

}

}while(true);

}

}


/*

Sample Output:

ooplab>javac QueueDS.java


ooplab>java QueueDS


Enter queue size : 3


Menu: 1. Enqueue        2. Dequeue      3. Exit

Enter choice : 1

Enter element : 5

Elements in Queue : 5


Menu: 1. Enqueue        2. Dequeue      3. Exit

Enter choice : 1

Enter element : 10

Elements in Queue : 5 10


Menu: 1. Enqueue        2. Dequeue      3. Exit

Enter choice : 1

Enter element : 15

Elements in Queue : 5 10 15


Menu: 1. Enqueue        2. Dequeue      3. Exit

Enter choice : 1

Enter element : 25

Full Queue. Cant insert element.


Elements in Queue : 5 10 15

Menu: 1. Enqueue        2. Dequeue      3. Exit

Enter choice : 2

Elements in Queue : 10 15


Menu: 1. Enqueue        2. Dequeue      3. Exit

Enter choice : 2

Elements in Queue : 15


Menu: 1. Enqueue        2. Dequeue      3. Exit

Enter choice : 2

Empty Queue. No elements present.


Menu: 1. Enqueue        2. Dequeue      3. Exit

Enter choice : 2

Empty queue. Cant delete element.

Empty Queue. No elements present.


Menu: 1. Enqueue        2. Dequeue      3. Exit

Enter choice : 3


ooplab>

*/