1 2 3 4 5 6 7 8 9 10 11  |    #include    #include    #include    struct TNode {             int data;             TNode *next; }; TNode *head, *tail,   *bantu, *baru, *hapus;  |   
12 13 14 15 16  |    void init() {             head = NULL; }  |   
17 18 19 20 21 22  |    int isEmpty() {             if(head == NULL) return 1;             else return 0; }  |   
23 24 25 26 27 28 29  |    void NodBaru(int   databaru) {             baru   = new TNode;             baru->data = databaru;             baru->next = NULL; }  |   
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45  |    void   insertDepan(int databaru) {             NodBaru(databaru);             if(isEmpty() == 1)              {                         head = baru;                         tail = baru;             }             else             {                         baru->next = head;                           head = baru;             }             cout<<"\n----- Data   masuk ------\n"; }  |   
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61  |    void   insertBelakang(int databaru) {             NodBaru(databaru);             if(isEmpty() == 1)              {                         head   = baru;                         tail = baru;             }             else             {                           tail->next = baru;                           tail = baru;             }             cout << "\n----- Data   masuk ------\n"; }  |   
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84  |    void hapusDepan() {             int del;             if (isEmpty() == 0)             {                         if(head->next !=   NULL)                         {                                       hapus = head;                                     del = hapus->data;                                     head = head->next;                                     delete hapus;                           }                         else                         {                                     del = head->data;                                     head   = tail = NULL;                          }                         cout   << "\n----- Data " << del << " terhapus   -----\n";             }             else                         cout << "\n----- List   masih kosong -----\n"; }  |   
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113  |    void   hapusBelakang() {             int del;             if (isEmpty() == 0)              {                         if(head->next !=   NULL)                          {                                 bantu = head;                                 while(bantu->next   != tail)                                 {                                           bantu = bantu->next;                                 }                                 hapus = tail;                                 tail = bantu;                                 del = hapus->data;                                  delete hapus;                                 tail->next = NULL;                         }                        else                         {                                 del = head->data;                                 head = tail = NULL;                       }                         cout <<   "\n----- Data " << del << " terhapus   -----\n";              }              else                        cout << "\n----- List   masih kosong -----\n"; }  |   
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130  |    void tampil() {             if(isEmpty() == 0)              {                        bantu = head;                       cout<< "\nData yang ada   dalam list:\n\n";                        while(bantu != NULL)                        {                                cout << bantu->data   <<"-->";                               bantu = bantu->next;                        }                         cout <<   "NULL" << endl;               }              else                       cout << "\n----- List   masih kosong -----\n"; }  |   
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200  |    void main() {    int databaru, pil;    menu:    clrscr();    cout<<" ||+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+||   "<    cout<<"   (1) Insert Depan"<    cout<<"   (2) Insert Belakang"<    cout<<"   (3) Hapus Depan"<    cout<<"   (4) Hapus Belakang"<    cout<<"   (5) Tampil"<    cout<<"   (6) Exit Program"<    cout<<"   ||+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+|| "<    cout<<" masukan pilihan anda :   ";    cin >> pil;    switch(pil)    {    case 1:{                cout << "\nMasukkan   data : ";                      cin >> databaru;                insertDepan(databaru);                cout << "\nTekan   Enter untuk kembali ke menu.";                getche();                goto menu;                break;               }   case 2: {                cout << "\nMasukkan   data : ";                      cin >> databaru;                insertBelakang(databaru);               cout   << "\nTekan Enter untuk kembali ke menu.";                getche();                goto menu;                break;                }   case 3: {                hapusDepan();                cout << "\nTekan   Enter untuk kembali ke menu.";                getche();                goto menu;                break;               }   case 4: {                hapusBelakang();                cout << "\nTekan   Enter untuk kembali ke menu.";                getche();                goto menu;                break;               }   case 5: {                tampil();                cout << "\nTekan   Enter untuk kembali ke menu.";                getche();                goto menu;                break;               }   case   6: {                cout << "\n-----   Program keluar -----\n";                break;                }   default:{                cout <<   "\n-----Salah input -----\n";                cout << "\nTekan   Enter untuk kembali ke menu.";                getche();                goto menu;                break;               }    }   getch(); }  |   







4 komentar:
header filenya: iostream.h, conio.h, dan stdlib.h
slamat mencoba,...:)
hmhm panjang amat.....tapi siap untu dipelajari untuk memahaminya
:@
Kang, ini latihan buat teknik elektro pengganti jadwal bukan?
Tsani --> bukan, untuk yg T.Elektro blum sy posting. Bentar lagi sy posting...
Posting Komentar