Monday, February 2, 2009

Stack

Concept

According to wikipedia  a stack is an abstack data type and its based on the principle of Last In First Out(LIFO).It is uesd to run a Java Virtual Machine,it is ubiquitous.

Basic Operations

Push-adds a given node to the top .
Pop- removes and returns the current top node.
top(peek)- can return the current top element.


some environments
*Dup(licate)- the top item is popped and pushed again.
*peek- the topmost item is popped.
*Swap or exchange- the two topmost items exchange places.
*rotate- the topmost items are moved in a rotating fashion.
[wiki]





B)Illustration

illustration of Stack[3]

C)References
http://en.wikipedia.org/wiki/File:Data_stack.svg
http://en.wikipedia.org/wiki/Stack_(data_structure)



Implementation of Stack


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 the 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(12);
theList.insertFirst(25);
theList.insertFirst(91);


theList.deleteFirst();

theList.displayList();
}
}




2 comments:

  1. Hmmmm.... comment??? well, all i can say that Data structure is not too dfficult but it can really make u crazyyyy... even though our instructor my DONY teach us well...hmmm... mahirap tlaga... nakakalito ang mga topics!huhuhuh.....

    ReplyDelete
  2. Sir, frankly speaking... kung minsan nalilito akon sa ating lesson I don't know why... I'm not saying all the time,maybe ala ko sa mood ana para maminaw...sorry....

    ReplyDelete