TI

Pertanyaan

contoh program c++ lengkap salah satu jenis stack

1 Jawaban

  • Single Stack

    Screen Shot



    Syntax :

    #include<stdio.h>
    #include<stdlib.h>
    #include<conio>

    typedef int ItemType;
    typedef struct simpul node;
    struct simpul
    {
    ItemType item;
    node *next;
    };

    struct Stack
    {
    node *TOS;
    };

    node *baru;
    void allocate_node(ItemType x)
    {
    baru = (node *) malloc (sizeof(node));
    if(baru==NULL)
    {
    printf(“Alokasi Gagal\n”);
    exit(1);
    }
    else
    {
    baru->item=x;
    baru->next=NULL;
    }
    }

    void inisialisasi(Stack *s)
    {
    s->TOS = NULL;
    }
    int kosong(Stack *s)
    {
    return s->TOS==NULL;
    }

    void push(Stack *s)
    {
    baru->next = s->TOS;
    s->TOS = baru;
    }

    ItemType pop(Stack *s)
    {
    node *temp;
    if(kosong(s))
    {
    printf(“Data Kosong\n”);
    return ‘ ‘;
    }
    else
    {
    temp = s->TOS;
    s->TOS = s->TOS->next;
    return temp->item;
    free(temp);
    temp=NULL;
    }
    }

    void tampil(Stack *s)
    {
    Stack bantu;
    bantu = *s;
    printf(“\nData Simpul ==>  “);
    while(bantu.TOS!=NULL)
    {
    printf(“%d “, bantu.TOS->item);
    bantu.TOS = bantu.TOS->next;
    }
    printf(“\n\n”);
    }

    void main()
    {
    int pilih, data;
    char lagi=’y’;
    Stack ujung;
    inisialisasi(&ujung);
    while(lagi==’y’)
    {
    system(“CLS”);
    //tampil(&ujung);
    printf(“Menu Pilihan : \n”);
    printf(“1. Push\n”);
    printf(“2. Pop\n”);
    printf(“3. Tampilkan Stack\n”);
    printf(“\nPilih No          : “);
    scanf(“%d”, &pilih);
    switch(pilih)
    {
    case 1:
    printf(“Masukkan data     : “);
    scanf(“%d”, &data);
    allocate_node(data);
    push(&ujung);
    break;

    case 2:
    pop(&ujung);
    break;

    case 3:
    tampil(&ujung);
    break;
    }

    fflush(stdin);
    printf(“Lagi (y/t) ? “);
    scanf(“%c”, &lagi);
    clrscr();
    }
    }

    2. Double Stack

    Screen Shot :



    Syntax :

    #include “stdio.h”
    #include “conio.h”
    #define MAX 10
    #define true 1
    #define false 0
    char stack[MAX];
    int top1, top2;

    void init(void);
    void push(char data, int nomorstack);
    char pop(int nomorstack);
    void clear(int nomorstack);
    int full(void);
    int empty(int nomorstack);
    void baca();

    main(){
    char data;
    int pilih, nomorstack;
    init();
    do{
    clrscr();
    printf(“Contoh program double stack”);
    printf(“\n1. Push”);
    printf(“\n2. Pop”);
    printf(“\n3. Clear”);
    printf(“\n4. Baca”);
    printf(“\n5. Selesai…”);
    printf(“\nPilihan anda  : \n”);
    scanf(“%i”,&pilih);
    switch(pilih){

    case 1: printf(“Push\n”);
    printf(“Masukkan datanya :\n”); scanf(“%s”,&data);
    printf(“Mau dimasukkan ke stack berapa ? 1 atau 2 ? :\n”);
    scanf(“%i”,&nomorstack);
    push(data, nomorstack);
    break;

    case 2: printf(“Pop\n”);
    printf(“Masukkan nomor stack\n”);
    scanf(“%i”,&nomorstack);
    data=pop(nomorstack);
    printf(“\nData yang dikeluarkan adalah %s”, data);
    break;

    case 3: printf(“Clear\n”);
    printf(“Nomor Stack yang akan dikosongkan \n”);
    scanf(“%i”,&nomorstack);
    clear(nomorstack);
    break;

    case 4: printf(“Baca\n”);
    baca();
    break;

    case 5: printf(“Exit”);
    break;
    default: printf(“Pilihan yang anda masukkan tidak ada”);
    break;
    }

    }while(pilih!=5);
    getch();
    }

    //init
    void init(){
    top1=0;
    top2=MAX+1;
    }

    //PUSH
    void push(char data, int nomorstack){
    if(full()!=true){
    switch(nomorstack){

    case 1: top1++;
    stack[top1]=data;
    break;

    case 2: top2–;
    stack[top2]=data;
    break;
    default: printf(“\nNomor stack salah”);
    break;
    }
    }
    else
    printf(“\nStack penuh”);
    getch();
    }

    //POP
    char pop(int nomorstack){
    char data;
    if(empty(nomorstack)!=true){
    switch(nomorstack){

    case 1: data=stack[top1];
    top1–;
    return data;
    break;

    case 2: data=stack[top2];
    top2++;
    return data;
    break;
    default: printf(“\nNomor stack salah”);
    break;
    }
    }
    else printf(“\nStack masih kosong”);
    getch();
    return 0;
    }
    //cek full

    int full(void){
    if(top1+1>=top2){
    return true;
    }
    else return false;
    }

    //cek empty
    int empty(int nomorstack){
    switch(nomorstack){
    case 1: if(top1==0) return true;
    else return false;
    break;

    case 2: if(top2==MAX+1) return true;
    else return false;
    break;
    default: printf(“nomor stack salah”);
    break;
    }
    }

    //clearing
    void clear(int nomorstack){
    switch(nomorstack){
    case 1: top1=0;
    break;

    case 2: top2=MAX+1;
    break;
    default: printf(“Nomor stack salah”);
    break;
    }
    }

    void baca(){
    int i;
    printf(“Baca isi stack pertama \n”);
    for(i=1; i<=top1; i++){
    printf(”  %c “,stack[i]);
    printf(“\n”);
    }
    printf(“Isi stack kedua\n”);
    for(i=MAX; i>=top2; i–){
    printf(”  %c “,stack[i]);
    printf(“\n”);
    }

    getch();
    }

Pertanyaan Lainnya