Fadlan Umar Rozikin-235150307111032-Teknik Komputer C - Asd Tugas 2
Fadlan Umar Rozikin-235150307111032-Teknik Komputer C - Asd Tugas 2
Fadlan Umar Rozikin-235150307111032-Teknik Komputer C - Asd Tugas 2
NIM:235150307111032
KELAS:TEKNIK KOMPUTER C
#include <cstdlib>
struct Node {
int data;
Node* prev;
Node* next;
};
class DoublyLinkedList {
private:
Node* head;
Node* tail;
int count;
public:
DoublyLinkedList() {
head = nullptr;
tail = nullptr;
count = 0;
bool is_empty() {
int size() {
return count;
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++;
if (current->data == data) {
if (current == head) {
head = current->next;
if (head != nullptr)
head->prev = nullptr;
tail = current->prev;
tail->next = nullptr;
} else {
current->prev->next = current->next;
current->next->prev = current->prev;
delete current;
count--;
return;
current = current->next;
newNode->data = newData;
newNode->prev = nullptr;
newNode->next = 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;
if (current->data == data)
return true;
current = current->next;
return false;
/* accesing data */
current = current->next;
return current->data;
};
int main() {
DoublyLinkedList dll;
dll.append(1);
dll.append(2);
dll.append(3);
dll.insertAfter(2, 4);
dll.remove(3);
cout << "Is the list empty? " << (dll.is_empty() ? "Yes" : "No") <<endl;
cout << "Is 4 present in the list? " << (dll.search(4) ? "Yes" : "No")<<endl;
return 0;
Jawab:
#include <iostream>
#include <cstdlib>
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;
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;
size++;
}
baru->data = data;
baru->next = baru;
baru->prev = baru;
if (isEmpty() == 1) {
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;
size++;
void deleteFirst() {
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;
size--;
} else {
void deleteLast() {
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;
size--;
} else {
cout << "Masih kosong\n";
void clear() {
if (isEmpty() == 0) {
bantu = head;
temp = bantu;
bantu = bantu->next;
delete temp;
head = NULL;
void printList() {
if (isEmpty() == 0) {
do {
bantu = bantu->next;
} else {
};
int main(){
CircularLinkedList circularlist;
circularlist.addFirst(4);
circularlist.addFirst(1);
circularlist.addFirst(3);
circularlist.addLast(2);
circularlist.addLast(5);
circularlist.printList();
circularlist.deleteFirst();
circularlist.deleteLast();
circularlist.printList();
circularlist.clear();
return 0;