Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Languge c++ Hi I need good explanation (Comments for all sections ) for this programe here: Circular linked list #include // for cout #include //

Languge c++ Hi I need good explanation (Comments for all sections ) for this programe here: Circular linked list image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

#include // for cout

#include // for vector (exercise 1f)

#include // for runtime_error

#include

#include

struct Node {

int value;

Node* next;

Node* previous;

Node(int n) {

value = n;

next = nullptr;

previous = nullptr;

}

Node(int n, Node* p) {

value = n;

next = p;

}

};

class CircLinkedList{

private:

Node* head;

Node* tail;

int size = 0;

public:

CircLinkedList() {

head = nullptr;

tail = nullptr;

}

CircLinkedList(int n){

head = nullptr;

tail = nullptr;

int i = 1;

while (i

append(i);

i++;

}

}

void append(int val) {

Node* ny_node = new Node(val);

size ++;

if (head==nullptr){

head = ny_node;

ny_node->next = ny_node;

ny_node = tail;

}

tail->next = ny_node;

ny_node->next = head;

ny_node = tail;

}

int& operator[](int index){

if (index

throw std::out_of_range("IndexError: Indeks utenfor range");

}

Node* current = head;

for (int i=0; i

current = current->next;

}

return current->value;

}

void print() {

Node* current = head;

std::cout

while (current->next != head) {

std::cout value;

std::cout

current = current->next;

}

std::cout value

}

~CircLinkedList() {

Node* current;

Node* next;

current = head;

while (current != head) {

next = current->next;

delete current;

current = next;

}

}

std::vector josephus_sequence(int k){

std::vector x{};

Node* previous = tail;

Node* current = head;

while(current->next !=current){

for (int j=1; j

previous = current;

current = current->next;

}

x.push_back(current->value);

previous->next = current->next;

delete current;

current = previous->next;

size--;

}

x.push_back(current->value);

delete current;

return x;

}

};

int last_man_standing(int n, int k){

CircLinkedList a{n};

std::vector x = a.josephus_sequence(k);

return x[n-1];

};

int main(){

CircLinkedList clist;

clist.append(0);

clist.append(2);

clist.append(3);

clist.append(4);

clist.append(5);

clist.print();

std::cout

return 0;

}

\( \begin{array}{ll}119 & \\ 120 & \text { int main() }\{ \\ 121 & \text { CircLinkedList clist; } \\ 122 & \text { clist.append }(0) ; \\ 123 & \text { clist.append }(2) ; \\ 124 & \text { clist.append }(3) ; \\ 125 & \text { clist.append }(4) ; \\ 126 & \text { clist.append(5); } \\ 127 & \text { clist.print }() ; \\ 128 & \text { std: cout

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Fundamentals Of Database System

Authors: Elmasri Ramez And Navathe Shamkant

7th Edition

978-9332582705

More Books

Students also viewed these Databases questions

Question

Describe five career management practices

Answered: 1 week ago