Sunday, February 15, 2009

Stack (Code Implementation)

/* Programmer: Marjorie T. Egot
Program name: A Stack code Implementation
Purpose: To implement a stack data stucture
Instructor: Dony Dongiapon
Subject: IT123 Data Structures
*/

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


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

}

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

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

public StackList(){
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
February 16, 2009

Concept/Definition:

A more sophisticated kind of linked list is a doubly-linked list or two-way linked list. Each node has two links: one points to the previous node, or points to a null value or empty list if it is the first node; and one points to the next, or points to a null value or empty list if it is the final node.
[wiki]
♥ As what I have learned, you can access the link before or after a node since they are connected with each other. You have the previous and the next links which are its feature. It's complicated since you have to deal with the 4 pointers.

Illustration:

This is taken from the given example of our instructor.


Code Implementation:

/* Programmer: Marjorie T. Egot
Program name: A Doubly Linked List
Purpose: To implement a doubly linked list.
Instructor: Dony Dongiapon
Subject: IT123 Data Structure */

//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
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;
}
}

// the main method
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:

Tuesday, February 10, 2009

Doubly-ended Code Implementation :

/* Program name: A Double Ended List
Programmer: Marjorie T. Egot
Purpose: To implement a double ended linked 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;
}

//displays the elements in the list
public void displayLink()
{
System.out.print("{"+iData+","dData+"}");
}
}


//a class which contains the methods

class DoubleEndList
{
private Link first;
private Link last;


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

//this is to test if there are elements in the list
public boolean isEmpty()
{
return (first == null);
}


//inserting a node in the list to become 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
public void insertLast(int id,double dd)
{
Link newLink = new Link(id,dd);
if (isEmpty())
first = newLink;
else
last.next = newLink;
last = newLink;
}

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

//deletes 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;
}

//displays the elements of 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(" ");
}



/* the main class or the application of the program.*/

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

DoubleEndList theList = new DoubleEndList();

//apply the insertion methods on the first and the last node
theList.insertFirst(12,2.55);
theList.insertFirst(21,4.55);
theList.insertFirst(67,8.99);
theList.insertLast(18,9.99);
theList.insertLast(43,3.33);
theList.insertLast(34,5.66);

//displays the elements of the list
System.out.println(theList);

//apply the deletion method on the first and last element of the list
theList.deleteFirst();
theList.deleteLast();
System.out.println(theList);
}
}

Sunday, February 8, 2009

Double-ended Linked List
February 9, 2009

Illustration:

This is taken from the given example of our instructor.

◘ Input:

♥ To fully understand the double-ended linked lists, be sure that you were able to learn the singly linked lists so that it would be easy for you to implement it. Once the node wasn't connected to the other node, the access will be lost and the java garbage collector will automatically get it. It wouldn't be retrieved again.

Definition:

○ What is a double-ended linked list?

♥ It allows 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]

Reference:

Saturday, February 7, 2009

Things Learned...

STACK
February 7, 2009
7:45 pm

Illustration:

This is an illustration that applied the principle of stack. [Surfdoggy.com]

Definition:

♥ Stack is based on the principle of Last In First Out (LIFO).
♥ It is an abstract data type and data structure.
♥ It is a container of nodes wherein different operations are implemented such as "pop" to get an object out of the stack and "push" to enter an object.
♥ Another helper operations are "top" to identify what is on the top of the stack and "null or isEmpty" to know if there is an object in the stack.
♥ The contents of the stack are hidden.
♥ The top of the stack is visible and accessible to the user, others remain hidden.
♥ As new objects are added, each new object becomes the top of the stack, hiding each object below, pushing the others down. As the top object is removed from the stack, they can be used, the objects pop back up, and the second object becomes the top of the stack.
[WIKI]

Reference: