Contoh program STACK dalam bahasa C++
Berikut adalah contoh script program STACK dalam bahasa C++
#include <iostream> #include <stdlib.h> #include <conio.h> #define MAX 5 using namespace std;
int top=0, Stack[MAX];
void push(){ if(top == MAX-1){ cout << ">> Data sudah Penuh !" << endl; }else{ top++; cout << ">> Masukan Data : "; cin >> Stack[top]; cout << "\n Pada Index ke '" << top << "' \n "; cout << "Data [" << Stack[top] << "] Telah Ditambah ! " << endl; } } void pop(){ if(top == 0){ cout << ">> Data Kosong !" << endl; }else{ cout << "\n Data [" << Stack[top] << "] pada index ke '" << top << "' dalam Stack Diambil !" << endl; Stack[top--]; } } int clearr(){ return top = 0; } void Print(){ if (top == 0) { cout << " Program Stack"; cout << "\n==========================="; cout << "\n Data : "; cout << "\n___________________________" << endl; cout << " Kosong! \n___________________________" << endl; } else { cout << " Data : "; cout << "\n___________________________" << endl; for (int i = top; i > 0; i--){ cout << " _____[ " << Stack[i] << " ]_____" << endl; } cout << "___________________________" << endl; } } int main() { int choose; do { Print(); cout << "Menu Perintah" << "\n1. Masukkan Data" << "\n2. Ambil Data" << "\n3. Hapus Semua" << "\n4. Keluar" << "\nJumlah Data : " << top << "\n\n>> Pilih : "; cin >> choose; switch (choose){ case 1: push();getch(); break; case 2: pop();getch(); break; case 3: clearr(); cout << "\nClear Succes" << endl; getch(); break; case 4: cout << "Terima Kasih telah menggunakan program ini :)" << endl; getch(); break; default: cout << "Pilihan tidak ada\nMasukan pilihan sesuai dengan daftar" << endl; getch(); break; } system("cls"); }while(choose !=4); }
|
Komentar
Posting Komentar