About Me

Wednesday, February 18, 2009

Stack
Code Implementation


//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 stackclass StackList
{
private Link first;
public StackList()
{
first=null;
}
public boolean isEmpty()
{
//checking if the list is empty or notreturn (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();
}
}

Sunday, February 15, 2009

DOUBLY LINKED LIST



fig.1 Illustration of doubly linked list
[Google Image]


DEFINITION:

A kind of linked list which is also called as two-way linked list.

♦ Node has two links:
1. One points to the previous node, or points to a null value
2. One points to the next, or points to a null value



REFERENCES:

[Wiki]
http://en.wikipedia.org/wiki/Linked_list#Doubly-linked_lists


Double-Ended Linked List Implementation

class Link {

public int iData;
public double dData:
public Link next;

public Link(int id,double dd) {
iData = id;
dData=dd;
}
public void displayLink(){
System.out.print("{"+iData+","dData+"}");
}
}


class FirstLastList {
private Link first;
private Link last;
public FirstLastList() {
first = null;
last = null;
}

public boolean isEmpty() {
return (first == null);
}
public void insertFirst(int id,double dd) {
Link newLink = new Link(id,dd);
if (isEmpty ())
last = newLink;
newLink.next = first;
first = newLink;
}
public void insertLast(int id,double dd) {
Link newLink = new Link(id,dd);
if (isEmpty()
first = newLink;
else
last.next = newLink;
last = newLink;
}
public Link deleteFirst(int id,double dd) {
int temp = first.iData;
if (first.next == null)
last = null;
first = first.next;
return temp;
}
public Link deleteLast(int id, double dd){
int temp=last.iData;
if(last.next==null)
first=null;
last=last.next;
return temp;
}
public void displayList(){
System.out.print("List(first-->Last);");
Link current=first;
while(current!=null){
current.displayLink();
current=current.next;
}
}
System.out.println(" ");
}
}
public class FirstLastApp{
public static void main(String[] args) {
FirstLastList theList = new FirstLastList();

theList.insertFirst(22,2.91);
theList.insertFirst(11,1.99);
theList.insertFirst(65,6.99);
theList.insertLast(77,7.99);
theList.insertLast(99,9.99);
theList.insertLast(44,4.99);


System.out.println(theList);
theList.deleteFirst();
theList.deleteFirst();
System.out.println(theList);
}
}

[Data Structure- Program Activity]

Sunday, February 8, 2009

Double-Ended Linked List

Double-Ended Linked List

List structures have both a head and a tail,
so nodes may be appended to the list,
and the first and last nodes may be removed.

"Nodes have both forward and backward pointers, so both traversals are natural, and nodes may be inserted or removed before or after any node.It would be easy to add circular variants as well[Wiki]".


References:

http://books.google.com.ph/books?id=1lCHYj5eV-EC&pg=PA139&lpg=PA139&dq=double-ended+linked+list&source=bl&ots=ca8pfQGsoc&sig=5lDix0HRM5VN8WdT2TWORGS800o&hl=tl&ei=dyqSSYCLEobRkAXZroyEDA&sa=X&oi=book_result&resnum=6&ct=result

Thursday, February 5, 2009

Data Structure - Stack

Stack


"The stack is a very common data structure used in programs. By data structure, we mean something that is meant to hold data and provides certain operations on that data[Wiki]".

fig.1 Illustration of Stack

A "STACK OF BOOKS" is a good example to understand the concept of it.

♥we have a stack of books
♥we place a book on the top - PUSH
♥we take one off in the top - POP

More complex operations to be inappropriate for our stack. For example, pulling out the 3rd book from the top cannot be done directly because the stack might fall over. According to our instructor it is "CHEATING".


Methods/Operations:

☻isEmpty()

☻Push()

☻Pop()

☻Top()

☻display()

☻size()

Reference:

[Wiki]

http://www.nationmaster.com/encyclopedia/Stack-data-structure