Download as pdf or txt
Download as pdf or txt
You are on page 1of 10

NAMA:FADLAN UMAR ROZIKIN

NIM:235150307111032

KELAS:TEKNIK KOMPUTER C

1. Buatlah semua fungsi yang ada di dalam Doubly Linked List!


#include <iostream>

#include <cstdlib>

using namespace std;

struct Node {

int data;

Node* prev;

Node* next;

};

class DoublyLinkedList {

private:

Node* head;

Node* tail;

int count;

public:

/* inititate double linked list */

DoublyLinkedList() {

head = nullptr;

tail = nullptr;

count = 0;

/* check empety list */

bool is_empty() {

return head == nullptr;

/* get size of list */

int size() {

return count;

/* append data to last list */


void append(int data) {

Node* newNode = new Node();

newNode->data = data;

newNode->prev = nullptr;

newNode->next = nullptr;

if (is_empty()) {

head = newNode;

tail = newNode;

} else {

tail->next = newNode;

newNode->prev = tail;

tail = newNode;

count++;

/* delete data from list */

void remove(int data) {

Node* current = head;

while (current != nullptr) {

if (current->data == data) {

if (current == head) {

head = current->next;

if (head != nullptr)

head->prev = nullptr;

} else if (current == tail) {

tail = current->prev;

tail->next = nullptr;

} else {

current->prev->next = current->next;

current->next->prev = current->prev;

delete current;

count--;
return;

current = current->next;

/* insert After data */

void insertAfter(int afterData, int newData) {

Node* newNode = new Node();

newNode->data = newData;

newNode->prev = nullptr;

newNode->next = nullptr;

Node* current = head;

while (current != nullptr) {

if (current->data == afterData) {

newNode->next = current->next;

newNode->prev = current;

if (current->next != nullptr)

current->next->prev = newNode;

current->next = newNode;

if (newNode->next == nullptr)

tail = newNode;

count++;

return;

current = current->next;

/* get data from list */

bool search(int data) {

Node* current = head;

while (current != nullptr) {

if (current->data == data)
return true;

current = current->next;

return false;

/* accesing data */

int access(int index) {

if (index < 0 || index >= count)

return -1; // Return -1 for invalid index

Node* current = head;

for (int i = 0; i < index; i++)

current = current->next;

return current->data;

};

int main() {

DoublyLinkedList dll;

/* example from funtion Doubly Linked List */

dll.append(1);

dll.append(2);

dll.append(3);

dll.insertAfter(2, 4);

dll.remove(3);

cout << "Size in list: " << dll.size() <<endl;

cout << "Is the list empty? " << (dll.is_empty() ? "Yes" : "No") <<endl;

cout << "Is 4 present in the list? " << (dll.search(4) ? "Yes" : "No")<<endl;

cout << "Data at index 1: " << dll.access(1) <<endl;

return 0;

2.Buatlah semua fungsi yang ada di circular linked list

Jawab:
#include <iostream>

#include <cstdlib>

using namespace std;

class Node{

public:

int data;

Node* next;

Node* prev;

Node(){

data = 0;

next = NULL;

prev = NULL;

Node(int data){

this->data = data;

this->next = NULL;

this->prev = NULL;

};

class CircularLinkedList{

Node* head;

Node* tail;

int size = 0;

public:

CircularLinkedList(){

head = NULL;

tail = NULL;

bool isEmpty(){
if (head == NULL) {

return 1;

else {

return 0;

int getSize(){

return this->size;

void addFirst(int data) {

Node *baru = new Node;

baru->data = data;

baru->next = baru;

baru->prev = baru;

if (isEmpty() == 1) {

head = baru;

tail = baru;

head->next = head;

head->prev = head;

tail->next = tail;

tail->prev = tail;

} else {

baru->next = head;

head->prev = baru;

head = baru;

head->prev = tail;

tail->next = head;

cout << "Data masuk\n";

size++;
}

void addLast(int data) {

Node *baru = new Node;

baru->data = data;

baru->next = baru;

baru->prev = baru;

if (isEmpty() == 1) {

head = tail = baru;

head->next = head;

head->prev = head;

tail->next = tail;

tail->prev = tail;

} else {

tail->next = baru;

baru->prev = tail;

tail = baru;

tail->next = head;

head->prev = tail;

cout << "Data masuk\n";

size++;

void deleteFirst() {

Node* temp = new Node;

int d;

if (isEmpty() == 0) {

if (head != tail) {

temp = head;

d = temp->data;

head = head->next;

tail->next = head;
head->prev = tail;

delete temp;

} else {

d = head->data;

head = NULL;

tail = NULL;

cout << d << " terhapus\n";

size--;

} else {

cout << "Masih kosong\n";

void deleteLast() {

Node* temp = new Node;

int d;

if (isEmpty() == 0) {

if (head != tail) {

temp = tail;

d = temp->data;

tail = tail->prev;

tail->next = head;

head->prev = tail;

delete temp;

} else {

d = head->data;

head = NULL;

tail = NULL;

cout << d << " terhapus\n";

size--;

} else {
cout << "Masih kosong\n";

void clear() {

Node* bantu = new Node;

Node* temp = new Node;

if (isEmpty() == 0) {

bantu = head;

while (bantu->next != head) {

temp = bantu;

bantu = bantu->next;

delete temp;

head = NULL;

void printList() {

Node *bantu = head;

if (isEmpty() == 0) {

do {

cout << bantu->data << " ";

bantu = bantu->next;

} while (bantu != tail->next);

cout << endl;

} else {

cout << "Masih kosong\n";

};

int main(){

CircularLinkedList circularlist;
circularlist.addFirst(4);

circularlist.addFirst(1);

circularlist.addFirst(3);

circularlist.addLast(2);

circularlist.addLast(5);

circularlist.printList();

cout << "\nSize = " << circularlist.getSize() << endl;

circularlist.deleteFirst();

circularlist.deleteLast();

circularlist.printList();

cout << "\nSize = " << circularlist.getSize() << endl;

circularlist.clear();

cout << "\nSize = " << circularlist.getSize() << endl;

return 0;

You might also like