Archive for the ‘Data Structures’ Category

JAVA LinkedList Class simple example

Wednesday, August 7th, 2013

import java.util.*;

public class LinkedListDemo {

public static void main(String args[]) {
// create a linked list
LinkedList ll = new LinkedList();
// add elements to the linked list
ll.add(“F”);
ll.add(“B”);
ll.add(“D”);
ll.add(“E”);
ll.add(“C”);
ll.addLast(“Z”);
ll.addFirst(“A”);
ll.add(1, “A2”);
System.out.println(“Original contents of ll: ” + ll);

// remove elements from the linked list
ll.remove(“F”);
ll.remove(2);
System.out.println(“Contents of ll after deletion: ”
+ ll);

// remove first and last elements
ll.removeFirst();
ll.removeLast();
System.out.println(“ll after deleting first and last: ”
+ ll);

// get and set a value
Object val = ll.get(2);
ll.set(2, (String) val + ” Changed”);
System.out.println(“ll after change: ” + ll);
}
}

Original contents of ll: [A, A2, F, B, D, E, C, Z]
Contents of ll after deletion: [A, A2, D, E, C, Z]
ll after deleting first and last: [A2, D, E, C]
ll after change: [A2, D, E Changed, C]

credit: http://www.tutorialspoint.com/java/java_linkedlist_class.htm

JAVA simple Deque Interface example

Wednesday, August 7th, 2013

import java.util.*;

class Deque {
public static void main(String[] arguments){

ArrayDeque<String> ourDeque = new ArrayDeque<String>();

// fill our deque interface
String[] alph = { “a”,”b”,”c”,”d”,”e”,”f”,”g”,”h”,”i”,”j”,”k”,”l”,”m”,”n”,”o”,”p”,”q”,”r”,”s”,”t”,”u”,”v”,”w”,”x”,”y”,”z” };
for( int i = 0; i < alph.length; i++){
ourDeque.addFirst( alph[i] );
}

// foreach
for (String str : ourDeque) {
System.out.print(str);
}

System.out.println(“\n”);

// iterator
//ArrayDeque<String> ourDeque = new ArrayDeque<String>();
for (Iterator<String> iter = ourDeque.iterator(); iter.hasNext(); ) {
System.out.print(iter.next());
}

System.out.println(“\n”);

// Let’s do it forward though
int holdSize = ourDeque.size();
for ( int j = 0; j < holdSize; j++){
String getOne = ourDeque.removeLast();
System.out.print(getOne);
}

System.out.println(“\n”);
}
}

JAVA simple HashMap (+Generics) syntax example

Wednesday, August 7th, 2013

import java.util.*;

HashMap<String, Float> quality = new HashMap<>();
float price1 = 3.00F;
quality.put(“mint”, price1);
quality.get(“very fine”);
quality.clear();
quality.containsValue(price1);
quality.containsKey(“good”);

JAVA simple ArrayList example

Wednesday, August 7th, 2013

import java.util.*;

public class CodeKeeper {

ArrayList<String> list;
String[] codes = { “alpha”, “lambda”, “gamma”, “delta”, “zeta” };

public CodeKeeper(String[] userCodes) {

list = new ArrayList<>();
// load codes
for (int i = 0; i < codes.length; i++) {

if (!list.contains(code)) {

list.add(codes[i]);

}

}
// display all codes with new for loop
for (String code : list) {

System.out.println(code);

}

}

public static void main(String[] arguments) {

CodeKeeper keeper = new CodeKeeper(arguments);

}

}

reverse a String

Wednesday, August 7th, 2013

JAVA

class Reverso{
	public static void main(String[] args) {
		for (String toReverse: args) {
			int countOf = toReverse.length();
			for(int $i = toReverse.length() - 1; $i >= 0; $i--){
				System.out.print(toReverse.charAt($i));
			}
		}
	}
}

JavaScript

var reverse = function(str) {
    var arr = [];

    for (var i = 0, len = str.length; i <= len; i++) {
        arr.push(str.charAt(len - i))
    }

    return arr.join('');
}

document.writeln(reverse('lorem ipsum dolor'));

JAVA simple Array refresher & some String methods

Wednesday, August 7th, 2013

int[] holiday = { 1, 15, 50, 148, 185, 246, 281, 316, 326, 359 };
for (int i = 0; i < holiday.length; i++ ) {
someFunc(holiday[i]);
}

 

str.length()
str.charAt(6)
str.substring(26, 32)
str.indexOf(‘a’)
str.toUpperCase())

JAVA simple BitSet refresher

Tuesday, August 6th, 2013

import java.util.*;

BitSet connex = new BitSet();
connex.set(3);
connex.get(3);
connex.clear(3);
int numBits = connex.size();

PHP SPL FixedArray example

Monday, August 5th, 2013

// Initialize the array with a fixed length
$array = new SplFixedArray(5);

$array[1] = 2;
$array[4] = “foo”;

// Increase the size of the array
$array->setSize(10);

$array[9] = “asdf”;

// Shrink the array to a size of 2
$array->setSize(2);

PHP DoublyLinkedList example

Monday, August 5th, 2013

/*

* Examples of DoublyLinkedList
*/

$obj = new SplDoublyLinkedList();

// Check wither linked list is empty
if ($obj->isEmpty())
{
echo “Adding nodes to Linked List<br>”;
$obj->push(2);
$obj->push(3);

echo “Adding the node at beginning of doubly linked list <br>”;
$obj->unshift(10);
}

echo “<br>Our Linked List:”;
print_r($obj);

echo “<br>Pick the node from beginning of doubly linked list”;
echo $obj->bottom();

/**
Our Linked List:SplDoublyLinkedList Object ( [flags:SplDoublyLinkedList:private] => 0 [dllist:SplDoublyLinkedList:private] => Array ( [0] => 10 [1] => 2 [2] => 3 ) )
Pick the node from beginning of doubly linked list10
*/

Simple Linked List example JavaSript

Wednesday, July 31st, 2013
// items of the list
var bird = {name:'Mockingbird'};
var cat = {name:'Keyboard'};
var dog = {name:'Fort'};
var duck = {name:'Scrooge'};

// the linked-list itself
var animals = {
    head : bird
};

// each item of the list stores a reference to the next one
bird.next = cat;
cat.next = dog;
dog.next = duck;

// output
var animal = animals.head;
while (animal) {
    console.log(animal.name);
    animal = animal.next;
}

source: http://blog.millermedeiros.com/linked-lists/