Monday, February 16, 2009

Implementation of Stack

//To implement an array in a certain stacks.

class Link {
public int iData=0;

public Link(int iData,){

iData=id;
}

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

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
{
System.out.print("Elements on stack: ");
Link temp=first;
while(temp!=null)
{
temp.displayList();
}

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

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

theList.insertFirst(23);
theList.insertFirst(13);
theList.insertFirst(22);

theList.deleteFirst();

theList.displayList();
}
}

No comments:

Post a Comment