Stack

Posted by Bl4ckB0y On 04.39 0 comments

Stack merupakan sebuah Struktur data yang dapat dibayangkan sebagai sebuah tumpukan (stack = tumpukan). contoh stack dalam kehidupan sehari-hari adalah sebuah tumpukan piring, dimana setiap kali piring terakhir yang kita tumpuk akan diambil terlebih dahulu. sifat stack adalah LIFO (last In forst out) --> artinya setiap data yang masuk terakhir kali akan keluar pertama kali....
berikut ini adalah source code implementasi dari sebuah stack....

/*
This Program Implemet about stack
Author : huda
e-mail : huda890@gmail.com
*/
import java.io.*;
class stack
{
static int MAXIMUMSTACK=10;
public static void main(String args[])throws IOException
{
stack s=new stack(); //build stack class
char temporary_character; //as temporary variabel to save char
String temporary_string; //temporary variabel for casting
int Choose; //this for choose
boolean flag=true; //flag for looping
int top=-1; //pointer of stack
char stack[]=new char[MAXIMUMSTACK]; //build stack object
int long_character=0; //show length of stack
/*make the buffer for input from console*/
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
/*main program*/
System.out.println("Stack");
System.out.print("insert String : ");
/*get char from console one by one */
while((temporary_character=(char)in.read())!='\n')
{
top++;
stack[top]=temporary_character;
}
top--; //character '\n' not use, so it must be pop
/*to show what user want*/
do
{
System.out.println("1.Pop 2.push 3.Print 4.exit");
System.out.print(“Inser your Choose:”);
/*because switch just use for primitif tipe, the string must be convert ot integer*/
temporary_string=in.readLine();
Choose=Integer.parseInt(temporary_string);
/*choosing*/
switch(Choose)
{
case 1: int y=s.pop(stack,top);top=y;break;
/*int y for save the value from return of pop*/
case 2: int x=s.push(stack,top);top=x;break;
/*integer x for save the return from push*/
case 3: s.Print(stack,top);break;
/*for print all data in the stack*/
case 4: flag=false;break;
/*Close porgram */
default : System.out.println("Wrong");
}
}while (flag);
}
/*for check Full stack*/
boolean full(int top)
{
if(top==MAXIMUMSTACK-1) //check stack full or not
return(true); //true if full
return (false); //otherwise false
}
/*for check the stack empty or not */
boolean empty(int top)
{
if(0>top) //check empty or not
return(true); //true if empty
return(false); //otherwise false
}
/*this function to push character*/
int push(char stack[],int top) throws IOException
{
if(!full(top)) //check stack full or not before pushing
{
char character;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Insert A Char : ");
character=(char) br.read(); //get char from console
stack[++top]=character;
/*if stack not full push wil progress*/
return(top);
/*return for x variabel*/
}
else
System.out.println("\nFull");
return(top);
/*return variabel to X*/
}
/*this methode to pop a character*/
int pop(char stack[], int top)
{
if ( empty(top)) //chaeck stack empty or not
{
System.out.println("Empty");
return(top); //return top for y variabel
}
else
{
System.out.println("Character : "+ stack[top]+" pop Succesfull");
return(--top); //return top for y variabel
}
}
/*print stack to console*/
void Print(char stack[], int top)
{
System.out.print("Stack is : ");
/*print all element to console from behind to top, (without pop proses) */
for (int i=0;top=>i;i++)
System.out.print(stack[i]);
System.out.println();
}
}

Categories:

0 Response for the "Stack"

Posting Komentar

your Ads Here

Your Ad Here FellowEquality.com