Contoh Program Implementasi Stack Menggunakan Linked List dalam Bahasa Java


class item //atau node/simpul
{
public int data; // data item
public item next; // next node link in list
public item prev; // previous node link in list
public item(int id) // constructor
{
data = id; // initialize data
} // set to null)
public void displayLink() // display ourself
{
System.out.print(“{” + data + “} “);
}
} // end class Link
class StackLinkList
{
private item top; // ref to first link on list
private item bottom; // ref to last link on list
public StackLinkList() // constructor
{
top = bottom = null; // no items on list yet
}
public boolean isEmpty() // true if list is empty
{
return (top==null);
}
public void push(int id) //node baru selalu di top
{ // make new link
item newitem = new item(id);
if (top == null) // the first node created
{
top = bottom = newitem; // first –> newLink
}
else // the second node and the next node
{
top.next = newitem; //next dr top (awal) diarahkan ke node baru
newitem.prev = top; //prev dr node baru diarahkan ke tail (awal)
top = newitem; //top (baru) diarahkan ke node baru
}
}
public item pop() // delete first item
{ item temp = null;
if (top == null) // stack is empty
System.out.println(“Stack is empty”);
else if (top == bottom) // stack is only one data
{
temp = top;
top = bottom = null;
}
else // stack has more than one data
{
temp = top; // save reference to link
top = top.prev; // delete it: first–>old next
top.next = null;
}
return temp;
}
public void display()
{
item current = bottom; // start from the first data
while(current != null) // until end of list,
{
current.displayLink(); // print data
current = current.next; // move to next link
}
System.out.println(“”);
}
} // end class LinkList
class StackLinkListApp
{
public static void main(String[] args)
{
StackLinkList theStack = new StackLinkList(); // make new list
System.out.println(“Initializing Stack…”);
theStack.push(22); // insert four items
theStack.push(44);
theStack.push(66);
theStack.push(88);
System.out.println(“Display Forward :”);
theStack.display(); // display list
System.out.println(“Delete Stack from Top…”);
while( !theStack.isEmpty() ) // until it’s empty,
{
item aLink = theStack.pop(); // delete link
System.out.print(“Deleted “); // display it
aLink.displayLink();
System.out.println(“”);
}
theStack.display(); // display list
} // end main()
}

About these ads

2 Responses to Contoh Program Implementasi Stack Menggunakan Linked List dalam Bahasa Java

  1. si keciL says:

    saya Lg ada tugas, saya ambiL buat Referensi ya mas?!
    ^_^

    • bluejundi says:

      silahkan. tp biasanya klo sumber referensi dr blog itu kurang sip krn nilai ilmiahnya tdk sekuat buku atau makalah :)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 44 other followers

%d bloggers like this: