Circular Linked Lists - Data Structures

There are some times data can be efficiently stored in a linked list like data structure but in a circular fashion. Just like a wheel. That is when circular linked lists come to play.


Basics about RR algorithm 

Round-robin (RR) is one of the algorithms employed by process and network schedulers in computing. As the term is generally used, time slices are assigned to each process in equal portions and in circular order, handling all processes without priority (also known as cyclic executive).  - Wikipedia

What happens basically in RR is to schedule processes in an OS or a packets in a network. Each process is given a time slice to execute (ie: a certain time period ). When time slice is for a process is over it is being interrupted no matter the process is completed or not. Inactive processes are removed. Each active process is given a time slice.

RR can be implemented using linked list as well.

process p = Linked List.removeFirst( )       // remove process from first
Give a time slice to process p
Linked List.addLast(p)         // add process to last

Although this is possible with a singly linked list there are inefficiencies. This is because the same node removed from the first and inserted at the last end. Plus additional computations with regard to this (ex: numberOfNodes --, numberOfNodes ++ ) have to be executed redundantly.


Circular linked list

What is different with circular LL and singly LL ?
-- singly LL's tail node'next refers to null
-- circular LL's tail node'next refers to head node

In circular ll it is enough to maintain head or tale reference because head can be located by tale.next reference.


Image obtained from :https://en.wikipedia.org/wiki/Linked_list



Implementation of Circular Linked List (CCL) ( except node implementation)

public class CircularlyLinkedList<E> {

private Node<E> tail = null;    // tail is referred to null initially
private int size = 0;                  // initial size is is 0

public CircularlyLinkedList( ) { }     // If needed tail and size could be initialized here

public int size( ) { return size; }     // gives number of nodes in CLL

public boolean isEmpty( ) { return size == 0; }       // Checks if CCL is empty


public E first( ) {
  if (isEmpty( )) return null;
  return tail.getNext( ).getElement( );       // return tail.next. which means the first node
}


public E last( ) {
  if (isEmpty( )) return null;
  return tail.getElement( );                  // returns tail node
}


public void rotate( ) {
  if (tail != null)
  tail = tail.getNext( );                      // tail reference is passed to next node
}                                                     // number of nodes doescnt change except for tail position


public void addFirst(E e) {

  if (size == 0) {
    tail = new Node<>(e, null);       // at first the tail is the new node. new node is also the first node
    tail.setNext(tail);
  } 
 else {
    Node<E> newest = new Node<>(e, tail.getNext( ));     // newly added node refers to previous head
    tail.setNext(newest);                                           // tail stays the same because insertion is at front. new tail.next that means head refers to the newly added node
 }
 size++;
}



public void addLast(E e) {
   addFirst(e);                           // add a node to front
   tail = tail.getNext( );            // set tail to front added node
}


public E removeFirst( ) {
  if (isEmpty( )) return null;                   // cant remove nodes from an empty list

 Node<E> temp = tail.getNext( );              // head node is assigned to a node

 if (head == tail) tail = null;                    // When there is only one node left

 else {
   tail.setNext(head.getNext( ));           // tail stays the same. tail.next is referred to temp.next
   size−−;
   return head.getElement( );
 }
}

Share:

Singly Linked List - Data structure

One major advantage with using arrays as a data structure is it is always fixed size. Linked lists data structure can solve that storage limit with arrays. Lists have in flavours of singly and doubly. We will consider singly lists here.

Singly linked lists are like a chain. You have the first node and you can keep adding nodes for infinity. One thing is to identify the ends of the chain we have some mechanisms like painting chain ends in different colors.

Basic feature of linked lists

Linked list is composed node nodes.
Each node has to parts. 
- First part is to store data(ints, strings or object types). 
- Second part is to store a reference to the next node. (Like in the chain example)
First node is called the head node. (This is how we identify the front)
Last node is marked as the node where its next is null which means not references to any node
size can be introduced as a variable to detect lists number of items in any given time.



Image obtained from : http://www.cs.cmu.edu/~adamchik/15-121/lectures/Linked%20Lists/linked%20lists.html




Node declaration in Java (For illustration purposes)



private static class Node<E>
{
   private E data;
   private Node<E> next;

   public Node(E d, Node<E> n)
   {
      data = d;
      next = n;
   }
}

Linked List Operations in Java - (For illustration purposes)


Adding a data item from the head of the lists

Process addFirst(e):
newest = Node(e)       // New node is created to add first
newest.next = head    // created nodes next reference is pointed to current head node
head = newest            // Set newly added node as the head
size = size + 1           // now that a new node is added to the chain increment its size by 1


Adding a data item from the back of the list

Process addLast(e):
newest = Node(e)       // create a new node to add at the last
newest.next = null     // to make newly created node last node, set its next to last
tail.next = newest      // set current tails next to newly crated node. So that current tails is now 1 node before last
tail = newest              // set tail to newly created node so that it is officialy the last node
size = size + 1           // now that a new node is added to the chain increment its size by 1

Remove data item from the head of the list

Process removeFirst( ):
if head == null then the list is empty // This is because cant remove a node from an empty list
head = head.next        // new head is set to next node. So that 2nd node is the new head 
size = size − 1     // New lists has 1 item less. But the reference from old new to new new is there


one major disadvantage in linked lists is it consumes more memory. This is because of the additional part for storing the reference.

See full implementation details here : http://www.cs.cmu.edu/~adamchik/15-121/lectures/Linked%20Lists/linked%20lists.html









Share:

Data structure 1 - Array

One most basic and most popular data structure is array. It is so hard to find a programming language where there is no arrays defined.  Normally arrays can hold primitive data types such as ints, floats, booleans and such but you can create arrays to hold more complex user defined data types as well. In here the focus is not to show you how to create an array, how to insert an item to array and such. Those depends on the prog language you are using. More focus in here is to analyse the data structure and basic actions that can be performed on arrays.

So to have it formally start, array with n elements can have addresses ranging from 0th index to (n-1)th index. Thats it. :) And basically you can insert an item to an array, search for an item and delete an item from array. Here we will look an un-ordered array. We will look at ordered array data structure in a another example. This is because when it is ordered it has some certain advantages. ;)

No duplicates

You can insert duplicate data to an array.There is no problem with that. Lets see such a scenario.

Insert - you can insert your data item to next available space. 1 move.
Search - When you want to search a data item it can be at the first index. So 1 move. Worst case is it can be at the last index. So n moves. So depending on where it resides number of steps differs. So it is fair to say it takes n/2 moves on average for searching.
Delete - Before you search you have to first find it. And after deletion you have to restructure the array so that items from right side of the deleted item are moved 1 place left. So on average for search the item it takes n/2 moves.  So using the same logic it takes on average n/2 moves to restructure. 


With duplicates

Insert -  You can just insert an item to an array. no matter it is a duplicate or not. So 1 move.
Search - You cant just stop finding the first element that you are looking for. Because there can more of that in the array. So you will have to search fro the whole array to search a specific item. So n moves.
Delete - Just like no duplicates example logic, n moves to search. Then on average n/2 moves to restructure after deletion.   
Share:

Data structures and Algorithms overview

In order to proceed with DSA (Data structures and Algorithms) you have to have some basic programming background. It is not always the case. You  can learn the concepts of DSA prior to learning programming. But personally I do not recommend the latter. Just an personal opinion. Lets understand the things very simply. ;)


Data structure 

Normally in a computer program, no matter in what language you write it we need data to work with. These data can be variables (int x= 25, String firstName = "kalpa" , boolean isSignedIn = false) or data that we store in a database (may be in an xml file , database like mySQL or simply a text file). Amount of data and the complexity of data increase the with program scale and complexity. For example when you code a basic calculator you may need some ints doubles, floats. But when it comes to enterprise level real time banking mobile application nature of data differs. 

So not all the data are alike. These data has to be stored in some sort of manner in order to use it. Hence we need some kind of structure to hold on that data. (no matter the data is in variables or a database).





Simply saying you cant store eggs and oranges in the same manner. You need specific structures to each type. Read more about DS from here.

Algorithms

Well, the name seems cool mathematicy, cody and geeky. But in simple terms an algorithm is way of solving a problems. In day to day life we use algorithms all the time. For example when we want to tie our shoes we have certain way answering that problem. You take the shoe laces tie it in a certain fashion. VIOLA ! your shoe is laced. :D all the time, in every nook and corner in the world this algorithm is the same. 
Algoithm can be simple as this

or it can be complex as this. :D

Normally algorithms are kind complex. ;)

Algorithms can be used with data structure also.
  • To insert a new data item (Adding a new orange to the orange basket basket)
  • To search for a specific data item (search a specific orange from the basket. This may take some time depending in how you have strucutred your oranges)
  • To delete a data item (get an odd yellow colored orange and throw it to garbage can.)
