0% found this document useful (0 votes)
17 views5 pages

Cyrcular Single Linked List

This document contains code for implementing a circular single linked list in C++. It includes functions for initializing the list, adding nodes to the front and back, removing nodes from the front and back, displaying the list, clearing the list, and a main function with a menu to test the different list operations.

Uploaded by

Sri Amelya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views5 pages

Cyrcular Single Linked List

This document contains code for implementing a circular single linked list in C++. It includes functions for initializing the list, adding nodes to the front and back, removing nodes from the front and back, displaying the list, clearing the list, and a main function with a menu to test the different list operations.

Uploaded by

Sri Amelya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

#include <iostream>

#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;
}

void tambahDepan(int databaru)


{
entry = new node;
entry->data = databaru;
entry->next = entry;
if(tail==NULL){
head=entry;
tail=entry;
head->next = head;
tail->next = tail;
}
else {
entry->next = head;
head = entry;
tail->next = head;
}
cout<<" Data "<<databaru<<" berhasil ditambah"<<endl;
}

void tambahBelakang(int databaru)


{
entry = new node;
entry->data = databaru;
entry->next = entry;
if(tail==NULL){
head=entry;
tail=entry;
head->next = head;
tail->next = tail;
}
else {
tail->next = entry;
tail=entry;
tail->next = head;

//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()
{

int pilih, bil;


menu:
{
system("cls");
tampil();
cout<<endl<<endl;
cout<<"\r Cyrcular Single Linked List"<<endl;
cout<<"\r ============================"<<endl;
cout<<"\r | 1. Tambah data depan |"<<endl;
cout<<"\r | 2. Tambah data belakang |"<<endl;
cout<<"\r | 3. Hapus data depan |"<<endl;
cout<<"\r | 4. Hapus data belakang |"<<endl;
cout<<"\r | 5. Clear data |"<<endl;
cout<<"\r | 6. Exit |"<<endl;
cout<<"\r ============================"<<endl;
cout<<endl;
cout<<"\r Pilih : ";cin>>pilih;

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

You might also like