Advertisement
adigaul21

Untitled

Apr 21st, 2019
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.30 KB | None | 0 0
  1. Nama : Rayhan Fathoni Andi Rafif
  2. NPM : 06.2018.1.07023
  3.  
  4. #include <iostream>
  5. #include <cstdlib>
  6. #include <stdlib.h>
  7. #include <conio.h>
  8. using namespace std;
  9. struct node
  10. {
  11. char nama[99];
  12. char npm[99];
  13. node *next;
  14. };
  15.  
  16.  
  17. node *awal_ptr = NULL;
  18. node *posisi;
  19. int option = 0;
  20.  
  21. void tambah_awal_list()
  22. {
  23. node *baru;
  24. baru = new node;
  25.  
  26. cout << "Masukkan nama : ";
  27. cin.getline(baru->nama,sizeof(baru->nama));
  28. cout << "Masukkan NPM : ";
  29. cin >> baru->npm;
  30. baru->next = NULL;
  31. if(awal_ptr == NULL)
  32. {
  33. awal_ptr=baru;
  34. awal_ptr->next = NULL;
  35. }
  36. else
  37. {
  38. baru->next = awal_ptr;
  39. awal_ptr = baru;
  40. }
  41. }
  42.  
  43. void menambah_node_di_akhir()
  44. {
  45. node *temp, *temp2;
  46. temp = new node;
  47. cout << "Masukkan nama : ";
  48. cin.getline(temp->nama,sizeof(temp->nama));
  49. cout << "Masukkan NPM : ";
  50. cin >> temp->npm;
  51. temp->next = NULL;
  52.  
  53. if (awal_ptr == NULL)
  54. {
  55. awal_ptr = temp;
  56. posisi = awal_ptr;
  57. }
  58. else
  59. {
  60. temp2 = awal_ptr;
  61. while (temp2->next != NULL)
  62. {
  63. temp2 = temp2->next;
  64. }
  65. temp2->next = temp;
  66. }
  67. }
  68.  
  69. void display_list()
  70. {
  71. node *temp;
  72. temp = awal_ptr;
  73. cout << endl;
  74. cout << "DATA = ";
  75. if (temp == NULL)
  76. cout << " List kosong! " << endl;
  77. else
  78. {
  79. while (temp != NULL)
  80. {
  81. cout << "\t" << temp->nama << "(" << temp->npm << ")\n";
  82.  
  83. if (temp == posisi)
  84. cout << " <<<< posisi node";
  85.  
  86. temp = temp->next;
  87. }
  88. cout << "\tAkhir list!" << endl;
  89. }
  90.  
  91. }
  92.  
  93. void hapus_awal_node()
  94. {
  95. node *temp;
  96. temp = awal_ptr;
  97. awal_ptr = awal_ptr->next;
  98. delete temp;
  99. }
  100.  
  101. void hapus_akhir_node()
  102. {
  103. node *temp1, *temp2;
  104. if (awal_ptr == NULL)
  105. cout << "List kosong!" << endl;
  106. else
  107. {
  108. temp1 = awal_ptr;
  109. if (temp1->next == NULL)
  110. {
  111. delete temp1;
  112. awal_ptr = NULL;
  113. }
  114. else
  115. {
  116. while (temp1->next != NULL)
  117. {
  118. temp2 = temp1;
  119. temp1 = temp1->next;
  120. }
  121. delete temp1;
  122. temp2->next = NULL;
  123. }
  124. }
  125. }
  126.  
  127.  
  128. void tambah_tengah_list()
  129. {
  130. node *baru, *bantu;
  131. int posisi_sisip;
  132. if(awal_ptr != NULL)
  133. {
  134. cout<<"Akan disisip setelah Data Ke ? : ";
  135. cin>>posisi_sisip;
  136. baru =new node;
  137. bantu=awal_ptr;
  138.  
  139. for(int i=1;i<posisi_sisip-1;i++) {
  140. if(bantu->next != NULL)
  141. bantu=bantu->next;
  142. else
  143. break;
  144. }
  145. cout << "Masukkan nama : ";
  146. cin.getline(baru->nama,sizeof(baru->nama));
  147. cout << "Masukkan NPM : ";
  148. cin >> baru->npm;
  149. baru->next=bantu->next;
  150. bantu->next=baru;
  151. }
  152. else
  153. {
  154. cout<<"Belum ada data !! silahkan isi data dulu....";
  155. getch();
  156. }
  157. }
  158. void Hapus_tengah_list()
  159. {
  160. int banyakdata,posisi_hapus,poshapus;
  161. node *hapus, *bantu;
  162. if(awal_ptr != NULL)
  163. {
  164. cout<<" Akan dihapus pada data ke : ";
  165. cin>>posisi_hapus;
  166. banyakdata=1;
  167. bantu=awal_ptr;
  168. while(bantu->next != NULL)
  169. {
  170. bantu=bantu->next;
  171. banyakdata++;
  172. }
  173. if((posisi_hapus<1)||(posisi_hapus>banyakdata))
  174. {
  175. cout<<"Belum ada data !! masukkan Data dula aja...\n";
  176. }
  177. else
  178. {
  179. bantu=awal_ptr;
  180. poshapus=1;
  181. while(poshapus<(posisi_hapus-1))
  182. {
  183. bantu=bantu->next;
  184. poshapus++;
  185. }
  186. hapus=bantu->next;
  187. bantu->next=hapus->next;
  188. delete hapus;
  189. }
  190. }
  191. else
  192. cout<<"Data Masih kosong, tidak bisa hapus data dari tengah! ";
  193. getch();
  194. }
  195.  
  196.  
  197. int main()
  198. {
  199. awal_ptr = NULL;
  200. do
  201. {
  202.  
  203. display_list();
  204. cout << endl;
  205. cout << "MENU PILIHAN : " << endl;
  206. cout << "0. Keluar program." << endl;
  207. cout << "1. Tambah awal list." << endl;
  208. cout << "2. Tambah akhir list." << endl;
  209. cout << "3. Tambah tengah list."<< endl;
  210. cout << "4. Hapus awal list." << endl;
  211. cout << "5. Hapus akhir list." << endl;
  212. cout << "6. Hapus tengah list." << endl;
  213. cout << endl << " Pilihan >> ";
  214. cin >> option;
  215. cin.ignore();
  216.  
  217. switch (option)
  218. {
  219. case 1 : tambah_awal_list();
  220. break;
  221. case 2 : menambah_node_di_akhir();
  222. break;
  223. case 3 : tambah_tengah_list();
  224. break;
  225. case 4 : hapus_awal_node();
  226. break;
  227. case 5 : hapus_akhir_node();
  228. break;
  229. case 6 : Hapus_tengah_list();
  230. break;
  231. }
  232. }
  233. while (option != 0);
  234. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement