Sunday, February 15, 2009

Stack (A Java Code)

/* Programmer: Julie Ann Pabio
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;


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() {
return (first == null);
}

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

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

public Link pick()
{
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

-it is a linked list in which each item has a link to the previous and as well as the next.
-it also allows easily accessing list items backward as well as forward and deleting an item in constant time.
Illustration of Double-Ended List
Double-Ended List

-it is a list with a first and last reference.
-using this list we can easily manipulate deleting and inserting a node(first and last).

Tuesday, February 10, 2009

Implementation of Double-Ended List

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.99);

theList.insertFirst(44,4.99);

theList.insertFirst(66,6.99);

theList.insertLast(11,1.99);

theList.insertLast(33,3.99);

theList.insertLast(55,5.99);



System.out.println(theList);

theList.deleteFirst();

theList.deleteFirst();

System.out.println(theList);

}

}

Friday, February 6, 2009

Illustration of Stack















Reference:http://en.wikipedia.org/wiki/Stack_(data_structure)
  • This is a simple representation of a stack
The Stack class represents a last-in-first-out (LIFO) stack of objects. The usual push and pop operations are provided, as well as a method to peek at the top item on the stack, a method to test for whether the stack is empty, and a method to search the stack for an item and discover how far it is from the top.

Method Involve In Stack
  • push()

  • pop()

  • peek() or top()

  • isEmpty()

  • search()

  • size()

[java]

Reference:

 http://java.sun.com/j2se/1.4.2/docs/relnotes/devdocs-vs-specs.html