plain the flow of the code. See attached photo for the problem. (This is not graded) int removeAt(int pos) {
plain the flow of the code. See attached photo for the problem. (This is not graded) int removeAt(int pos) {
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
Explain the flow of the code. See attached photo for the problem. (This is not graded)
int removeAt(int pos) {
if (pos <= index){
int elem = 0;
if (pos == 1){
return removeHead();
}
node* currnode = head;
node* prevnode = NULL;
int count = 0;
while (currnode != NULL) {
count++;
if (count == pos) {
elem = currnode-> element;
prevnode->next = currnode->next;
if (currnode == tail) {
tail = prevnode;
}
free(currnode);
index--;
return elem;
}else {
prevnode = currnode;
currnode = currnode->next;
}
}
}
return -1;
}
int removeAll(int num) {
int count=0;
node* currnode = head;
node* prevnode = NULL;
while (currnode != NULL) {
if (currnode->element == num) {
count++;
if (prevnode != NULL) {
if (currnode == tail) {
tail = prevnode;
}
node* rem = currnode;
prevnode->next = currnode->next;
currnode = currnode->next;
free(rem);
index--;
} else {
node* rem = currnode;
currnode = rem->next;
removeHead();
}
} else {
prevnode = currnode;
currnode = currnode->next;
}
}
return count;
}
int contains(int num) {
for (int i=1; i <= index; i++){
if (get(i) == num){
return i;
}
}
return 0;
}
int count(int num) {
int count =0;
for (int i =1; i <= index; i++){
if (get(i) == num){
count++;
}
}
return count;
}
bool move(int pos1, int pos2) {
if ( pos1 <= index && pos2 <= index){
int elem = removeAt(pos1);
if(pos2 == 1){
addHead(elem);
}
else{
addAt(elem,pos2);
}
return true;
}
return false;
}
int removeTail() {
int elem=0;
node* currnode = head;
node* prevnode = NULL;
while (currnode != NULL) {
if (currnode == tail) {
elem = currnode-> element;
if (prevnode != NULL){
prevnode->next = currnode->next;
}
tail = prevnode;
if(tail == NULL){
head = NULL;
}
free(currnode);
index--;
return elem;
}else {
prevnode = currnode;
currnode = currnode->next;
}
}
return 0;
}
bool isEmpty() {
if (index == 0){
return true;
}
return false;
}
void clear() {
while(head != NULL){
node* temp = head->next;
removeHead();
head = temp;
}
}
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 5 images
Similar questions
- Recommended textbooks for youDatabase System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSONDatabase System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSONC How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag…Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education