/************************************************************************************************************
* Nama Program : Implementasi Stack Dan Queue Non circular *
* Menggunakan Linked List Non Circular *
* Nama : Wayan Wardana *
* Tanggal : 15 November 2008 *
*************************************************************************************************************/
#include <iostream>
using namespace std;
int databaru,menu;
struct TNode
{
int data;
TNode *next;
};
TNode *top;
void init()
{
top = NULL;
}
int isEmpty()
{
if(top == NULL)
return 1;
else
return 0;
}
void push(int databaru)
{
TNode *baru;
baru = new TNode;
baru->data = databaru;
baru->next = NULL;
if(isEmpty()==1)
{
top=baru;
top->next = NULL;
}
else
{
baru->next = top;
top = baru;
}
cout<<"Data masuk\n";
}
void pop()
{
TNode *hapus;
int d;
if (isEmpty()==0)
{
if(top->next != NULL)
{
hapus = top;
d = hapus->data;
top = top->next;
delete hapus;
}
else
{
d = top->data;
top = NULL;
}
cout<<d<<" terhapus\n";
} else cout<<"Masih kosong\n";
}
void dequeue()
{
TNode *hapus,*bantu;
int d;
if (isEmpty()==0)
{
if(top->next != NULL)
{
bantu = top;
while(bantu->next->next!=NULL)
{
bantu = bantu->next;
}
hapus = bantu->next;
d = hapus->data;
bantu->next = NULL;
delete hapus;
}
else
{
d = top->data;
top = NULL;
}
cout<<d<<" terhapus\n";
} else
cout<<"Masih kosong\n";
}
void clear()
{
TNode *bantu,*hapus;
bantu = top;
while(bantu!=NULL)
{
hapus = bantu;
bantu = bantu->next;
delete hapus;
}
top = NULL;
}
void tampilstack()
{
TNode *bantu;
bantu = top;
if(isEmpty()==0)
{
while(bantu!=NULL)
{
cout<<"| "<<bantu->data<<" |";
cout<<endl;
bantu=bantu->next;
}
cout<<endl;
} else cout<<"Data kosong\n";
}
void tampilqueue()
{
TNode *bantu;
bantu = top;
if(isEmpty()==0)
{
while(bantu!=NULL)
{
cout<<" "<<bantu->data<<" ";
bantu=bantu->next;
}
cout<<endl;
} else cout<<"Data kosong\n";
}
void mainstack()
{
do
{
cout<<"MENU PROGRAM\n";
cout<<"============\n";
cout<<"1. Push\n";
cout<<"2. Pop\n";
cout<<"3. Clear\n";
cout<<"4. Print\n";
cout<<"5. Menu Sebelumnya\n";
cout<<"Masukan pilihan : ";
cin>>menu;
if (menu<1||menu>5)
{
cout<<"\nKode Pilihan salah\n\n";
}
else
{
switch (menu)
{
case 1 :
{
cout<<"Masukkan data= ";
cin>>databaru;
push(databaru);
cout<<endl<<endl;
}
break;
case 2 :
{
cout<<endl;
pop ();
cout<<endl;
}
break;
case 3 :
{
cout<<endl;
clear();
cout<<endl;
}
break;
case 4 :
{
cout<<endl;
tampilstack();
cout<<endl;
}
break;
}
}
}while(menu!=5);
clear();
}
void mainqueue()
{
do
{
cout<<"MENU PROGRAM\n";
cout<<"============\n";
cout<<"1. Queue\n";
cout<<"2. Dequeue\n";
cout<<"3. Clear\n";
cout<<"4. Print\n";
cout<<"5. Menu Sebelumnya\n";
cout<<"Masukan pilihan : ";
cin>>menu;
if (menu<1||menu>5)
{
cout<<"\nKode Pilihan salah\n\n";
}
else
{
switch (menu)
{
case 1 :
{
cout<<"Masukkan data= ";
cin>>databaru;
push(databaru);
cout<<endl<<endl;
}
break;
case 2 :
{
cout<<endl;
dequeue();
cout<<endl;
}
break;
case 3 :
{
cout<<endl;
clear();
cout<<endl;
}
break;
case 4 :
{
cout<<endl;
tampilqueue();
cout<<endl;
}
break;
}
}
}while(menu!=5);
clear();
}
void main()
{
do
{
cout<<"\t ============================================================="<<endl;
cout<<"\t| |"<<endl;
cout<<"\t| IMPLEMENTASI SINGLE STACK DAN QUEUE NON CIRCULAR |"<<endl;
cout<<"\t| MENGGUNAKAN SINGLE LINKED LIST NON CIRCULAR |"<<endl;
cout<<"\t| |"<<endl;
cout<<"\t ============================================================= "<<endl<<endl;
cout<<"MENU UTAMA\n";
cout<<"==========\n";
cout<<"1. Linked List Non Circular Dengan Stack\n";
cout<<"2. Linked List Non Circular Dengan Queue Non Circular\n";
cout<<"3. Exit Program\n";
cout<<"Masukan pilihan : ";
cin>>menu;
if (menu<1||menu>3)
{
cout<<"\nKode Pilihan salah\n\n";
}
else
{
switch (menu)
{
case 1 :
{
cout<<endl<<endl;
mainstack();
cout<<endl<<endl;
}
break;
case 2 :
{
cout<<endl<<endl;
mainqueue();
cout<<endl<<endl;
}
break;
}
}
}while(menu!=3);
}
Harap Maklum Jika ada kesalahan
thank a lot
BalasHapusma kasih semoga bisa bermanfaat
BalasHapusKok ga bisa dicompile ya
BalasHapusharusnya bisa kk,.. coba teliti kembali setiap carakternya
Hapus