Wednesday, June 19, 2019

java LinkedList Example



package linkedlistexp;

class Node {
    int data;
    Node nextNode;
}
class LinkedList {

    private Node head;

    public void insert(int data) {
        System.out.println("----------insert() data=" + data);
        //create a new Node and store a data.
        Node new_node = new Node();
        new_node.data = data;
        new_node.nextNode = null;

        //check the head is null or not.
        //if head is null, assign the Node and exit.
        if (this.head == null) {
            head = new_node;
            System.out.println("1st node: head.data=" + head.data + " next=" + head.nextNode + " ");
            return;
        }

        //assign a head into the last Node and loop it until find the null reference.
        Node lastNode = this.head;
        while (lastNode.nextNode != null) {
            lastNode = lastNode.nextNode;
            System.out.println("while() : last.data=" + lastNode.data+ " last.next=" + lastNode.nextNode);
        }

        //assign new node.
        lastNode.nextNode = new_node;
        System.out.println("last.next.data=" + lastNode.nextNode.data+
                                " last.next.next=" + lastNode.nextNode.nextNode);
    }

    public void print() {
        if (this.head == null) {
            return;
        }
        //print all nodes
        Node tempNode = this.head;
        while (tempNode != null) {
            System.out.print(tempNode.data + "->");
            tempNode = tempNode.nextNode;
        }
        System.out.println("NULL");
    }
}
public class LinkedListExp {

  public static void main(String[] args) {
      LinkedList ls = new LinkedList();
      ls.insert(10);
      ls.insert(20);
      ls.insert(30);
      ls.insert(40);
      ls.insert(50);
      ls.print();
  }

}

output:
run:
run:
----------insert() data=10
1st node: head.data=10 next=null
----------insert() data=20
last.next.data=20 last.next.next=null
----------insert() data=30
while() : last.data=20 last.next=null
last.next.data=30 last.next.next=null
----------insert() data=40
while() : last.data=20 last.next=linkedlistexp.Node@15db9742
while() : last.data=30 last.next=null
last.next.data=40 last.next.next=null
----------insert() data=50
while() : last.data=20 last.next=linkedlistexp.Node@15db9742
while() : last.data=30 last.next=linkedlistexp.Node@6d06d69c
while() : last.data=40 last.next=null
last.next.data=50 last.next.next=null
10->20->30->40->50->NULL
BUILD SUCCESSFUL (total time: 0 seconds)


---------------------------------------------------------------------------------------------------------

Advantages of a Linked List

  • Insertions and deletions are quick.
  • Grows and shrinks as needed.

Disadvantages of a Linked List

  • Random access is slow. Objects in a linked list must be accessed sequentially, therefore, it can be slow to access a specific object.
  • Memory is a concern. Each object in a linked list requires data as well as one (or more) pointers to other objects in the linked list. Using significantly less memory, each object in an array only requires data.

When to Use a Linked List?

  • You don’t need random access to any specific elements.
  • You need to do constant insertions and deletions.
  • You’re not sure how many items will be in the list.


Thursday, August 23, 2018

java xml parsing

download: http://www.mediafire.com/file/wcob5kk3013kvkp/sax_project1.zip/file

<?xml version="1.0" encoding="UTF-8"?>

<department>

  <employee>
      <empNo>E01</empNo>
      <empName>KING</empName>
      <hireDate>17-11-1981</hireDate>
      <salary>100000</salary>
  </employee>

  <employee>
      <empNo>E02</empNo>
      <empName>JONES</empName>
      <hireDate>02-04-1981</hireDate>
      <salary>200000</salary>
  </employee>

</department>