Sunday, February 15, 2009

Stack (Java Code Implementation)

/* Programmer: Chrisdyll P. Pellejo
Program name: A Stack Code Implementation
Purpose: To implement a Stack Code.
Instructor: Dony Dongiapon
Subject: IT123 Data Structures*/

//a class which declares the variables and the constructors
class Link{
public int iData=0;//data item


public Link(int iData, ) //constructor
{
iData=id;

}

public void displayLink() //displaying the data
{
System.out.println(iData+":" );
}
}

//the class which contains the methods or the operations on the stack
class StackList{
private Link first;

public StackList(){ //declaring the list as empty or null
first=null;

}

public boolean isEmpty() { //checking if the list is empty or not
return (first == null);
}

public void insertFirst( int id) { //insertion operation
Link newLink = new Link( id);
newLink.next = first;
first = newLink;
}

public Link deleteFirst() //deletion operation
{
Link temp=first;
return temp;
}

public Link pick() //determining the top of the list but doing nothing with it
{
Link temp=first;
return temp;
}

public void displayList //display the data
{
System.out.print("Elements on the stack: ");
Link temp=first;
while(temp!=null)
{
temp.displayList();
}

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

//the main class which applies the methods on the stack
class StackListApp
{
public static void main (String[]args)
{
StackList theList=new StackList();

theList.insertFirst(12);
theList.insertFirst(25);
theList.insertFirst(91);

//when deleting
//just erase the comment if you want to run the method of deletion
theList.deleteFirst();

//when displaying the element
theList.displayList();
}
}

Doubly Linked List ILLUSTRATION

Double-ended Linked List Illustration

Saturday, February 14, 2009

Doubly-Linked Lists

Concept/Definition:
"a doubly linked list is not an abstract data type, but only an implementation type".[TUTORIAL_DOUBLY]
Double-linked lists require more space per node and their elementary operations are more expensive but they are often easier to manipulate because they allow sequential access to the list in both directions. In particular, one can insert or delete a node in a constant number of operations given only that node's address. [WIKI].
As what I Have Learned in our class..
doubly linked lists are complicated because there is another feature which is the "previous" in such you can access to the previous link. In inserting or deleting a node, you have to keep track of the four pointers: "2 next and 2 previous" pointers.
Code Implementation:
/* Programmer: Chrisdyll P. Pellejo
Program name: A Doubly-Linked List
Purpose: To implement a doubly linked list.
Instructor: Dony Dongiapon
Subject: IT123 Data Structures
*/
//constructor
class Link {
public int iData; //data item
public Link next; //next link in the list
public Link previous; //previous link in the list

public Link(int id) {
iData = id;
}

//display the elements in the list
public void displayList(){
return "{" + iData + "} ";
}
}

/*another class which contains the methods for the program*/
class DoublyLinkedList {
private Link first;
private Link last;

public DoublyLinkedList() {
first = null;
last = null;
}

public boolean isEmpty() {
return first == null;
}

public void insertFirst(int dd) {
Link newLink = new Link(dd);
if (isEmpty()){
last = newLink;
}else{
first.previous = newLink;
}
newLink.next = first;
first = newLink;
}

public void insertLast(int dd) {
Link newLink = new Link(dd);
if (isEmpty()){
first = newLink;
}else {
last.next = newLink;
newLink.previous = last;
}
last = newLink;
}

public Link deleteFirst() {
Link temp = first;
if (first.next == null){
last = null;
}else{
first.next.previous = null;
}
first = first.next;
return temp;
}

public Link deleteLast() {
Link temp = last;
if (first.next == null){
first = null;
}else{
last.previous.next = null;
}
last = last.previous;
return temp;
}

public boolean insertAfter(int key, int dd) {
Link current = first;
while (current.iData != key) {
current = current.next;
if (current == null){
return false;
}
}
Link newLink = new Link(dd);

if (current == last) {
newLink.next = null;
last = newLink;
} else {
newLink.next = current.next;

current.next.previous = newLink;
}
newLink.previous = current;
current.next = newLink;
return true;
}

public Link deleteKey(int key) {
Link current = first;
while (current.iData != key) {
current = current.next;
if (current == null)
return null;
}
if (current == first){
first = current.next;
}else{
current.previous.next = current.next;
}

if (current == last){
last = current.previous;
}else{
current.next.previous = current.previous;
}
return current;
}

}
/*another class which is the main that applies all of the methods*/
public class DoublyLinkedApp{
public static void main(String[] args) {
DoublyLinkedList theList = new DoublyLinkedList();

theList.insertFirst(22);
theList.insertFirst(44);
theList.insertFirst(66);

theList.insertLast(11);
theList.insertLast(33);
theList.insertLast(55);

System.out.println(theList);

theList.deleteFirst();
theList.deleteLast();
theList.deleteKey(11);

System.out.println(theList);

theList.insertAfter(22, 77);
theList.insertAfter(33, 88);

System.out.println(theList);
}
}
References:
[TUTORIAL_DOUBLY]
[WIKI]

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

STACK DATA STRUCTURE



Illustration of the stack data structure [WIKI]


Definition/Concept
Stack is an abstract data type and data structure. It’s principle is a LIFO (w/c means last in first out). Among other uses, stacks are used to run a Java Virtual Machine, and the Java language itself has a class called "Stack", which can be used by the programmer. "The stack is ubiquitous."
[WIKI]
The meaning of ubiquitous is that it is omnipresent or present everywhere. "It also means universal." [blurtit.com]

How do we connect a stack data structure in a real world representation??
In connecting a stack data structure in areal worls representation, just imagine pingpong balls put into a straight glass tube or your plates at home being arranged vertically or just a pile of hollowblocks. If you could really imagine, the first thing that you put on your pile is the last thing you could pull out and the last thing you put is the first that you could pull out. That is the concept of a stack..



..in our class discussion for the stack data structure, pop is the same as deletion and push is just the same with inserting and you cannot cheat on this kind of structure, you really have to go through it's method of deleting elements.♥♥♥♥
..iv'e also learned about different stack operations:
•isEmpty()//checking if the list is empy or null
•push() //the same as insertion operation
•pop() //the same as deletion operation
•top() //operation that will determine the top element of the list but will not delete it