-
Notifications
You must be signed in to change notification settings - Fork 0
/
SLL_G1A021037.cpp
123 lines (83 loc) · 2.28 KB
/
SLL_G1A021037.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
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
114
115
116
117
118
119
120
121
122
123
#include <iostream>
using namespace std;
//komponen
struct Mahasiswa{
string Nama, NPM, Prodi;
Mahasiswa *next;
};
Mahasiswa *head, *tail, *cur, *newNode, *del;
//buat single linked list
void createSingleLinkedList(string Nama, string NPM, string Prodi){
head = new Mahasiswa();
head->Nama = Nama;
head->NPM = NPM;
head->Prodi = Prodi;
head->next = NULL;
tail = head;
}
//tambahkan awal single linked list
void addFirst(string Nama, string NPM, string Prodi){
newNode = new Mahasiswa();
newNode->Nama = Nama;
newNode->NPM = NPM;
newNode->Prodi = Prodi;
newNode->next = head;
head = newNode;
}
//tambahkan akhir single linked list
void addLast(string Nama, string NPM, string Prodi){
newNode = new Mahasiswa();
newNode->Nama = Nama;
newNode->NPM = NPM;
newNode->Prodi = Prodi;
newNode->next = NULL;
tail->next = newNode;
tail = newNode;
}
//hapus awal
void removeFirst(){
del = head;
head = head->next;
delete del;
}
//hapus akhir
void removeLast(){
del = tail;
cur = head;
while(cur->next != tail){
cur = cur->next;
}
tail = cur;
tail->next = NULL;
delete del;
}
//cetak single linked list
void printSingleLinkedList(){
cur = head;
while( cur != NULL ) {
cout << "Nama Mahasiswa :" << cur->Nama << endl;
cout << "NPM Mahasiswa :" << cur->NPM << endl;
cout << "Prodi Mahasiswa :" << cur->Prodi << endl;
cur = cur->next;
}
}
int main(){
createSingleLinkedList("M.Anjasfedo Afridiansah", "G1A021037", "Informatika" );
printSingleLinkedList();
cout << "\n\n" << endl;
addFirst("Fahmi Yohari Edward", "B1A021387", "Ilmu Hukum" );
printSingleLinkedList();
cout << "\n\n" << endl;
addLast("Alif Thareq Aziz", "C1C021079", "Akuntansi" );
printSingleLinkedList();
cout << "\n\n" << endl;
removeFirst();
printSingleLinkedList();
cout << "\n\n" << endl;
addLast("Fardho Tri Kurniawan", "D1D021065", "Administrasi Publik" );
printSingleLinkedList();
cout << "\n\n" << endl;
removeLast();
printSingleLinkedList();
cout << "\n\n" << endl;
}