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
icon
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; 
                } 
            }
int count(int num)
This will return the count of the instances of the element num
in the list. In the linked list in removeAll method, having the
method count(10) will return 3 as there are three 10's in the
linked list.
Additionally, you are to implement these methods:
bool move(int pos1, int pos2)
This method will move the element at position pos1 to the
position specified as pos2 and will return true if the move is
successful. This will return false if either the specified
positions is invalid. In the example stated, operating the
method move(1, 3) will move the 1st element and shall now
become the 3rd element. The linked list shall now look like: 30
→ 40 + 10 + 50
• int removeTail()
This method will remove the last element in your linked list and
return the said element. If the list is empty, return 0. You can
use the existing methods.
• void isEmpty)
This method will return true if the linked list is empty,
otherwise return false.
• void clear()
This method will empty your linked list. Effectively, this should
and already has been called in your destructor (ie., the
-LinkedList() method) so that it will deallocate the nodes
created first before deallocating the linked list itself.
Transcribed Image Text:int count(int num) This will return the count of the instances of the element num in the list. In the linked list in removeAll method, having the method count(10) will return 3 as there are three 10's in the linked list. Additionally, you are to implement these methods: bool move(int pos1, int pos2) This method will move the element at position pos1 to the position specified as pos2 and will return true if the move is successful. This will return false if either the specified positions is invalid. In the example stated, operating the method move(1, 3) will move the 1st element and shall now become the 3rd element. The linked list shall now look like: 30 → 40 + 10 + 50 • int removeTail() This method will remove the last element in your linked list and return the said element. If the list is empty, return 0. You can use the existing methods. • void isEmpty) This method will return true if the linked list is empty, otherwise return false. • void clear() This method will empty your linked list. Effectively, this should and already has been called in your destructor (ie., the -LinkedList() method) so that it will deallocate the nodes created first before deallocating the linked list itself.
• int removeAt(int pos)
Removes the number in the posth position of the list and
returns the element removed.
Performing removeAt(3) in the example list will remove the 3rd
element of the linked list and the updated list will be: 10 → 30 →
50
When the value of pos is greater than the size or less than one,
return -1.
• int removeAll(int num)
Removes all instances of num in the linked list and returns the
number of instances removed.
In this list 10 → 10 → 20 → 30 → 10, performing removeAll(10)
will remove all three 10's and the list will look like this: 20 → 30.
Then, it will return the number of instances removed, in this
case, 3.
• int contains(int num)
This will return the position of the first instance of the element
num in the list. If num is not found, return 0. In the example,
having the method contains(30) will return 2 as it is located in
the second position.
Transcribed Image Text:• int removeAt(int pos) Removes the number in the posth position of the list and returns the element removed. Performing removeAt(3) in the example list will remove the 3rd element of the linked list and the updated list will be: 10 → 30 → 50 When the value of pos is greater than the size or less than one, return -1. • int removeAll(int num) Removes all instances of num in the linked list and returns the number of instances removed. In this list 10 → 10 → 20 → 30 → 10, performing removeAll(10) will remove all three 10's and the list will look like this: 20 → 30. Then, it will return the number of instances removed, in this case, 3. • int contains(int num) This will return the position of the first instance of the element num in the list. If num is not found, return 0. In the example, having the method contains(30) will return 2 as it is located in the second position.
Expert Solution
steps

Step by step

Solved in 4 steps with 5 images

Blurred answer
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
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)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education