Source Code Link Stack
/*
* Linked List Berbasis Stack.
*
* @author : Fauzan Azhmi Siregar
* @id : G1A011022
* @class : A
*/
class item
{
public char data;
public item next;
public item prev;
public item(char id)
{
data = id;
}
public void displayLink()
{
System.out.print("[" + data + "] ");
}
}
class StackLink
{
private item top;
private item bottom;
public StackLink()
{
top = bottom = null;
}
public boolean isEmpty()
{
return (top==null);
}
public void push(char id)
{
item newitem = new item(id);
if (top == null)
{
top = bottom = newitem;
}
else
{
top.next = newitem;
newitem.prev = top;
top = newitem;
}
}
public item pop()
{
item temp = null;
if (top == null)
System.out.print("Empty!");
else if (top == bottom)
{
temp = top;
top = bottom = null;
}
else
{
temp = top;
top = top.prev;
top.next = null;
}
return temp;
}
public void display()
{
item current = bottom;
if (current == null)
System.out.print("[ ]");
else
while(current != null)
{
current.displayLink();
current = current.next;
}
System.out.print("");
}
}
* Linked List Berbasis Stack.
*
* @author : Fauzan Azhmi Siregar
* @id : G1A011022
* @class : A
*/
class item
{
public char data;
public item next;
public item prev;
public item(char id)
{
data = id;
}
public void displayLink()
{
System.out.print("[" + data + "] ");
}
}
class StackLink
{
private item top;
private item bottom;
public StackLink()
{
top = bottom = null;
}
public boolean isEmpty()
{
return (top==null);
}
public void push(char id)
{
item newitem = new item(id);
if (top == null)
{
top = bottom = newitem;
}
else
{
top.next = newitem;
newitem.prev = top;
top = newitem;
}
}
public item pop()
{
item temp = null;
if (top == null)
System.out.print("Empty!");
else if (top == bottom)
{
temp = top;
top = bottom = null;
}
else
{
temp = top;
top = top.prev;
top.next = null;
}
return temp;
}
public void display()
{
item current = bottom;
if (current == null)
System.out.print("[ ]");
else
while(current != null)
{
current.displayLink();
current = current.next;
}
System.out.print("");
}
}
Source Code Stack
import java.util.*;
/*
* Linked List Berbasis Stack.
*
* @author : Fauzan Azhmi Siregar
* @id : G1A011022
* @class : A
*/
class Stack_LIFO
{
public static void main(String[] args)
{
System.out.println("\t_Stack_Last_In_First_Out_\n");
char p;
Scanner in = new Scanner(System.in);
StackLink theStack = new StackLink();
do
{
System.out.print("Input Data : ");
char nilai = in.nextLine().charAt(0);
theStack.push(nilai);
System.out.print("Continue to Input Data (y/n) : ");
p = in.nextLine().charAt(0);
}
while (p=='y' );
System.out.print("\nData Stack : ");
theStack.display();
System.out.println("");
System.out.println("\n\n\t_Stack Last In First Out (LIFO)_\n");
System.out.print("Data In\t: ");
theStack.display();
System.out.print(" ==> ");
while( !theStack.isEmpty() )
{
item aLink = theStack.pop();
aLink.displayLink();
System.out.println("Out.\n");
System.out.print("Last Data\t: ");
theStack.display();
System.out.print(" ==> ");
}
System.out.println(" Empty! ");
}
}
/*
* Linked List Berbasis Stack.
*
* @author : Fauzan Azhmi Siregar
* @id : G1A011022
* @class : A
*/
class Stack_LIFO
{
public static void main(String[] args)
{
System.out.println("\t_Stack_Last_In_First_Out_\n");
char p;
Scanner in = new Scanner(System.in);
StackLink theStack = new StackLink();
do
{
System.out.print("Input Data : ");
char nilai = in.nextLine().charAt(0);
theStack.push(nilai);
System.out.print("Continue to Input Data (y/n) : ");
p = in.nextLine().charAt(0);
}
while (p=='y' );
System.out.print("\nData Stack : ");
theStack.display();
System.out.println("");
System.out.println("\n\n\t_Stack Last In First Out (LIFO)_\n");
System.out.print("Data In\t: ");
theStack.display();
System.out.print(" ==> ");
while( !theStack.isEmpty() )
{
item aLink = theStack.pop();
aLink.displayLink();
System.out.println("Out.\n");
System.out.print("Last Data\t: ");
theStack.display();
System.out.print(" ==> ");
}
System.out.println(" Empty! ");
}
}
Tidak ada komentar:
Posting Komentar