-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathct.cpp
227 lines (225 loc) · 3.18 KB
/
ct.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
124
125
126
127
128
129
130
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
#include <iostream>
#include <conio.h>
#include <cstdlib>
using namespace std;
struct data
{
int angka;
data *next;
}*baru, *kepala=NULL, *ekor=NULL, *tampil, *hapus, *tambah, *a,
*b;
//fungsi untuk menginputkan data
void gerbong()
{
int nilai;
cout<<"\nMasukkan nilai : ";
cin>>nilai;
baru = new data;
baru->angka = nilai;
baru->next = NULL;
}
///////////////////////////////////
//fungsi untuk menambah depan
void tambah_depan()
{
gerbong();
if(kepala==NULL)
{
kepala = baru;
ekor = baru;
}
else
{
baru->next = kepala;
kepala = baru;
}
}
/////////////////////////////////
//fungsi untuk menambah belakang
void tambah_belakang()
{
gerbong();
if(kepala==NULL)
{
kepala = baru;
ekor = baru;
}
else
{
ekor->next = baru;
ekor = baru;
}
}
/////////////////////////////////
//fungsi untuk menampilkan data
void muncul()
{
if(kepala==NULL)
{
cout<<"Data kosong";
}
else
{
tampil = kepala;
while(tampil!=NULL)
{
cout<<tampil->angka<<" ";
tampil = tampil->next;
}
}
}
////////////////////////////////
//fungsi untuk menambah tengah
void tambah_tengah()
{
int masuk;
gerbong();
cout<<"\nData yang ada : ";
muncul();
cout<<"\n\ningin dimasukkan setelah data : ";
cin>>masuk;
if(kepala==NULL)
{
kepala = baru;
ekor = baru;
}
else
{
tambah = kepala;
while(tambah->angka!=masuk)
{
tambah = tambah->next;
}
baru->next = tambah->next;
tambah->next = baru;
if(baru->next==NULL)
{
ekor=baru;
}
}
}
///////////////////////////////////////////
//fungsi untuk menghapus depan
void hapus_depan()
{
hapus = kepala;
if(kepala==NULL)
{
cout<<"\nData kosong";
}
else
{
kepala = kepala->next;
delete hapus;
}
}
//////////////////////////////////////
//fungsi untuk menghapus belakang
void hapus_belakang()
{
hapus = kepala;
if(kepala==NULL)
{
cout<<"\nData kosong";
}
else if (kepala==ekor)
{
delete hapus;
kepala = ekor = NULL;
}
else
{
while(hapus->next!=ekor)
{
hapus = hapus->next;
}
ekor = hapus;
ekor->next = NULL;
hapus = hapus->next;
delete hapus;
}
}
////////////////////////////////////////
//fungsi untuk menghapus tengah
void hapus_tengah()
{
int hps;
cout<<"\nData yang ada : ";
muncul();
cout<<"\ningin menghapus nilai berapa : ";
cin>>hps;
hapus = kepala;
if(kepala==NULL)
{
cout<<"\nData kosong";
}
else if (hapus->angka==hps)
{
hapus_depan();
}
else
{
while(hapus->angka!=hps)
{
a = hapus;
hapus = hapus->next;
b = hapus->next;
}
a->next = b;
delete hapus;
}
}
/////////////////////////////////////////
int main()
{
int pilih;
do
{
cout<<"\n1. tambah depan";
cout<<"\n2. tambah belakang";
cout<<"\n3. tambah tengah";
cout<<"\n4. hapus depan";
cout<<"\n5. hapus belakang";
cout<<"\n6. hapus tengah";
cout<<"\n7. tampil";
cout<<"\n8. keluar...";
cout<<"\nMasukkan pilihan : ";
cin>>pilih;
if (pilih==1)
{
tambah_depan();
}
else if (pilih==2)
{
tambah_belakang();
}
else if (pilih==3)
{
tambah_tengah();
getch();
}
else if (pilih==4)
{
hapus_depan();
}
else if (pilih==5)
{
hapus_belakang();
}
else if (pilih==6)
{
hapus_tengah();
}
else if(pilih==7)
{
muncul();
getch();
}
else
{
exit(1);
}
system("cls");
}
while(1);
}