#include<iostream>
#include<conio.h>
#include<stdlib.h>
using namespace std;
class node {
public:
class node *next;
int data;
};
class stack : public node {
node *head;
int tos;
public:
stack() {
tos=-1;
}
void push(int x) {
if (tos < 0 ){
head =new node;
head->next=NULL;
head->data=x;
tos ++;
}
else {
node *temp,*temp1;
temp=head;
if(tos >= 4) {
cout <<"stack over flow";
return;
}
tos++;
while(temp->next != NULL)
temp=temp->next;
temp1=new node;
temp->next=temp1;
temp1->next=NULL;
temp1->data=x;
}
}
void display() {
node *temp;
temp=head;
if (tos < 0) {
cout <<" stack under flow";
return;
}
while(temp != NULL)
{
cout <<temp->data<< " ";
temp=temp->next;
}
}
void pop() {
node *temp;
temp=head;
if( tos < 0 ) {
cout <<"stack under flow";
return;
}
tos--;
while(temp->next->next!=NULL) {
temp=temp->next;
}
temp->next=NULL;
}
};
int main() {
stack s1;
int ch;
while(1) {
cout <<"\n1.Tambah\n2.Ambil\n3.Tampilkan\n4.Keluar\n Masukan Pilihan Anda : ";
cin >> ch;
switch(ch) {
case 1: cout <<"\n Masukan Elemen : ";
cin >> ch;
s1.push(ch);break;
case 2: s1.pop();break;
case 3: s1.display(); break;
case 4: exit(0);
}
}
return (0); }
Output :
Saturday 5 April 2014
Contoh Program Stack Singgle Linked list
Posted by tegar on Saturday 5 April 2014
Previous
« Prev Post
« Prev Post
Next
Next Post »
Next Post »
Subscribe to:
Post Comments (Atom)
wah yang ini nih ringkas mudah di mengerti :)
ReplyDeletethx gan
referensi bagus. thanks gan
ReplyDelete