Saturday, May 19, 2012

Android: Queue and Stack

When we talk about Data Structure, the most common terms would most probably be Queue and Stack. A Queue is pretty much the same as a real world Queue (first come first serve theory) and a Stack would be pretty much similar as a Stack of books (first item will be the last to be taken out).

Time to look at my codes. :D
SimpleDataStructureActivity.java
package zcs.dataStructure;

import zcs.utility.dataStructure.Queue;
import zcs.utility.dataStructure.Stack;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class SimpleDataStructureActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        //Grab the TextView first
        TextView txtView;
        txtView = ((TextView)findViewById(R.id.lblView));
        txtView.setText("");
        
        //Following codes shows how to use a Queue
        Queue tmpQueue = new Queue();
        txtView.append("====Queue====\n");
        int tempNum;
        for(int i = 0; i < 5; i ++)
        {
         tempNum = (int) Math.floor(Math.random() * 1000);
            txtView.append("Insert " +
              "" + tempNum + " into Queue\n");
            tmpQueue.push(tempNum);
            txtView.append("Queue has " +
              "" + tmpQueue.length() + " items.\n");
        }
        for(int i = 0; i < tmpQueue.length(); i ++)
        {
         tempNum = ((Integer)tmpQueue.getValue(i));
         txtView.append("Item " + i + "" +
          " has a value of " + tempNum +".\n");
        }
        for(int i = 0; i < 5; i ++)
        {
         tmpQueue.pop();
            txtView.append("Queue has " +
              "" + tmpQueue.length() + " items.\n");
            if(!tmpQueue.isEmpty())
            {
             for(int j = 0; j < tmpQueue.length(); j ++)
             {
              tempNum = ((Integer)tmpQueue.getValue(j));
              txtView.append("Item " + j + "" +
               " has a value of " + tempNum +".\n");
             }
            }
        }
        txtView.append("\n\n");
        
        //Following codes shows how to use a Stack
        Stack tmpStack = new Stack();
        txtView.append("====Stack====\n");
        for(int i = 0; i < 5; i ++)
        {
         tempNum = (int) Math.floor(Math.random() * 1000);
            txtView.append("Insert " +
              "" + tempNum + " into Stack\n");
            tmpStack.push(tempNum);
            txtView.append("Stack has " +
              "" + tmpStack.length() + " items.\n");
        }
        for(int i = 0; i < tmpStack.length(); i ++)
        {
         tempNum = ((Integer)tmpStack.getValue(i));
         txtView.append("Item " + i + "" +
          " has a value of " + tempNum +".\n");
        }
        for(int i = 0; i < 5; i ++)
        {
         tmpStack.pop();
            txtView.append("Stack has " +
              "" + tmpStack.length() + " items.\n");
            if(!tmpStack.isEmpty())
            {
             for(int j = 0; j < tmpStack.length(); j ++)
             {
              tempNum = ((Integer)tmpStack.getValue(j));
              txtView.append("Item " + j + "" +
               " has a value of " + tempNum +".\n");
             }
            }
        }
    }
}

Node.java
package zcs.utility.dataStructure;

public class Node {
 
 //The Next Node
 private Node _nextNode = null;

 public Node nextNode() {
  return _nextNode;
 }

 public void nextNode(Node _tempNode) {
  this._nextNode = _tempNode;
 }
 
 //The Prev Node
 private Node _prevNode = null;

 public Node prevNode() {
  return _prevNode;
 }

 public void prevNode(Node _tempNode) {
  this._prevNode = _tempNode;
 }

 //THe Node data
 private Object _data = null;
 
 public Object data() {
  return _data;
 }

 public void data(Object _data) {
  this._data = _data;
 }
}

List.java
package zcs.utility.dataStructure;

public class List {
 protected Node head = null;

 /**
  * Number of items in the list
  * @return int
  */
 public final int length()
 {
  int count = 0;
  Node tempNode = null;
  tempNode = head;
  if(!isEmpty())
  {
   count = 1;
   while(tempNode.nextNode() != null)
   {
    count ++;
    tempNode = tempNode.nextNode();
   }
  }
  return count;
 }
 
 /**
  * Is the list empty
  * 
* -> true when empty *
* -> false when not empty * @return true or false */ public final boolean isEmpty() { return (head == null); } /** * Get the data of the selected Item in the list * based on count * @return Object */ public Object getValue(int count) { Node tempNode = null; int tempCount = 0; if(isEmpty()) { return null; }else{ if(this.length() < count) { return null; }else{ tempNode = head; while(tempCount != count) { tempCount ++; tempNode = tempNode.nextNode(); } return tempNode.data(); } } } /** * Push a Object into the List */ public void push(Object tempObj) { Node newNode = new Node(); newNode.data(tempObj); Node tempNode; if(!isEmpty()) { tempNode = head; while(tempNode.nextNode() != null) { tempNode = tempNode.nextNode(); } tempNode.nextNode(newNode); newNode.prevNode(tempNode); }else{ head = newNode; } } /** * Pop item from the List * @return Object */ public Object pop() { return null; } }

Queue.java
package zcs.utility.dataStructure;

public class Queue extends List {
 /**
  * Pop first item from the List
  * @return Object
  */
 public Object pop()
 {
  Node tempNode = null;
  Node nextNode = null;
  if(!isEmpty())
  {
   tempNode = head;
   if(tempNode.nextNode() != null)
   {
    nextNode = tempNode.nextNode();
    nextNode.prevNode(null);
   }
   head = nextNode;
   tempNode.nextNode(null);
   return tempNode.data();
  }else{
   return null;
  }
 }
}

Stack.java
package zcs.utility.dataStructure;

public class Stack extends List {
 /**
  * Pop last item from the List
  * @return Object
  */
 public Object pop()
 {
  Node tempNode = null;
  Node prevNode = null;
  if(!isEmpty())
  {
   tempNode = head;
   while(tempNode.nextNode() != null)
   {
    tempNode = tempNode.nextNode();
   }
   prevNode = tempNode.prevNode();
   if(prevNode != null)
   {
    prevNode.nextNode(null);
   }else{
    head = null;
   }
   tempNode.prevNode(null);
   return tempNode.data();
  }else{
   return null;
  }
 }
}

AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="zcs.dataStructure"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="7" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name=".SimpleDataStructureActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

* Click here for the source files.

No comments:

Post a Comment