Explain the code (Do not use flowchart). See attached photo for the problem. It doesn't have to be long as long as you explain the important parts of the code int removeHead() { if(!isEmpty()) { return remove_between(head->next); } return 0; } int removeTail() { if(!isEmpty()) { return remove_between(tail->prev); } return 0; } int add(int num) { addTail(num); return index; } int remove(int num) { node* currnode = head->next; int pos = 0; while(currnode != tail) { pos++; if(currnode->element == num) { remove_between(currnode); return pos; } else { currnode = currnode->next; } } return 0; } bool addAt(int num, int pos) { node* currnode; int count; bool start_from_head = true; if (pos > index/2) { start_from_head = false; } if (start_from_head) { // START FROM HEAD currnode = head->next; count = 0; while (currnode != NULL) { count++; if (count == pos) { add_between(num, currnode->prev, currnode); return true; } else { currnode = currnode->next; } } } else { // START FROM TAIL currnode = tail->prev; count = index + 1; while (currnode != NULL) { count--; if (count == pos - 1) { add_between(num, currnode, currnode->next); return true; } else { currnode = currnode->prev; } } } return false; } int removeAt(int pos) { node* currnode; int count; bool start_from_head = true; if (pos > index/2) { start_from_head = false; } if (start_from_head) { // START FROM HEAD currnode = head->next; count = 0; while (currnode != NULL) { count++; if (count == pos) { int elem = currnode->element; remove_between(currnode); return elem; } else { currnode = currnode->next; } } } else { // START FROM TAIL currnode = tail->prev; count = index + 1; while (currnode != NULL) { count--; if (count == pos) { int elem = currnode->element; remove_between(currnode); return elem; } else { currnode = currnode->prev; } } } return -1; } int removeAll(int num) { node* currnode = head->next; int count = 0; while(currnode != tail) { if(currnode->element == num) { count++; node* newcurrnode = currnode->next; remove_between(currnode); currnode = newcurrnode; } else { currnode = currnode->next; } } return count; } int contains(int num) { node* currnode = head->next; int pos = 0; while(currnode != NULL) { pos++; if(currnode->element == num) { return pos; } else { currnode = currnode->next; } } return 0; } int count(int num) { node* currnode = head->next; int ctr = 0; while(currnode != NULL) { if(currnode->element == num) { ctr++; } currnode = currnode->next; } return ctr; } bool move(int pos1, int pos2) { int elem = removeAt(pos1); if(elem == -1) { return false; } bool success = addAt(elem, pos2); if(!success) { addAt(elem, pos1); } return success; } bool isEmpty() { return index == 0; } void clear() { while(!isEmpty()){ removeHead(); } }
Explain the code (Do not use flowchart). See attached photo for the problem. It doesn't have to be long as long as you explain the important parts of the code int removeHead() { if(!isEmpty()) { return remove_between(head->next); } return 0; } int removeTail() { if(!isEmpty()) { return remove_between(tail->prev); } return 0; } int add(int num) { addTail(num); return index; } int remove(int num) { node* currnode = head->next; int pos = 0; while(currnode != tail) { pos++; if(currnode->element == num) { remove_between(currnode); return pos; } else { currnode = currnode->next; } } return 0; } bool addAt(int num, int pos) { node* currnode; int count; bool start_from_head = true; if (pos > index/2) { start_from_head = false; } if (start_from_head) { // START FROM HEAD currnode = head->next; count = 0; while (currnode != NULL) { count++; if (count == pos) { add_between(num, currnode->prev, currnode); return true; } else { currnode = currnode->next; } } } else { // START FROM TAIL currnode = tail->prev; count = index + 1; while (currnode != NULL) { count--; if (count == pos - 1) { add_between(num, currnode, currnode->next); return true; } else { currnode = currnode->prev; } } } return false; } int removeAt(int pos) { node* currnode; int count; bool start_from_head = true; if (pos > index/2) { start_from_head = false; } if (start_from_head) { // START FROM HEAD currnode = head->next; count = 0; while (currnode != NULL) { count++; if (count == pos) { int elem = currnode->element; remove_between(currnode); return elem; } else { currnode = currnode->next; } } } else { // START FROM TAIL currnode = tail->prev; count = index + 1; while (currnode != NULL) { count--; if (count == pos) { int elem = currnode->element; remove_between(currnode); return elem; } else { currnode = currnode->prev; } } } return -1; } int removeAll(int num) { node* currnode = head->next; int count = 0; while(currnode != tail) { if(currnode->element == num) { count++; node* newcurrnode = currnode->next; remove_between(currnode); currnode = newcurrnode; } else { currnode = currnode->next; } } return count; } int contains(int num) { node* currnode = head->next; int pos = 0; while(currnode != NULL) { pos++; if(currnode->element == num) { return pos; } else { currnode = currnode->next; } } return 0; } int count(int num) { node* currnode = head->next; int ctr = 0; while(currnode != NULL) { if(currnode->element == num) { ctr++; } currnode = currnode->next; } return ctr; } bool move(int pos1, int pos2) { int elem = removeAt(pos1); if(elem == -1) { return false; } bool success = addAt(elem, pos2); if(!success) { addAt(elem, pos1); } return success; } bool isEmpty() { return index == 0; } void clear() { while(!isEmpty()){ removeHead(); } }
Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
Related questions
Question
100%
Explain the code (Do not use flowchart). See attached photo for the problem. It doesn't have to be long as long as you explain the important parts of the code
int removeHead() {
if(!isEmpty()) {
return remove_between(head->next);
}
return 0;
}
int removeTail() {
if(!isEmpty()) {
return remove_between(tail->prev);
}
return 0;
}
int add(int num) {
addTail(num);
return index;
}
int remove(int num) {
node* currnode = head->next;
int pos = 0;
while(currnode != tail) {
pos++;
if(currnode->element == num) {
remove_between(currnode);
return pos;
} else {
currnode = currnode->next;
}
}
return 0;
}
bool addAt(int num, int pos) {
node* currnode;
int count;
bool start_from_head = true;
if (pos > index/2) {
start_from_head = false;
}
if (start_from_head) {
// START FROM HEAD
currnode = head->next;
count = 0;
while (currnode != NULL) {
count++;
if (count == pos) {
add_between(num, currnode->prev, currnode);
return true;
} else {
currnode = currnode->next;
}
}
} else {
// START FROM TAIL
currnode = tail->prev;
count = index + 1;
while (currnode != NULL) {
count--;
if (count == pos - 1) {
add_between(num, currnode, currnode->next);
return true;
} else {
currnode = currnode->prev;
}
}
}
return false;
}
int removeAt(int pos) {
node* currnode;
int count;
bool start_from_head = true;
if (pos > index/2) {
start_from_head = false;
}
if (start_from_head) {
// START FROM HEAD
currnode = head->next;
count = 0;
while (currnode != NULL) {
count++;
if (count == pos) {
int elem = currnode->element;
remove_between(currnode);
return elem;
} else {
currnode = currnode->next;
}
}
} else {
// START FROM TAIL
currnode = tail->prev;
count = index + 1;
while (currnode != NULL) {
count--;
if (count == pos) {
int elem = currnode->element;
remove_between(currnode);
return elem;
} else {
currnode = currnode->prev;
}
}
}
return -1;
}
int removeAll(int num) {
node* currnode = head->next;
int count = 0;
while(currnode != tail) {
if(currnode->element == num) {
count++;
node* newcurrnode = currnode->next;
remove_between(currnode);
currnode = newcurrnode;
} else {
currnode = currnode->next;
}
}
return count;
}
int contains(int num) {
node* currnode = head->next;
int pos = 0;
while(currnode != NULL) {
pos++;
if(currnode->element == num) {
return pos;
} else {
currnode = currnode->next;
}
}
return 0;
}
int count(int num) {
node* currnode = head->next;
int ctr = 0;
while(currnode != NULL) {
if(currnode->element == num) {
ctr++;
}
currnode = currnode->next;
}
return ctr;
}
bool move(int pos1, int pos2) {
int elem = removeAt(pos1);
if(elem == -1) {
return false;
}
bool success = addAt(elem, pos2);
if(!success) {
addAt(elem, pos1);
}
return success;
}
bool isEmpty() {
return index == 0;
}
void clear() {
while(!isEmpty()){
removeHead();
}
}
Expert Solution
This question has been solved!
Explore an expertly crafted, step-by-step solution for a thorough understanding of key concepts.
Step by step
Solved in 4 steps with 4 images
Recommended textbooks for you
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education