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();
}
}

No comments:

Post a Comment