-
Notifications
You must be signed in to change notification settings - Fork 0
/
cll.cpp
164 lines (160 loc) · 3.11 KB
/
cll.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
#include <iostream>
#include <stdlib.h>
using namespace std;
struct node{
struct node *prev;
struct node *next;
int data;
};
struct node *head;
void tambah_depan();
void tambah_belakang();
void hapus_depan();
void hapus_belakang();
void tampil();
int main(){
int pilihan = 0;
while (pilihan!=6)
{
cout<<"==================================="<<endl;
cout<<" Double Circular Linked List"<<endl;
cout<<"==================================="<<endl;
cout<<"1. Tambah Depan"<<endl;
cout<<"2. Tambah Belakang"<<endl;
cout<<"3. Hapus Depan"<<endl;
cout<<"4. Hapus Belakang"<<endl;
cout<<"5. Tampilkan"<<endl;
cout<<"6. Exit"<<endl;
cout<<"Masukkan Pilihan Anda :"<<endl;
cin>>pilihan;
switch(pilihan){
case 1:
tambah_depan();
break;
case 2:
tambah_belakang();
break;
case 3:
hapus_depan();
break;
case 4:
hapus_belakang();
break;
case 5:
tampil();
break;
case 6:
exit(0);
break;
default:
cout<<"Pilihan yang Anda Masukkan Tidak Tersedia!";
}
}
}
void tambah_depan(){
struct node *ptr,*temp;
int item;
ptr= (struct node *)malloc(sizeof(struct node));
if (ptr==NULL){
cout<<"\nOVERFLOW"<<endl;
}else{
cout<<"\nMasukkan Nilai"<<endl;
cin>>item;
ptr->data=item;
if(head==NULL){
head=ptr;
ptr->next=head;
ptr->prev=head;
}else{
temp=head;
while(temp->next!=head){
temp=temp->next;
}
temp->next=ptr;
ptr->prev=temp;
head->prev=ptr;
ptr->next=head;
head=ptr;
}
cout<<"Node Berhasil Dimasukkan"<<endl;
}
}
void tambah_belakang(){
struct node *ptr,*temp;
int item;
ptr= (struct node *)malloc(sizeof(struct node));
if (ptr==NULL){
cout<<"\nOVERFLOW"<<endl;
}else{
cout<<"\nMasukkan Nilai"<<endl;
cin>>item;
ptr->data=item;
if(head==NULL){
head=ptr;
ptr->next=head;
ptr->prev=head;
}else{
temp=head;
while(temp->next!=head){
temp=temp->next;
}
temp->next=ptr;
ptr->prev=temp;
head->prev=ptr;
ptr->next=head;
}
cout<<"Node Berhasil Dimasukkan"<<endl;
}
}
void hapus_depan(){
struct node*temp;
if(head==NULL){
cout<<"\nOVERFLOW"<<endl;
}else if(head->next==head){
head=NULL;
free(head);
cout<<"Node Berhasil Dihapus"<<endl;
}else{
temp=head;
while(temp->next!=head){
temp=temp->next;
}
temp->next=head->next;
head->next->prev=temp;
free(head);
head=temp->next;
}
}
void hapus_belakang(){
struct node*ptr;
if(head==NULL){
cout<<"\nOVERFLOW"<<endl;
}else if(head->next==head){
head=NULL;
free(head);
cout<<"\nNode Berhasil Dihapus"<<endl;
}else{
ptr=head;
if(ptr->next!=head)
{
ptr=ptr->next;
}
ptr->prev->next=head;
head->prev=ptr->prev;
free(ptr);
cout<<"\nNode Berhasil Dihapus"<<endl;
}
}
void tampil(){
struct node *ptr;
ptr=head;
if(head==NULL){
cout<<"\nTidak ada yang akan dicetak"<<endl;
}else{
cout<<"\nCetak Nilai :"<<endl;
while(ptr->next!=head){
cout<<"\n"<<ptr->data<<endl;
ptr=ptr->next;
} cout<<"\n"<<ptr->data<<endl;
}
}