NotesWhat is notes.io?

Notes brand slogan

Notes - notes.io

#include <iostream>
using namespace std;

class Node {
public:
string data;
Node* next;

Node(const string& data) {
this->data = data;
this->next = NULL;
}
};

class CircularLinkedList {
private:
Node* head;

public:
CircularLinkedList() {
head = NULL;
}

void append(const string& data) {
Node* newNode = new Node(data);
if (!head) {
head = newNode;
head->next = head;
} else {
Node* current = head;
while (current->next != head) {
current = current->next;
}
current->next = newNode;
newNode->next = head;
}
}

void display() {
if (!head) {
cout << "Dokumen kosong" << endl;
return;
}
Node* current = head;
do {
cout << current->data << " ";
current = current->next;
} while (current != head);
cout << endl;
}

void insert(const string& data, int position) {
if (position < 0) {
cout << "Posisi tidak valid" << endl;
return;
}
Node* newNode = new Node(data);
if (position == 0) {
newNode->next = head;
Node* current = head;
while (current->next != head) {
current = current->next;
}
current->next = newNode;
head = newNode;
} else {
Node* current = head;
int count = 0;
while (count < position - 1 && current->next != head) {
current = current->next;
count++;
}
if (count != position - 1) {
cout << "Posisi tidak valid" << endl;
return;
}
newNode->next = current->next;
current->next = newNode;
}
}

void remove(int position) {
if (!head) {
cout << "Dokumen kosong" << endl;
return;
}

if (position < 0) {
cout << "Posisi tidak valid" << endl;
return;
}

if (position == 0) {
Node* current = head;
while (current->next != head) {
current = current->next;
}
if (head == head->next) {
delete head;
head = NULL;
} else {
Node* temp = head;
head = head->next;
current->next = head;
delete temp;
}
return;
}

Node* current = head;
int count = 0;
while (count < position - 1 && current->next != head) {
current = current->next;
count++;
}

if (count != position - 1 || !current->next) {
cout << "Posisi tidak valid" << endl;
return;
}

Node* temp = current->next;
current->next = temp->next;
delete temp;
}

void sort() {
if (!head) {
cout << "Dokumen kosong" << endl;
return;
}

Node* current = head;
do {
Node* temp = current->next;
do {
if (current->data > temp->data) {
swap(current->data, temp->data);
}
temp = temp->next;
} while (temp != head);
current = current->next;
} while (current->next != head);
}

string findMin() {
if (!head) {
cout << "Dokumen kosong" << endl;
return "";
}

Node* current = head;
string minVal = current->data;

do {
if (current->data < minVal) {
minVal = current->data;
}
current = current->next;
} while (current != head);

return minVal;
}

string findMax() {
if (!head) {
cout << "Dokumen kosong" << endl;
return "";
}

Node* current = head;
string maxVal = current->data;

do {
if (current->data > maxVal) {
maxVal = current->data;
}
current = current->next;
} while (current != head);

return maxVal;
}
};

int main() {
CircularLinkedList doc;

while (true) {
cout << "nMenu:" << endl;
cout << "1. Tampilkan Dokumen" << endl;
cout << "2. Tambahkan Teks" << endl;
cout << "3. Sisipkan Teks" << endl;
cout << "4. Hapus Teks" << endl;
cout << "5. Urutkan Dokumen" << endl;
cout << "6. Teks Minimum" << endl;
cout << "7. Teks Maksimum" << endl;
cout << "8. Keluar" << endl;

int choice;
cin >> choice;

cin.ignore(); // Menghapus karakter newline dari buffer input

switch (choice) {
case 1:
cout << "Isi Dokumen: ";
doc.display();
break;
case 2: {
string text;
cout << "Masukkan teks yang ingin ditambahkan: ";
getline(cin, text);
doc.append(text);
break;
}
case 3: {
string text;
int position;
cout << "Masukkan teks yang ingin disisipkan: ";
getline(cin, text);
cout << "Masukkan posisi sisipan: ";
cin >> position;
doc.insert(text, position);
break;
}
case 4: {
int position;
cout << "Masukkan posisi teks yang ingin dihapus: ";
cin >> position;
doc.remove(position);
break;
}
case 5:
doc.sort();
cout << "Dokumen telah diurutkan." << endl;
break;
case 6: {
string minVal = doc.findMin();
cout << "Teks Minimum: " << minVal << endl;
break;
}
case 7: {
string maxVal = doc.findMax();
cout << "Teks Maksimum: " << maxVal << endl;
break;
}
case 8:
return 0;
default:
cout << "Opsi tidak valid" << endl;
}
}

return 0;
}
     
 
what is notes.io
 

Notes.io is a web-based application for taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000 notes created and continuing...

With notes.io;

  • * You can take a note from anywhere and any device with internet connection.
  • * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
  • * You can quickly share your contents without website, blog and e-mail.
  • * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
  • * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.

Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.

Easy: Notes.io doesn’t require installation. Just write and share note!

Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )

Free: Notes.io works for 12 years and has been free since the day it was started.


You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;


Email: [email protected]

Twitter: http://twitter.com/notesio

Instagram: http://instagram.com/notes.io

Facebook: http://facebook.com/notesio



Regards;
Notes.io Team

     
 
Shortened Note Link
 
 
Looding Image
 
     
 
Long File
 
 

For written notes was greater than 18KB Unable to shorten.

To be smaller than 18KB, please organize your notes, or sign in.