For more on algos read some from here. 



Share:

Android Emulator not starting on ubuntu - Android studio

If you are using Android studio on ubuntu there are can be number of reasons why Android studio cant  start an instance of AVD. Emulator is a one needy insane bugger you always want to work with and you have to tolerate with.  Here the focus is on a specific issue of

"PANIC: HOME is defined but could not find TESTPHONE.ini file in $HOME\.android\avd
(Note: avd is searched in the order of $ANDROID_AVD_HOME,$ANDROID_SDK_HOME\.android\avd and $HOME\.android\avd)"

Normally what happens in Ubuntu is you either download Android Studio + Open JDK or Orcale JDK via bash or you download the zip files and install java and install Android-studio.

If java is there Android studio will work just fine. Most probably you will have to go to 
<Android-studio extracted location>/bin/   and ./studio.sh there. Most of the time you will do this as root.

If you have successfully installed Android in your system what it does is in your profile it makes a hidden directory called .android and your avd instance settings will be saved in a directory called avd and there you can find your your_test_avd.ini file

If you are root when you are installing android what happens in these .android/avd directories are created under root users folder and when you try to execute avd manager and start a avd as a normal users there are no .ini files in  <your profiile>/.android/avd.

There are 2 things that you can do to get it over with.

1. Copy /root/.android/avd content to $HOME/.android/avd. Just a matter of simple command and worked for me. :D

2. Before android search .ini files in $HOME/.android/avd it looks for environmental variables called 
$ANDROID_AVD_HOME. SO you can set its value to root/.android/avd. :) (y)



Share:

Sneak peak at Arduino platform


Just like I said. Arduino basically composed of two parts.

  1. 1. hardware (Arduino board)
  2. 2.software (The programs we write to Arduino)

Imagine Arduino is like a mother board of your computer. Each mother board has its form factor. Meaning each board is made according to a specification like these number of components should be there, these components should be placed there, these are the voltages to be used like that.
Well Arduino boards also has its own classifications.

Basically there are 5 hardware classes in which any Arduino board falls in.  These hardware are classed according to their functionality and specifications. So each class is used for specific tasks. Of course, you cant have simple LED blinking Arduino board to enterprise level highly complex robots. Can you ? :D
Each hardware class has Arduino models and here are the classes and some models.

Hardware class - Models - Functions

Basic - Uno, Leanardo - Most popular class. Good for experiments

Mega - Mega2560, MegaADK - Good for complex robots, More external device connections

Mini - MiniPro, Nano - Ideal for space utilization when building small robots

Breadboard - Boarduino - A solderless breadboard, Good for ever changing projects because you can attach parts anytime

Specialty - Lilypad, Fio, Ethernet - For specific application usages


This is some model comparison from Arduino site.
http://arduino.cc/en/Products.Compare

More on these components later. :D





These are some comparisons of Arduino models I`ve found on the web. Check them also.

http://www.robotshop.com/blog/en/arduino-microcontroller-feature-comparison-2-3631
https://learn.adafruit.com/adafruit-arduino-selection-guide/arduino-comparison-chart





Share:

Intro to Arduino



As a simplified version Arduino can be introduced to some novel people to hardware, micro controller, electronic,computer field as a mini computer. It is 100% not true. But lets just stick to it now. So just like a normal computer you have everything there. A memory, hard disk, usb connections, CPU like all of them.

But when it comes to micro controllers one thing not as normal computing is everything is micro-ed. :D. Meaning you have RAM but not as a large one like 4GB. You have only 2KB or so. You dont have 500GB hard disk. Instead you have 40 KB flash memory. Yeah , it may seem not enough. But when it comes to micro things like sensor works, robotics it is more than enough. :)

Micro Controller is a small computer like thingy which is embedded in an IC (Integrated Circuit). Lets just leave it at that. Arduino is a micro controlled platform where you can connect with external components like usb, sensors, motors. Arduino is an open source platform which was initiated by some Itallian fellows for an art project. You can read the history of Arduino in the wiki also. How ever now it has expanded its wings and now it can build robots also.

Arduino is piece of electronic junk with out its software .Arduino software is his soul mate. Together they live or without they die, in this case not function. You can plug your Arduino board to you computer, run Arduino software there and upload it to the Arduino board and viola. Arduino is live it can do stuff.  

You can get to know about more on Arduino from its official site and wiki.
Share: