You are on page 1of 5

Tugas 6

Struktur Data
Jurusan Teknik Informatika Fakultas Sains dan Teknologi
Nama : Muhammad Fahmi Rizaldi Ilham
Nim : 1197050084
Kelas : C Struktur Data
Dosen : Ichsan Taufik, MT.

#Program yg menggunakan stack

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package Tugas6;

/**
*
* @author MFahmiRizaldi
*/
import java.util.Scanner;
public class PenerapanStack {
int nim;
String nama,jurusan;
PenerapanStack next;
public static Scanner in=new Scanner(System.in);
public static Scanner str=new Scanner(System.in);
public void input(){
System.out.print("Masukkan Nim : ");
nim=in.nextInt();
System.out.print("Masukkan Nama Mahasiswa : ");
nama=str.nextLine();
System.out.print("Masukkan Jurusan : ");
jurusan=str.nextLine();
next=null;
}
public void view(){
System.out.println("Nim : "+nim);
System.out.println("Nama : "+nama);
System.out.println("Jurusan : "+jurusan);
}
public static void main(String[] args) {
int menu=0;
linked st=new linked();
while(menu!=4){
System.out.print("1.push\n2.pop\n3.view\n4.exit\n : ");
menu=in.nextInt();
if(menu==1){
PenerapanStack baru=new PenerapanStack();
baru.input();
st.push(baru);
}
else if(menu==2) st.pop();
else if(menu==3) st.view();
else if(menu==4) System.out.println("keluar . . .");
else System.out.println("salah . . .");
System.out.println(" ");
}
}
}
class linked{
PenerapanStack top;
public linked(){
top=null;
}
public void push(PenerapanStack a){
if(top==null) top=a;
else{
a.next=top;
top=a;
}
}
public void pop(){
if(top==null) System.out.println("kosong");
else{
System.out.println("Popping Data . . .");
top.view();
top=top.next;
}
}
public void view(){
if(top==null) System.out.println("kosong");
else{
PenerapanStack ptr=top;
while(ptr!=null){
System.out.println("- - - - -");
ptr.view();
ptr=ptr.next;
}
}
}
}

You might also like