10.06.08

Testing Our implementation of LinkedList…

Posted in DataStructures at 11:07 pm by vchaithanya

package com.test.ds;

import java.util.*;

public class LinkedListTest {

 public static void main(String[] arg){
  CLinkedList clist = new CLinkedList();
  clist.addFirst(1);
  clist.addFirst(2);
  clist.addFirst(3);
  
  clist.addFirst(4);
  
  clist.addLast(5);
  clist.addLast(6);
  clist.addLast(7);
  clist.print();
  clist.reverse();
  clist.print();
  /*ArrayList list = new ArrayList();
  list.add(“1″);
  list.add(“5″);
  list.add(“3″);
  for(Iterator itr = (Iterator)list.iterator();itr.hasNext();){
   System.out.println(itr.next());
  }
  list.add(“9″); */
  
 }
}

Implementing Linked List…

Posted in DataStructures at 10:10 pm by vchaithanya

package com.test.ds;

public class CLinkedList {
 Node head;
 CLinkedList(){  }
 public void addLast(int data)
 {
  Node node = new Node(data, null);
  if(head == null){
   head = node;
   return;
  }
  Node next = head;
  while(next.link != null) {
   next = next.link;
  }
  next.link = node;
 
 }
 public void addFirst(int data){
  Node node = new Node(data, head);
  head = node;
 }
 public void print()
 {
  Node next = head;
  for(;next!=null;next=next.link){
   System.out.println(next);
  }
 }
 //reversing a linked list
 public void reverse()
 {
  Node prev = null;
  while(head!=null){
   Node tmp = head;
   head = head.link;
   tmp.link = prev;
   prev = tmp;
  }
  head = prev;
 }
 

}
class Node {
 int data;
 Node link;
 public Node() {
  data = 0;
  link = null;
 }
 public Node(int data, Node link) {
  this.data = data;
  this.link = link;
 }
 public String toString(){
  return(“data value is :”+data);
 }
}

Different operations of BinaryTree…

Posted in DataStructures at 10:07 pm by vchaithanya

//depth of a binary Tree
/*
int depth(Root T)
{
if(T == NULL)
return 0;
d1 = depth(T -> left);
d2 = depth(T -> right);
return (max(d1,d2) + 1);
}
*/
// Print binary tree
/*
Void print(Tree t){
If(t==null)
Return;
s.o.p(t.value);
Print(t.left);
Print(t.right);
}
*/
//Find out total number of nodes at level L?
//Note: pass the curlevel as 0;
/*
int A[MAX_LEVELS];
void DFS(Tree root, int curlevel)
{
if(root == NULL) return;
A[curlevel] = A[curlevel] + 1;
DFS(root->left, curlevel+1);
DFS(root->right, curlevel+1);
}
*/
//Inserting a node into Binary Tree
/*
Void Insertion(Tree t, int ele)
{
if(t!=null)
{
if(t.ele>ele)
{
if(t.left!=null)
{ Insertion(t.left); }
else {
Node n = new Node(ele,null);
t.left = n;
}
}
else
{
if(t.right!=null)
{
Insertion(t.right);
}else{
Node n= new Node(ele,null);
t.right = n;
}
}
}
}
*/

Optimized String Reverse Algorithm…

Posted in Strings at 10:04 pm by vchaithanya

package com.test.stringOps;

public class StringReverse {

public static String reverse(String str) {
if (str == null || str.length() == 0) {
return str;
} else {
StringBuilder bul = new StringBuilder(str);
for(int i=0,j=bul.length()-1;i char c=bul.charAt(i);
bul.setCharAt(i, bul.charAt(j));
bul.setCharAt(j, c);
}
return bul.toString();
}
}
}