Showing posts with label ds. Show all posts
Showing posts with label ds. Show all posts

August 03, 2021

Profile creation and data structure concepts examples

Found few good tutorials and capture the profile building and data-structure concepts examples.

Profile creation tips and related sites:

https://github.com/ramanujadasu/golden-profile-tips/tree/main/profilecreationtips

Data structure concepts and example git repos:

https://github.com/ramanujadasu/golden-profile-tips/tree/main/datastructures

Visual view for DataStructures:

https://visualgo.net/en/bst

Java 9 Features:

https://www.youtube.com/watch?v=oRcOiGWK9Ts

Read more ...

June 09, 2012

DS: Circular Queue

import java.util.Scanner;

public class CQueue {
 public static void main(String args[]) {

  Scanner s = new Scanner(System.in);
  int choice, n;
  Object value;
  System.out.println("Enter a size");
  n = s.nextInt();

  List c = new List(n);
  do {
   System.out
     .println("CQueue operations\n1.puts()\n2.remove()\n3.getfrontelement()\n4.getrearelement()\n5.empty()\n6.size()\n7.display()\nEnter ur choice");
   choice = s.nextInt();
   switch (choice) {
   case 1:
    System.out.println("Enter a value");
    value = s.nextInt();
    c.puts(value);
    break;
   case 2:
    System.out.println("The deleted element is=" + c.remove());
    break;
   case 3:
    System.out.println("The front element is=" + c.front_element());
    break;
   case 4:
    System.out.println("The rear element is=" + c.rear_element());
    break;
   case 5:
    if (c.isempty())
     System.out.println("CQueue is not empty");
    else
     System.out.println("CQueue is not empty");
    break;
   case 6:
    System.out.println("The size is=" + c.size());
    break;
   case 7:
    System.out.println("The elements in the queue is=" + c);
    break;
   }
  } while (choice < 8);

 }
}

class List {
 int front, rear;
 Object x[];
 int size = 0;

 public List(int y) {
  front = -1;
  rear = -1;
  x = new Object[y];

 }

 public int size() {
  return size;
 }

 public boolean isempty() {
  return front == -1;
 }

 public void puts(Object value) {

  Scanner s = new Scanner(System.in);
  if (rear == x.length - 1)
   rear = 0;
  else
   rear++;
  if (front == rear) {
   if (rear == 0)
    rear = x.length - 1;
   else
    rear--;

   System.out.println("Queue is overflow");
   return;
  } else {
   if (front == -1)
    front = 0;
   x[rear] = value;
  }
  size++;
 }

 public Object remove() {

  if (front == -1) {
   System.out.println("Queue is underflow");
   return null;
  }
  Object value = x[front];
  x[front] = null;
  if (front == rear) {
   front = rear = -1;

  } else {
   if (front == x.length - 1)
    front = 0;
   else
    front++;
  }
  size--;
  return value;

 }

 public String toString() {
  StringBuffer y = new StringBuffer("[");
  if (front < rear) {
   for (int i = front; i <= rear; i++) {
    if (x[i] == (Object) 0)
     y.append("null,");
    else
     y.append(x[i] + " ");

   }
  } else {
   for (int i = front; i < x.length; i++) {
    if (x[i] == (Object) 0)
     y.append("null,");
    else
     y.append(x[i] + " ");

   }

   for (int i = 0; i <= rear; i++) {
    if (x[i] == (Object) 0)
     y.append("null,");
    else
     y.append(x[i] + ", ");

   }

  }
  y.append("]");
  return new String(y);

 }

 public Object front_element() {
  if (front == -1) {
   System.out.println("Queue is underflow");
   return null;

  }
  return x[front];

 }

 public Object rear_element() {
  if (front == -1) {
   System.out.println("Queue is underflow");
   return null;

  }
  return x[rear];

 }
}
Output:

Enter a size                                                                   
5                                                                               
CQueue operations                                                              
1.puts()                                                                       
2.remove()                                                                      
3.getfrontelement()                                                            
4.getrearelement()                                                             
5.empty()                                                                      
6.size()                                                                       
7.display()                                                                    
Enter ur choice                                                               
 1                                                                               
Enter a value                                                                  
10                                                                             
CQueue operations                                                               
1.puts()                                                                       
2.remove()                                                                     
3.getfrontelement()                                                             
4.getrearelement()                                                             
5.empty()                                                                      
6.size()                                                                        
7.display()                                                                    
Enter ur choice                                                                
1                                                                               
Enter a value                                                                 
 20                                                                             
CQueue operations                                                              
1.puts()                                                                       
2.remove()                                                                     
3.getfrontelement()                                                            
4.getrearelement()                                                             
5.empty()                                                                      
6.size()                                                                       
7.display()                                                                     
Enter ur choice                                                                
1                                                                              
Enter a value                                                                   
30                                                                             
CQueue operations                                                              
1.puts()                                                                        
2.remove()                                                                     
3.getfrontelement()                                                            
4.getrearelement()                                                              
5.empty()                                                                      
6.size()                                                                       
7.display()                                                                    
Enter ur choice  
1
Enter a value
40
CQueue operations
1.puts()
2.remove()
3.getfrontelement()
4.getrearelement()
5.empty()
6.size()
7.display()
Enter ur choice
2
The deleted element is=10                                                      
CQueue operations                                                              
1.puts()                                                                       
2.remove()                                                                     
3.getfrontelement()                                                            
 4.getrearelement()                                                             
5.empty()                                                                      
6.size()                                                                        
7.display()                                                                   
Enter ur choice  
8   

Read more ...

DS: Bubble Sort Program


import java.util.Scanner;

public class Bubblesort {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter size of the array");
int size = sc.nextInt();
bsort s = new bsort(size);
System.out.println("Elements before sorting");
s.display();
s.bubble();
System.out.println("Elements after sorting");
s.display();
}
}

class bsort {
int a[], N;
Scanner sc = new Scanner(System.in);

public bsort(int size) {
a = new int[size];
N = size;
System.out.println("Enter elements into the array");
for (int i = 0; i < N; i++) {
a[i] = sc.nextInt();
}
}

public void bubble() {
int last = N;
for (int pass = 0; pass < N - 1; pass++) {
int ex = 0;
for (int j = 0; j < last - 1; j++) {
if (a[j] > a[j + 1]) {
int temp = a[j];
a[j] = a[j + 1];
a[j + 1] = temp;
ex = ex + 1;
}
}
if (ex != 0)
last = last - 1;
else
return;
}
}

public void display() {
for (int i = 0; i < N; i++) {
System.out.print(" " + a[i]);
}
System.out.println();
}
}

Output:

Enter size of the array
5
Enter5  elements into the array
96
73
68
63
60
Elements before sorting  
96 73 68 63 60 
Elements after sorting 
60 63 68 73 96                                                                                                                                                                                                   
Read more ...

DS: Binary Tree Program


import java.util.Scanner;

public class BTree {
public static void main(String args[]) {

List c = new List();
int d;
Scanner s = new Scanner(System.in);
do {
System.out.println("Binary tree operations");
System.out.println("1.create()");
System.out.println("2.inorder()");
System.out.println("3.preorder()");
System.out.println("4.postorder()");
System.out.println("5.empty()");
System.out.println("Enter ur choice");
d = s.nextInt();
switch (d) {
case 1:
c.create();
break;
case 2:
c.inorder(c.root);
break;
case 3:
c.preorder(c.root);
break;
case 4:
c.postorder(c.root);
break;
case 5:
if (c.isempty())
System.out.println("BTree is empty");
else
System.out.println("BTree is not empty");
break;

}

} while (d < 6);

}

}

class Node {
Object data;
Node left;
Node right;
}

class List {
Node s[] = new Node[10];
int top;
Object value;
Node root;
Node p, temp;
Scanner sc = new Scanner(System.in);

public List() {
top = 0;
s[top] = null;

}

public boolean isempty() {
return root == null;

}

public void create() {
root = new Node();
System.out.println("Enter a value");
Object value;
value = sc.nextInt();
String ch;
root.data = value;
p = root;
while (p != null) {
System.out.println("The right child" + p.data + "is exists");
ch = sc.next();
if (ch.equals("n"))
p.right = null;
else {
temp = new Node();
System.out.println("Enter a value");
value = sc.nextInt();
temp.data = value;
p.right = temp;
push(temp);

}
System.out.println("The left child of " + p.data + "is exists");
ch = sc.next();
if (ch.equals("n")) {
p.left = null;
p = pop();
} else {
temp = new Node();
System.out.println("Enter a value");
value = sc.nextInt();
temp.data = value;
p.left = temp;
p = temp;
}
}

}

public void push(Node value) {
if (top == s.length - 1) {
System.out.println("Stack is overflow");
return;

} else
s[++top] = value;

}

public Node pop() {
if (top == -1) {
System.out.println("Stack is underflow");
return null;
}
Node x = s[top--];
return x;
}

public void inorder(Node p) {
if (p != null) {

inorder(p.left);
System.out.print(p.data + " ");
inorder(p.right);
}

}

public void preorder(Node p) {
if (p != null) {

System.out.print(p.data + " ");
preorder(p.left);
preorder(p.right);

}

}

public void postorder(Node p) {
if (p != null) {

postorder(p.left);
postorder(p.right);
System.out.print(p.data + " ");

}

}

}

Output:
 Binary tree operations
1.create()
2.inorder() 
3.preorder()
4.postorder()
5.empty() 
Enter ur choice
 1
Enter a value
10   
The right child10is exists
Y
 Enter a value
20
The left child of 10is exists
Y
 Enter a value   
 30
 The right child30is exists
 Y
 Enter a value 
 40   
The left child of 30is exists             
n
The right child40is exists
n
The left child of 40is exists
The right child20is exists
n  
The left child of 20is exists
n
                                                                                                                                                      Binary tree operations                                                       
1.create()
2.inorder()
3.preorder()
4.postorder()
5.empty() 
Enter ur choice
 2
The  inorder is =30 ,40, 10, 20,
Binary tree operations
1.create()
2.inorder()
3.preorder() 
4.postorder()
5.empty() 
Enter ur choice
3
The preorder is=10 30 40 20
Binary tree operations
1.create()
2.inorder()
3.preorder()
4.postorder()
5.empty()
Enter ur choice 
4
The post order is=40 30 20 10
Binary tree operations
1.create()
2.inorder()
3.preorder()
4.postorder()
5.empty()
Enter ur choice
5
BTree is not empty

Binary tree operations
1.create()
2.inorder()
3.preorder()
4.postorder()
5.empty()
Enter ur choice
6
Read more ...

DS: Binary Search Tree Program

import java.io.*;
import java.util.Scanner;

public class BinaryOne {
public static void main(String args[]) {
int choice, value;
bst b = new bst();
do {
System.out.println("Binary Search Tree Operations");
System.out.println("1.create()");
System.out.println("2.display()");
System.out.println("3.empty()");
System.out.println("Enter U'r choice");
Scanner s = new Scanner(System.in);
choice = s.nextInt();
switch (choice) {
case 1:
System.out.println("Enter item");
value = s.nextInt();
b.insert(value);
break;
case 2:
b.inorder(b.root);
break;
case 3:
if (b.equals(null))
System.out.println("BSTree is empty");
else
System.out.println("BSTree is not empty");
break;
}
} while (choice < 4);
}
}

class node {
node right;
node left;
int data;
}

class bst {
node root, loc, par;
int item;

public void find(int x) {
int item;
item = x;
node ptr, save;
if (root == null) {
loc = null;
par = null;
return;
}
if (item == root.data) {
loc = root;
par = null;
return;
}
if (item < root.data) {
ptr = root.left;
save = root;
} else {
ptr = root.right;
save = root;
}
while (ptr != null) {
if (item == ptr.data) {
loc = ptr;
par = save;
return;
}
if (item < ptr.data) {
save = ptr;
ptr = ptr.left;
} else {
save = ptr;
ptr = ptr.right;
}
}
loc = null;
par = save;
return;
}

public void insert(int item) {
node temp;
find(item);
if (loc != null) {
System.out.println(item + "is already exists");
return;
}
temp = new node();
temp.data = item;
temp.left = null;
temp.right = null;
loc = temp;
if (par == null)
root = temp;
else {
if (item < par.data)
par.left = temp;
else
par.right = temp;
}
}

public void inorder(node p) {
if (p != null) {
inorder(p.left);
System.out.println("  " + p.data);
inorder(p.right);
}
}
}



Output:
Binary Search Tree Operations
1.create()                                                                    
2.display()  
3.empty()                                                                 
Enter U'r choice                                                               
1                                                                              
Enter item                                                                      
22
Binary Search Tree Operations                                                                            
1.create()                                                                      
2.display()                                                                    
3.empty()

Enter U'r choice                                                               
1                                                                               
Enter item                                                                     
1                                                                              
Binary Search Tree Operations
1.create()                                                                      
2.display() 
3.empty()                                                                  
Enter U'r choice                                                               
1                                                                               
Enter item                                                                     
76                                                                             
Binary Search Tree Operations
1.create()                                                                      
2.display()                               
3.empty()                                    
Enter U'r choice            
2                                                                                
1                                                                               
22                                                                             
54                                                                             
76                                                                            
Binary Search Tree Operations
1.create()                                                                     
2.display()
3.empty()                                                                   
Enter U'r choice 
3                                                                              
BSTree is not empty  
Binary Search Tree Operations
1.create()                                                                     
2.display()
3.empty()                                                                    
Enter U'r choice 
4                                                             
Read more ...

My Favorite Site's List

#update below script more than 500 posts