Sunday, February 8, 2009

A Double Ended List

Illustration

Concept/Definition:
♥♥ In going through the concept of a double ended Linked List, you must first understand the full concept of a singly linear linked list since it is so much connected with one another.♥♥The concept of a double ended List is that it has access to the first and last element of the nodes of a Linked List. "Double ended lists allow for insertions in the front or in the back of the list. Each type of link list will build off of the previous one. First we'll examine the singly linked list before moving onto the double-ended and doubly linked lists. "[SAFARI_BOOKS]

What I Have Learned in our class
....as what Iv'e also learned in our class, it is very important that a linked list should be always connected with one another because once it losses access to the other, it can never be back again since the java garbage collector would erase it immediately......i would really advice that all the nodes of the list should be connected so that it would always be in the list or else!!! say goodbye to the list...

Code Implementation
/* Programmer: Chrisdyll P. Pellejo
Program name: A Double Ended List
Purpose: To implement a double ended list.
Instructor: Dony Dongiapon
Subject: IT123 Data Structures
*/

/*a class that contains the constructor*/
class Link {
public int iData;
public double dData:
public Link next;

public Link(int id,double dd) {
iData = id;

dData=dd;
}
//displaying the elements in the list
public void displayLink(){

System.out.print("{"+iData+","dData+"}");
}
}


/*another class which contains the methods*/

class DoubleEndList {
private Link first;
private Link last;


public DoubleEndList() {
first = null;
last = null;
}
//testing if the the list has elements or if it is null
public boolean isEmpty() {
return (first == null);
}


//inserting a node to be the first element
public void insertFirst(int id,double dd) {
Link newLink = new Link(id,dd);

if (isEmpty ())
last = newLink;
newLink.next = first;

first = newLink;
}
//inserting a node on the last part
public void insertLast(int id,double dd) {
Link newLink = new Link(id,dd);
if (isEmpty())
first = newLink;
else
last.next = newLink;
last = newLink;
}


//deleting the first node on the list
public Link deleteFirst(int id,double dd) {
int temp = first.iData;
if (first.next == null)
last = null;
first = first.next;
return temp;
}

//deleting the last node of the list
public Link deleteLast(int id, double dd){
int temp=last.iData;
if(last.next==null)
first=null;
last=last.next;
return temp;
}

//display the elements on the list
public void displayList(){
System.out.print("List(first-->Last);");
Link current=first;
while(current!=null){
current.displayLink();
current=current.next;

}
}
System.out.println(" ");
}
}


/*another class for the application of the program.
Or it is formally called the main class*/

public class DoubleEndApp{
public static void main(String[] args) {

DoubleEndList theList = new DoubleEndList();

//application of the insertion methods on the first and the last
theList.insertFirst(12,25);
theList.insertFirst(2,45);
theList.insertFirst(67,89);
theList.insertLast(1,99);
theList.insertLast(243,33);
theList.insertLast(234,90);

//displaying the elements on the node
System.out.println(theList);

//deletion operation on the first and last element on the list
theList.deleteFirst();
theList.deleteFirst();
System.out.println(theList);
}
}

Reference:
[SAFARI_BOOKS] http://my.safaribooksonline.com/30000LTI00162/ch05lev1sec2

1 comment:

  1. Its my destiny to visit at this blog and find out my required paragraph along with video demo, that’s YouTube video and its also in quality.

    Curso java

    ReplyDelete