Question

I don't know how to do the clear function, which mean delete the nodes between n1 and n2

# include<ios-ream> using namespace sta; General instructions: Please modify this file and submit the modified one via svn an/Task 3: Implement the following function for deleting the nodes in between Node nl and Node n2. input Nodes nl a pointer to15); head add (head, = // at this point, the list is: HEAD->15->12->9->8->7->->3->2->1-NULL clear (head, p7) // between p7 an

# include using namespace sta; General instructions: Please modify this file and submit the modified one via svn and then websubmission (under the entry "pracExam"). No design documents are needed. struct Node int data; Node* next; /Task 1: Implement the following function for adding a front of the given list. new node to the input Node head: a head pointer to a list int val: an integer that represent the value of the new node return the head pointer of the new list after the insertion Example add a node of value 9 before HEAD-1->2->3-NULL after HEAD->9->1->2-3-NULI Nodeadd (Node* head, int val) I / Task 2 Implement the following function for printing the elements in odd position of the given list. Assume the head node is defined to be in position 1. input Node head: a head pointer to a list return void output The elements in odd position of the given 1ist HEAD->1->2-3-9-12-NULL Example: input output HEAD-1->NA-3-NA->12->NULL void printodd (Node head)
/Task 3: Implement the following function for deleting the nodes in between Node nl and Node n2. input Nodes nl a pointer to a node in a 1ist N ae3" n2: a pointer to a node in the зате її3t return void Example: input l points to the 1st node n2 points to the 4th node before HEAD-1->2->3->9-12-NULI after HEAD->1->9-12-NULT void clear (Node nl, Noden2) // You are not supposed to change the main function int main) Node* head = NULL; Node *p4, p7; int test cin >> test; for (int i 1; i 9-8-7->4->3->2-1-NULL //You can uncomment this line to test. //the output should be HEAD->9-NA--NA->3->NA-1->NULL //Please remember to comment this line out before submitting //printodd (head); head add (head, 12)
15); head add (head, = // at this point, the list is: HEAD->15->12->9->8->7->->3->2->1-NULL clear (head, p7) // between p7 and head, there are 3 nodes (12, 9 and 3). Remove them all. // The resulting 1ist is HEAD-15->7-4-3-2-1-ULL printodd (head) /7 the output should be: HEAD->15-»NA->4-»NA->2->NA-NULL // free all nodes Node* tmp; while (head) tmp head; head head->next ; delete tmp; return 0;
0 0
Add a comment Improve this question Transcribed image text
Answer #1

This is the code to perform the clear() function:

void clearList (Node *nl, Node n2) 34 Node traverse nl->next; 35 while (traversen2) 36 n1->next traverse->next ; = 37 travers

I will explain how it works now

suppose we have the linked list as 9->8->7->6->5->4->3->2->1, and we need to delete all elements between nodes that point to 9 and 3:

let us see how we delete a node from a linked list:

  • a->b->c <---- initial list
  • we connect a to c and remove b;
  • suppose ponter1 points to a and pointer2 points to b, then we do pointer1->next = pointer2->next; which removes the node b.

this is what we keep doing from n1 to n2.

9->8->7->6->5->4->3->2->1 <--------- initial list
9->7->6->5->4->3->2->1 <--------- deleted 8
9->6->5->4->3->2->1 <--------- deleted 7
9->5->4->3->2->1 <---------deleted 6
9->4->3->2->1 <--------- deleted 5
9->3->2->1 < ------ deleted 4 to get final list

Since you have not provides the test code i have only provided the function you needed. For any further assistance, please comment and if the answer helped please upvote

Add a comment
Know the answer?
Add Answer to:
I don't know how to do the clear function, which mean delete the nodes between n1 and n2 # include<ios-ream>...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • C++ 1. Please use attached script for your reference to create the node structure and a...

    C++ 1. Please use attached script for your reference to create the node structure and a linked list class, say LinkedList, that has the following basic member methods:     constructor, destructor//IMPORTANT, display(), add_node(). 2. Please implement the following additional member methods:     Please feel free to change T with any data type you'd like to use for your node stricture's data type. -- addFirst(T data) // Adds an node with data at the beginning of the list -- pop() //...

  • Your task is to complete the following function/functions: 1. Given a position in the linked list,...

    Your task is to complete the following function/functions: 1. Given a position in the linked list, delete the node at that position.(Silver problem - Mandatory ) 2. Print the sum of all negative elements in the linked list.(Gold problem) If you want, you can refer to the the previous recitation manual (which was on Linked Lists) to implement node deletion. #include <iostream> using namespace std; //----------- Define Node --------------------------------------------------- struct Node{ int key; Node *next; }; //----------- Define Linked List...

  • BELOW IS THE CODE I ALREADY HAVE Linked List - Delete Modify Lab 5 and include...

    BELOW IS THE CODE I ALREADY HAVE Linked List - Delete Modify Lab 5 and include the method to delete a node from the Linked List. In summary: 1) Add the method Delete 2) Method call to delete, with a number that IS in the list 3) Method call to delete, with a number that is NOT in the list - Be sure to include comments - Use meaningful identifier names (constants where appropriate) import java.io.*; 1/ Java program to...

  • Enum {FALSE=0, TRUE}; #define MAXBUFF 1024 #define SMALLBUFF 10 /* The LinkNode type is used as a...

    enum {FALSE=0, TRUE}; #define MAXBUFF 1024 #define SMALLBUFF 10 /* The LinkNode type is used as an element of a linked list ** implementation of a stack of tree nodes. */ typedef struct link_t LinkNode; typedef LinkNode *LinkNodePtr; /* The TreeNode type is used as an element of a binary "parse" tree. ** Tree nodes are created while parsing the RPN expression and ** stored on a stack until it's time to place them. */ typedef struct tree_t TreeNode; typedef...

  • need this updated so it will delete the list and then recreate it again /***********************************************************/ /*...

    need this updated so it will delete the list and then recreate it again /***********************************************************/ /* Header files. */ /***********************************************************/ #include <stdio.h> #include <stdlib.h> #include <string.h> /***********************************************************/ /* Structure definitions. */ /***********************************************************/ struct node { int data; struct node *right; struct node *left; }; struct trash { struct node *node; struct trash *next; }; /****************************************/ /* BUILD_LIST. */ /****************************************/ void BUILD_LIST(int number2Add, struct node *(*head), struct node *(*tail)) { int i; struct node *previous, *current; *head = NULL; *tail =...

  • could somone please help me to complete this ! public class MyLinkedList<AnyType> { private Node<AnyType> head,...

    could somone please help me to complete this ! public class MyLinkedList<AnyType> { private Node<AnyType> head, tail; private int size; public MyLinkedList() { this.head = null; this.tail = null; this.size = 0; } //1.Insert a node at the end of the list public void insert(AnyType data) { Node<AnyType> newNode = new Node(); newNode.data = data; if (head == null) { head = newNode; tail = newNode; head.next = null; tail.next = null; } else { tail.next = newNode; tail =...

  • In def c++ IDE correct mistake and screenshot output ------------------------------------------------------------------------------ #define NULL 0 #include <iostream> /*...

    In def c++ IDE correct mistake and screenshot output ------------------------------------------------------------------------------ #define NULL 0 #include <iostream> /* Name: Rabia Saad Al-wethnani ID: 43503535 Section: 2486 */ using namespace std; struct Node { int item; //data Node *next; //pointer to the next node in the list }; Node* createNode(int); int isPresent(Node*, int); void appendNode(Node*&, int); void displayList(Node*); void printLists(Node*, Node*); void swapNodes(Node* &head, int s); void findIntersection(Node* first, Node* P); void findUnion(Node* first, Node* P); int main() { Node* L = NULL,...

  • Modify the following code in order for it to perform the following operations: *This code is...

    Modify the following code in order for it to perform the following operations: *This code is meant to simulate Floyd's cycle finding algorithm* A) Start with two pointers at the head of the list. We'll call the first one tortoise and the second one hare B) Advance hare by two nodes. If this is not possible because of a null pointer, we have found the end of the list, and therefore the list is acyclic C) Advance tortoise by one...

  • I have a C++ code that lets me enter, display and delete a student record. I...

    I have a C++ code that lets me enter, display and delete a student record. I need to implement a function that prints the average grade score of the students I input. Below is my code and a picture of how my code looks right now. #include<iostream> #include<stdlib.h> using namespace std; //Node Declaration struct node {    string name;    string id;    int score;    node *next;   }; //List class class list {        private:        //head...

  • Q1: You can find a file that defines the CircularlyLinked List class similar to what we...

    Q1: You can find a file that defines the CircularlyLinked List class similar to what we discussed in the class. Download the file and work on it. Your task is to: 1. Complete the missing methods in the file as discussed in the class. Search for the comment/" MISSING / in the file to see the methods that need to be completed. 2. Add the following methods to the class a. public Node getMin 1. Task: find the node with...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT