Program yang mengimplementasikan stack menggunakan fungsi Linked list dengan Bahasa Java

Apakah itu Single Linked List?

Dalam ilmu komputer, linked list dapat didefinisikan sebagai koleksi linear dari elemen – elemen data. Penempatan elemen – elemen data ini acak di dalam memori, namun antar elemen data ini terhubung dengan node. Jadi satu node dalam suatu elemen data akan menunjuk ke node elemen data lain melalui suatu penunjuk yang umumnya disebut pointer. Jadi dapat disimpulkan, sebuah koleksi data disebut sebagai Linked List apabila antar data tersebut nodenya saling terhubung melalui pointer.

Lantas, apakah itu Single Linked List? Sesuai penjelasan pada paragraf sebelumnya, antar elemen data terhubung nodenya melalui pointer. Nah, pointer inilah yang menentukan sebuah Linked List apakah itu Single, Double, Circular, atau Double Circular. Sebuah Linked List dikatakan sebagai single apabila dua node hanya terhubung dengan satu pointer saja [entah itu pointer maju ataupun mundur]. Jika, dua node terhubung dengan dua pointer [bolak balik] maka disebut Double. Apabila node first dan last nya saling terhubung dan hanya dihubungkan dengan satu pointer maka disebut single circular. Terakhir, apabila node first dan last nya terhubung dengan dua pointer [bolak balik] maka disebut double circular.

Secara sederhana berikut penjelasan melalui gambar.

Sebuah Node lengkap dengan elemen data dan pointernya

Kemudian contoh dari Single Linked List adalah sebagai berikut.

Dapat kita lihat dari gambar di atas, masing – masing node dihubungkan oleh satu pointer. Sedangkan pada pointer node terakhir tidak menunjuk ke mana – mana alias null.

DosenIT.com

Type your search query and hit enter:

  • Homepage
  • Kuliah IT
  • Pemrograman

Pemrograman

Contoh Stack : Fungsi, Jenis dan Implementasinya

Section Artikel

  • 1 Apa itu Stack?
  • 2 Fungsi dalam Stack
  • 3 Operasi pada stack
  • 4 Implementasi Stack dengan Array menggunakan bahasa Java
  • 5 Stack dengan Linked List
      • 5.0.1 Perbandingan Stack dengan Linked List dan Stack dengan Array

Contoh Program Implementasi Stack Menggunakan Linked List dalam BahasaJava


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[]
}

Share this:

  • Twitter
  • Facebook

Like this:

Like Loading...

Related

This entry was posted on Thursday, May 5th, 2011 at 5:28 pm and is filed under IT, kantor. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

Post navigation

« Previous Post Next Post »

Video liên quan

Bài mới nhất

Chủ Đề