Cyrcular Single Linked List
Cyrcular Single Linked List
#include <conio.h>
#include <iomanip>
#include <stdlib.h>
using namespace std;
struct node
{
int data;
node *next;
};
node *head;
node *tail;
node *curr;
node *entry;
node *del;
void inisialisasi()
{
head = NULL;
tail = NULL;
}
//curr=head;
//while(curr->next!=NULL){
// curr=curr->next;
//}
//curr->next = entry;
}
cout<<" Data "<<databaru<<" berhasil ditambah"<<endl;
}
void hapusDepan()
{
if(tail!=NULL){
int d;
del = head;
d = head->data;
if(head != tail){
del = head;
head = head->next;
tail->next = head;
delete del;
} else {
head = NULL;
tail = NULL;
}
cout<<"\n Data yang dihapus adalah "<<d<<endl;
}
else
cout<<"\n Linked list kosong, penghapusan tidak bisa dilakukan"<<endl;
}
void hapusBelakang()
{
if (tail!=NULL){
int d;
if(head == tail){
d = tail->data;
head = NULL;
tail = NULL;
} else {
curr = head;
while(curr->next != tail){
curr = curr->next;
}
del = tail;
tail = curr;
d = del->data;
tail->next = head;
delete del;
}
cout<<"\n Data yang dihapus adalah "<<d<<endl;
}
else cout<<"\n Linked list kosong, penghapusan tidak bisa dilakukan"<<endl;
}
void tampil()
{
curr = head;
if(tail != NULL){
cout<<"\n Data yang ada dalam linked list adalah"<<endl;
cout<<setw(6);
do {
cout<<curr->data<<"->";
curr=curr->next;
} while (curr != tail->next);
cout<<endl;
}
else cout<<"\n Tidak ada data dalam linked list"<<endl;
}
void clear()
{
if(tail != NULL)
{
curr = head;
while (curr->next != head){
del = curr;
curr = curr->next;
delete del;
}
head = NULL;
tail = NULL;
cout<<"\n Data clear";
} else {
cout<<"\n Linked list sudah kosong"<<endl;
}
}
int main()
{
switch(pilih)
{
case 1:
cout<<" Input Data : ";cin>>bil;
tambahDepan(bil);
getch ();
break;
case 2:
cout<<" Input Data : ";cin>>bil;
tambahBelakang(bil);
getch ();
break;
case 3:
hapusDepan ();
getch();
break;
case 4:
hapusBelakang();
getch();
break;
case 5:
clear();
getch();
break;
case 6:
exit(0);
}
goto menu;
}
}
Dengan output