Showing posts with label data structure. Show all posts
Showing posts with label data structure. Show all posts

Monday, April 4, 2011

Stack Data Structure in JAVA

stack data structure in java
A stack is a last in, first out (LIFO) abstract data type and data structure. A stack can have any abstract data type as an element, but is characterized by only two fundamental operations: push and pop. The push operation adds an item to the top of the stack, hiding any items already on the stack, or initializing the stack if it is empty. The pop operation removes an item from the top of the stack, and returns this value to the caller. A pop either reveals previously concealed items, or results in an empty stack.

A stack is a restricted data structure, because only a small number of operations are performed on it. The nature of the pop and push operations also means that stack elements have a natural order. Elements are removed from the stack in the reverse order to the order of their addition: therefore, the lower elements are those that have been on the stack the longest

Now let the code talk...
class Olink{
 Object arr[];
 int Size;

 Olink(int Size_Stack){
  arr=new Object[Size_Stack];
  Size=0;
 }
 boolean chk_full(){
  return Size==arr.length;
 }
 boolean chk_empty(){
  return Size==0;
 }
 void push(Object d){
  if(!(chk_full())) arr[Size++]=d;
  else System.out.println("Stack Full !");
 }
 Object pop(){
  if(!(chk_empty())) return arr[--Size];
  else {
   System.out.println("Stack Empty !");
   return null;
  }
 }
 Object peek(){
  if(!(chk_empty())) return arr[Size];
  else {
   System.out.println("Stack Empty");
   return null;
  }
 }
 void show(){
  for (int i=0;i<Size;i++){
   System.out.print(arr[i]+" ");
  }
 }
}
class StackArray{
 public static void main(String [] nix){
  Olink o=new Olink(10);
  o.push(1);
  o.push(2);
  o.push(3);
  o.push(4);
  o.push(5);
  o.push(6);
  o.push(7);
  o.push(8);
  o.push(9);
  o.push(10);
  o.push(11);
  System.out.println("pop : "+o.pop());
  System.out.println("pop : "+o.pop());
  System.out.println("pop : "+o.pop());
  o.show();
 }
}
Firstly we create a stack of size 10 and then push a value on it by pushing we just increment the value of size variable until its value equals to array length, once it is full no value is inserted, to pop a value out from the stack we just decrement the value of size variable and return the value, peek will give the value that will be pop out next.
Continue Reading...

Tuesday, March 29, 2011

Data Structure Singly LinkList in java

java tutorial
Linked lists are among the simplest and most common data structures. They can be used to implement several other common abstract data structures, including stacks, queues, associative arrays, and symbolic expressions, though it is not uncommon to implement the other data structures directly without using a list as the basis of implementation.

The principal benefit of a linked list over a conventional array is that the list elements can easily be added or removed without reallocation or reorganization of the entire structure because the data items need not be stored contiguously in memory or on disk. Linked lists allow insertion and removal of nodes at any point in the list, and can do so with a constant number of operations if the link previous to the link being added or removed is maintained during list traversal.

On the other hand, simple linked lists by themselves do not allow random access to the data other than the first node's data, or any form of efficient indexing. Thus, many basic operations — such as obtaining the last node of the list (assuming that the last node is not maintained as separate node reference in the list structure), or finding a node that contains a given datum, or locating the place where a new node should be inserted may require scanning most or all of the list elements.

Singly Link Ling in java data structure
A linked list whose nodes contain two fields: an integer value and a link to the next node

Now let implement it in java, first we need a class of which we want to make links in the list we call it, say Link, below is the code to that class
class Link{
    private String data;
    private int id;
    private Link next;

    /**
     * @return the data
     */
    public String getData() {
        return data;
    }

    /**
     * @param data the data to set
     */
    public void setData(String data) {
        this.data = data;
    }

    /**
     * @return the id
     */
    public int getId() {
        return id;
    }

    /**
     * @param id the id to set
     */
    public void setId(int id) {
        this.id = id;
    }

    /**
     * @return the next
     */
    public Link getNext() {
        return next;
    }

    /**
     * @param next the next to set
     */
    public void setNext(Link next) {
        this.next = next;
    }
    public void printData(){
        System.out.println(id+" => "+data);
    }
}
in the class we make three data members id of type int, data of type String and the last member next, focus on its data type it is Link itself, this is because we will store the reference of the next Link in next data member using its assessor method.

Now create a class in which we make the singly link list using the Link class we just made.
class LinkList {
    private Link first;

    public LinkList(){
        first=null;
    }
    public void addLink(int id, String data){
        Link link=new Link();
        link.setId(id);
        link.setData(data);
        link.setNext(first);
        first=link;
        //first.printData();// un-comment this line to see what data is added 

    }
    public void printList(){
        Link currentLink=first;
        System.out.println("Printing List:");
        if(currentLink==null){
            System.out.println("List is Empty, Consider adding elements.");
            return;
        }
        while(currentLink!=null){
            currentLink.printData();
            currentLink=currentLink.getNext();
        }
    }
    public static void main(String []nix){
        LinkList linklist=new LinkList();
        linklist.printList();
        linklist.addLink(1, "val1");
        linklist.addLink(2, "val2");
        linklist.addLink(3, "val3");
        linklist.addLink(4, "val4");
        linklist.printList();
    }
}
in this class we made two methods first method addLink add a new link at the head of the previous node, line link.setNext(first); set the existing reference of the link list to the next of the new link object and first=link; make the first reference to the newly created link, hence we can add any link at the head of the list. Second method, printList() simply iterate through all the "link" objects in the link list
Continue Reading...
 

Blog Info

A Pakistani Website by Originative Systems

Total Pageviews

Tutorial Jinni Copyright © 2015 WoodMag is Modified by Originative Systems