This is default featured post 1 title

Go to Blogger edit html and find these sentences.Now replace these sentences with your own descriptions.

This is default featured post 2 title

Go to Blogger edit html and find these sentences.Now replace these sentences with your own descriptions.

This is default featured post 3 title

Go to Blogger edit html and find these sentences.Now replace these sentences with your own descriptions.

This is default featured post 4 title

Go to Blogger edit html and find these sentences.Now replace these sentences with your own descriptions.

This is default featured post 5 title

Go to Blogger edit html and find these sentences.Now replace these sentences with your own descriptions.

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

Sunday, 21 April 2013

Traversing a Single Linked List

Traversing a SLL

  The following method traverses a list (and prints its elements):


 Cell insertAtFront(Cell oldFront, Object value) {
    Cell newNode = new Cell(value, oldFront);
    return newNode;
}
 
 Use this as:  myList = insertAtFront(myList, value);

Why can’t we just make this an instance method of Cell?

Using a header node

A header node is just an initial node that exists at the front of every list, even when the list is empty. The purpose is to keep the list from being null, and to point at the first element

 inseritn a node after a given number
Inserting a node after a given value


 void insertAfter(Object target, Object value) {
    for (Cell here = this; here != null; here = here.next) {
          if (here.value.equals(target)) {
              Cell node = new Cell(value, here.next);
              here.next = node;
              return;
          }
}
    // Couldn't insert--do something reasonable here!
Inserting after (animation)

Deleting a node from sll

Deleting a node from a SLL

  1.  In order to delete a node from a SLL, you have to change the link in its predecessor
  2.  This is slightly tricky, because you can’t follow a pointer backwards
  3.  Deleting the first node in a list is a special case, because the node’s predecessor is the list header

Deleting an element from a SLL

To delete the first element, change the link in the header

delete the first elemetn

To delete some other element, change the link in its predecessor
delete some other element

Deleted nodes will eventually be garbage collected

Doubly-linked lists

   Here is a doubly linked list (DLL):

double linked list

 Each node contains a value, a link to its successor (if any), and a link to its predecessor (if any).The header points to the first node in the list and to the last node in the list (or contains null links if the list is empty)

DLLs compared to SLLs


compared to sll

 Advantages:


It can be traversed in either direction (may be essential for some programs)    Some operations, such as deletion and inserting before a node, become easier.

Disadvantages:


It requires more space. List manipulations are slower (because more links must be changed). Greater chance of having bugs (because more links must be manipulated)

Deleting a node from a DLL


deleting a node form double linked list


  1.       Node's deletion from a DLL involves changing two links.
  2.       In this example, we will delete node b.
  3.       We don’t have to do anything about the links in node b.
  4.       Garbage collection will take care of deleted nodes.
  5.       Deletion of the first node or the last node is a special case.

Other operations on linked lists


Most “algorithms” on linked lists—such as insertion, deletion, and searching—are pretty obvious; you just need to be careful.

 Sorting a linked list is just messy, since you can’t directly access the nth element—you have to count your way through a lot of other elements.

Friday, 19 April 2013

Single Linked List


Singly-linked lists



singly-linked-list




  Here is a singly linked list (SLL):

 Each node contains a value and a link to its successor (the last node has no successor)
 The header points to the first node in the list (or contains the null link if the list is empty)



Creating a simple list



    To create the list ("one," "two," "three"):


 Cell numerals = new Cell();
   numerals =
    new Cell("one",
        new Cell("two",
            new Cell("three", null)));
 

Wednesday, 17 April 2013

Linked List in Java

A linked list consists of sequence of nodes.

linked_list


Each node contains a value and a link (pointer or reference) to some other node. The last node contains a null link. The list may (or may not) have a header.

More terminology

A node’s successor is the next node in the sequence the last node has no successor node’s predecessor is the previous node in the sequence. The first node has no predecessor list’s length is the number of elements in it list may be empty (contain no elements).


Pointers and references

  In C and C++, we have “pointers,” while in Java, we have "references," This  is essentially the same thing, The difference is that C and C++ allow you to modify pointers in arbitrary ways, and to point to anything, In Java, a reference is more of a “black box,” or ADT.

 Available operations are:

DE references (“follow”)

  copy

  Compare for equality

There are constraints on what kind of thing is referenced: for example, a reference to an array of into can only refer to an array of into.

 Creating references

  The keyword new creates a new object, but also returns a reference to that object.

  For example, Person p = new Person("John")

   New Person("John") creates the object and returns a reference to it.

  We can assign this reference to p, or use it in other ways.
linked list



 class Cell {
     int value;
     Cell next;

     Cell (int v, Cell n) { // constructor
     value = v;
     next = n;
}
     }

     Cell temp = new Cell(17, null);
     temp = new Cell(23, temp);
     temp = new Cell(97, temp);
     Cell myList = new Cell(44, temp);






Twitter Delicious Facebook Digg Stumbleupon Favorites More