Last active
December 16, 2019 08:23
-
-
Save teramuza/f8ff9554966c6c4737aec82ec3bd07ab to your computer and use it in GitHub Desktop.
Singly Circular Linked List (id-ID)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Singly Circular Linked List with 2 directions of addition and deletion | |
* | |
* Created By : teramuza (@tera.ajaa) 16/12/19 | |
* Lang : id-ID | |
*/ | |
#include <iostream> | |
#include <stdlib.h> | |
using namespace std; | |
typedef struct Node { | |
int info; | |
struct Node *next; | |
}node; | |
node *front=NULL, *rear=NULL, *temp; | |
void insertFront() { | |
node *newNode; | |
newNode = (node*)malloc(sizeof(node)); | |
cout << "Enter value : "; | |
cin >> newNode->info; | |
newNode->next=NULL; | |
if(rear == NULL) | |
front = rear = newNode; | |
else { | |
rear->next = newNode; | |
rear = newNode; | |
} | |
rear->next=front; | |
} | |
void insertBack() { | |
node *newNode; | |
newNode = (node*)malloc(sizeof(node)); | |
cout << "Enter value : "; | |
cin >> newNode->info; | |
newNode->next=NULL; | |
if(front == NULL) | |
rear = front = newNode; | |
else { | |
newNode->next = front; | |
free(front); | |
front = newNode; | |
} | |
rear->next=front; | |
} | |
void deleteBack() { | |
temp=front; | |
if(front == NULL) | |
cout << "\nUnderflow"; | |
else { | |
if(front == rear) { | |
cout << "\nData " << front->info << " sudah dikeluarkan"; | |
front = rear = NULL; | |
} else { | |
cout << "\nData " << front->info << " sudah dikeluarkan"; | |
front = front->next; | |
rear->next=front; | |
} | |
} | |
} | |
void deleteFront() { | |
temp=front; | |
if(front == NULL) | |
cout << "\nUnderflow"; | |
else { | |
if(front == rear) { | |
cout << "\nData " << front->info << " sudah dikeluarkan"; | |
front = rear = NULL; | |
} else { | |
while(temp->next != rear){ | |
temp = temp->next; | |
} | |
cout << "\nData " << rear->info << " sudah dikeluarkan"; | |
rear->next = NULL; | |
free(rear); | |
rear = temp; | |
rear->next = front; | |
} | |
} | |
} | |
void printCredit() { | |
cout << "--- Program Circular Linked List ---\n"; | |
cout << "--------- 03TPLM008 Kel 04 ---------\n"; | |
cout << "------------------------------------\n"; | |
} | |
void printNode() { | |
temp = front; | |
if(front == NULL) | |
cout << "\nEmpty"; | |
else { | |
cout << endl; | |
for(; temp != rear; temp = temp->next) | |
cout << temp->info << ", "; | |
if(temp == rear) | |
cout << rear->info << ""; | |
cout << endl; | |
} | |
} | |
int main() { | |
int chc; | |
printCredit(); | |
do { | |
cout << "\n1. Insert Depan\n" | |
<< "2. Insert Belakang\n" | |
<< "3. Hapus Depan\n" | |
<< "4. Hapus Belakang\n" | |
<< "5. Cetak Baris\n" | |
<< "6. Keluar\n" | |
<< "\nMasukkan pilihan : "; | |
cin >> chc; | |
switch(chc) { | |
case 1: | |
insertFront(); | |
break; | |
case 2: | |
insertBack(); | |
break; | |
case 3: | |
deleteFront(); | |
break; | |
case 4: | |
deleteBack(); | |
break; | |
case 5: | |
printNode(); | |
break; | |
case 6: | |
cout << "\nProgram Selesai\n"; | |
return 1; | |
default: | |
cout << "\nPilihan tidak tersedia" << endl; | |
break; | |
} | |
} while(1); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment