Tuesday, February 17, 2009

Stack Implementation

//a class
class Link{
public int iData=0;


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

}

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

//the classfor operations on the stack
class StackList{
private Link first;

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

}

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

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

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

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

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

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

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

theList.insertFirst(116);//show the insertion of items
theList.insertFirst(82);
theList.insertFirst(34);

theList.deleteFirst();//show the delete item/element

theList.displayList();
}
}

No comments:

Post a Comment