Jumat, 21 Maret 2014

Stack Uasing Linked List


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


struct tnode{
int hasil;
struct tnode *next;

};
struct tnode *TOP = 0;

void add(){
int angka;
printf("masukkan x:");
scanf("%d",&angka);

struct tnode *node = (struct tnode *)malloc(sizeof(struct tnode));
node->hasil = angka;
node->next = TOP;
TOP = node;
}

void view1(){
if(TOP==0)
printf("kosong");
else
printf("atas:%d",TOP->hasil);

getchar();
}


void hapus(){
if(TOP==0)
printf("kosong");
else{
struct tnode *del=TOP;
TOP=TOP->next;
free(del);
}
}

void main(){
int pilih;
do{
system("cls");
printf("\t\tstack using linked list\n");
printf("\t\t=======================\n\n");
printf("1. add\n");
printf("2. delete\n");
printf("3. view\n");
printf("4. exit\n");
printf("pilihan anda:");
scanf("%d",&pilih);fflush(stdin);
if(pilih<1||pilih>5){
printf("pilih antara 1..4");
getchar();
}else{
switch(pilih){
case 1:add();break;
case 2:hapus();break;
case 3:view1();break;

}
}
}while(pilih!=4);

getchar();
}

Tidak ada komentar: