Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. #include <iostream>
  2. #include <conio.h>
  3. #include <iomanip> //setw()
  4. using namespace std;
  5.  
  6. struct node
  7. {
  8.       int data;
  9.       node* next; // untuk menghubungkan dengan node lain, tipe data dibuat sama seperi aturan penggunaan pointer.
  10. };
  11.  
  12. node* head;
  13. node* tail;
  14. node* curr;
  15. node* entry;
  16. node* del;
  17.  
  18. void inisialisasi()
  19. {
  20.       head = NULL;
  21.       tail = NULL;
  22. }
  23.  
  24. void input(int dt)
  25. {
  26.       entry = (node* )malloc(sizeof(node)); //alokasi memori
  27.       entry->data = dt;
  28.       entry->next = NULL;
  29.       if(head==NULL)
  30.       {
  31.             head = entry;
  32.             tail = head;
  33.       }
  34.       else
  35.       {
  36.             tail->next = entry;
  37.             tail = entry;
  38.       }
  39. }
  40.  
  41. void hapus()
  42. {
  43.       int simpan;
  44.       if(head==NULL)
  45.       {
  46.             cout<<"\nlinked list kosong, penghapusan tidak bisa dilakukan"<<endl;
  47.       }
  48.       else
  49.       {
  50.             simpan  = head ->data;
  51.             //hapus depan
  52.             del = head;
  53.             head = head->next;
  54.             delete del;
  55.            
  56.             cout<<"\ndata yang dihapus adalah "<<simpan<<endl;
  57.       }
  58.  
  59. }
  60.  
  61. void cetak()
  62. {
  63.       curr = head;
  64.       if(head == NULL)
  65.             cout<<"\ntidak ada data dalam linked list"<<endl;
  66.       else
  67.       {
  68.             cout<<"\nData yang ada dalam linked list adalah"<<endl;
  69.             cout<<setw(6);
  70.             while(curr!=NULL)
  71.             {
  72.                   cout<<curr->data<<"->";
  73.                   curr = curr->next;
  74.             }
  75. cout<<"NULL";
  76.             cout<<endl;
  77.       }
  78. }
  79.  
  80. void menu()
  81. {
  82.       char pilih, ulang;
  83.       int data;
  84.  
  85.       do
  86.       {
  87.       system("cls");
  88.       cout<<"SINGLE LINKED LIST NON CIRCULAR"<<endl;
  89.       cout<<"-------------------------------"<<endl;
  90.       cout<<"Menu : "<<endl;
  91.       cout<<"1. Input data"<<endl;
  92.       cout<<"2. Hapus data"<<endl;
  93.       cout<<"3. Cetak Data"<<endl;
  94.       cout<<"4. Exit"<<endl;
  95.       cout<<"Masukkan pilihan Anda : ";
  96.       cin>>pilih;
  97.  
  98.       switch(pilih)
  99.       {
  100.       case '1' :
  101.             cout<<"\nMasukkan data : ";
  102.             cin>>data;
  103.             input(data);
  104.             break;
  105.       case '2' :
  106.             hapus();
  107.             break;
  108.       case '3' :
  109.             cetak();
  110.             break;
  111.       case '4' :
  112.             exit(0);
  113.             break;
  114.       default :
  115.             cout<<"\nPilih ulang"<<endl;
  116.       }
  117.       cout<<"\nKembali ke menu?(y/n)";
  118.       cin>>ulang;
  119.       }while(ulang=='y' || ulang=='Y');
  120. }
  121.  
  122.  
  123. int main()
  124. {
  125.  
  126.       inisialisasi();
  127.       menu();
  128.  
  129.       return EXIT_SUCCESS;
  130. }