#include<stdio.h> #include<stdlib.h> typedef int ItemType; typedef struct simpul node; struct simpul { ItemType item; node *next; }; struct Stack{ node *TOS; }; node *baru; void awal() { puts("==================================================="); puts("= PROGRAM STACK DENGAN LINKED LIST ="); puts("===================================================\n"); puts("NRP : 7411030828"); puts("Nama : M.Faishal Imam Choiri\n"); } 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"); awal(); //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); } }
http://l.ead.me/dhtzl
ReplyDeletehttps://desainerhub.com
ReplyDelete