Drop Down

Tuesday, March 5, 2019

Dynamic_Stack_Implementation

package ds.Dynamic_Stack;

public class Runner {

public static void main(String[] args)
{
  Stack stack = new Stack();
  stack.size();
  stack.push(10);
  stack.size();
  stack.push(10);
  stack.size();
  stack.push(10);
  stack.size();
  stack.push(10);
  stack.size();
  stack.push(10);
  stack.size();
  stack.size();
  stack.push(10);
  stack.size();
  stack.push(10);
  stack.size();
  stack.pop();
  stack.size();
  stack.pop();
  stack.pop();
  stack.size();
  stack.pop();
  stack.size();
  stack.pop();
  stack.pop();
  stack.pop();
  stack.pop();
  stack.pop();
  stack.pop();
  stack.pop();
  stack.size();

}

}
====================================================================

package ds.Dynamic_Stack;

public class Stack 
{
int[] stack = new int[2];
int  top = 0;
int capacity = 2;
public void push(int data) 
{
if(top == capacity)
expand();
stack[top] = data;
top++;
}

private void expand() 
{
System.out.println("Expanding");
int[] newStack = new int[capacity*2];
System.arraycopy(stack, 0, newStack, 0, stack.length);
stack = newStack;
capacity = capacity *2;
}

public void show() 
{
for(int n :stack)
System.out.print(n+" ");
}

public void pop() 
{
int data;
if(top<=0)
{
System.out.println("Stack is Empty");
return;
}
else
{
top--;
data  = stack[top];
stack[top]= 0;
shrink();
}
System.out.println("Popped Data: "+data);
}

private void shrink() 
{
System.out.println("Shrinked");
  int size = stack.length;
  if(size<capacity/2)
  {
  int s1 = capacity/2;
  int[] ShrinkedArray = new int[s1];
  System.arraycopy(stack, 0, ShrinkedArray, 0, stack.length);
  stack = ShrinkedArray;
  }
}

public int  peek() 
{
if(top<=0)
{
System.out.println("Stack is Empty");
return 0;
}
else
{
int data;
data  = stack[top -1];
return data;
}
}

public void size() 
{
  //return (top);
  System.out.println("Size: "+top);
}

public void  isEmpty() 
{
  if(top<=0)
  System.out.println("Stack is Empty");
  
  else
  System.out.println("Stack is NOT Empty");
  
}

}
===========================================================

Star Patterns

StartPattern14

package interview;
/*

 *     *
  *   *
   * *
    *
    *
   * *
  *   *
 *     *

*/
public class StartPattern14 {

public static void main(String[] args)
{
  for(int i =0;i<5;i++)
  {
  for(int j=0;j<5;j++)
  {
  if(i==j || i+j== 5-1)
  {
  System.out.print("*");
  }else
  {
  System.out.print(" ");
  }
 
  }
  System.out.println();
  }

}

}

StarPattern15

package interview;
/*

*****
*   *
*   *
*   *
*****


*/public class StarPattern15 {

public static void main(String[] args)
{
for(int i =0;i<14;i++)
  {
  for(int j=0;j<14;j++)
  {
  if(i==0 || i==14-1 ||j==0 ||j==14-1)
  {
  System.out.print("*");
  }else
  {
  System.out.print(" ");
  }  
  }
  System.out.println();
  }

}

}

StarPattern13

package interview;
/*

 *     *
  *   *
   * *
    *
    *
   * *
  *   *
 *     *

*/

public class StarPattern13 {

public static void main(String[] args)
{
for(int i = 4;i>=1;i--)
  {
  for(int j = 5;j>i;j--)
  {
  System.out.print(" ");
  }
 
  for(int j = 1;j<(i*2);j++)
  {
if(j>1 && j<(i*2)-1)
{
System.out.print(" ");
}else
{
System.out.print("*");
}  
  }
  System.out.println();
  }
//-----
for(int i = 1 ;i<=4;i++)
  {
  for(int j = 4;j>=i;j--)
  {
  System.out.print(" ");
  }
  for(int j = 1;j<(i*2);j++)
  {
if(j>1 && j<(i*2)-1)
{
System.out.print(" ");
}else
{
System.out.print("*");
}  
  }  
  System.out.println();
  }

}

}

StarPattern12

package interview;
/*
    *
   ***
  *****
 *******
*********
 *******
  *****
   ***
    *
*/

public class StarPattern12 {

public static void main(String[] args)
{

  for(int i = 1 ;i<=5;i++)
  {
  for(int j = 5;j>i;j--)
  {
  System.out.print(" ");
  }
  for(int j = 1;j<=i;j++)
  {
  System.out.print("*");
  }
  //-----------
  for(int j =2;j<=i;j++)
  {
  System.out.print("*");
  }
 
  System.out.println();
  }
//-----------below triangle
  for(int i = 1;i<5;i++)
  {
  for(int j = 0;j<i;j++)
  {
  System.out.print(" ");
  }
  for(int j = 5;j>i;j--)
  {
  System.out.print("*");
  }
  for(int j = 4;j>i;j--)
  {
  System.out.print("*");
  }
  System.out.println();
  }

}

}

StarPattern11

package interview;
/*
     *
    * *
   *   *
  *     *
 *       *
  *     *
   *   *
    * *
     *
*/
public class StarPattern11 {

public static void main(String[] args)
{
  for(int i = 1 ;i<=5;i++)
  {
  for(int j = 4;j>=i;j--)
  {
  System.out.print(" ");
  }
  for(int j = 1;j<(i*2);j++)
  {
if(j>1 && j<(i*2)-1)
{
System.out.print(" ");
}else
{
System.out.print("*");
}  
  }  
  System.out.println();
  }
//-----------below triangle
  for(int i = 4;i>=1;i--)
  {
  for(int j = 5;j>i;j--)
  {
  System.out.print(" ");
  }
 
  for(int j = 1;j<(i*2);j++)
  {
if(j>1 && j<(i*2)-1)
{
System.out.print(" ");
}else
{
System.out.print("*");
}  
  }
  System.out.println();
  }

}

}

Java 8 Notes Pics