Question

In: Computer Science

Write a program to remove the node of Fruit type which contains “Banana” from a given...

Write a program to remove the node of Fruit type which contains “Banana” from a given linked list and then print the updated linked list. Part of the program is given below but it is incomplete. You need to complete it. You should create your own data file. After you complete the program, please check it carefully to ensure it can handle the following cases:

Banana is the first node in the linked list.

Banana is not the first node in the linked list.

Banana doesn’t exist in the linked list.

Note that head should points to the first node of the linked list even after you remove the node which contains “banana”.

#include<iostream>

#include<string>

#include<fstream>

using namespace std;

struct Fruit{

string strF;

Fruit* next;

};

int main()

{

       ifstream myfile;

       myfile.open("test.txt");

       Fruit * head = 0;

      

       Fruit f;

       myfile >> f.strF;

       f.next = head;

       head = &f;

       Fruit f1;

       myfile >> f1.strF;

       f1.next = head;

       head = &f1;

       Fruit f2;

       myfile >> f2.strF;

       f2.next = head;

       head = &f2;

       //print the linked list:

       cout<<"Print the linked list before the insertion:"<<endl;

       Fruit * t = head;

       while(t!= 0)

       {

             cout << t->strF << "--->"<<endl;

             t = t->next;

       }

       cout<<endl;

       //Write your code here to delete the node which has "banana"

     

       cout<<"Print the linked list after the insertion:"<<endl;

  //print the updated linked list


       return 0;

}

Solutions

Expert Solution

#include<iostream>
#include<string>
#include<fstream>
using namespace std;
struct Fruit {
   string strF;
   Fruit* next;
};
bool check(Fruit* a)
{
   if (a->strF == "Banana")
   {
       return true;
   }
   return false;
}
int main()
{
   ifstream myfile;
   myfile.open("file1.txt");
   Fruit * head = 0;
   Fruit f;
   myfile >> f.strF;
   f.next = head;
   head = &f;
   Fruit f1;
   myfile >> f1.strF;
   f1.next = head;
   head = &f1;
   Fruit f2;
   myfile >> f2.strF;
   f2.next = head;
   head = &f2;
   //print the linked list:
   cout << "Print the linked list before the insertion:" << endl<<endl;
   Fruit * t = head;
   int count = 0;
   while (t != 0)
   {
       if (count == 0)
           cout << t->strF;
       else
           cout << "--->" << t->strF;
       t = t->next;
       count++;
   }
   cout << endl << endl;;
   //Write your code here to delete the node which has "banana"
   cout << "Print the linked list after the insertion:" << endl<<endl;
   t = head;
   Fruit* temp = head;
   string notb;
   notb = t->strF;
   if (check(t))
   {
       head = head->next;
       t = head;
       temp = head;
   }
   t = t->next;
   while(t!=0)
   {
       if (check(t))
       {
           temp->next = t->next;
           t = t->next;
       }
       temp = temp->next;
       t = t->next;

   }
   t = head;
   count = 0;
   while (t != 0)
   {
       if(count==0)
       cout << t->strF;
       else
       cout<<"--->" << t->strF;
       t = t->next;
       count++;
   }
   cout << endl<<endl;
   //print the updated linked list
   system("pause");
   return 0;

}

Comment Down For Any Queries
Please Give A Thumbs Up If You Are Satisfied With The Answer


Related Solutions

Write a JAVA program which prints the root node after the "remove" procedure the the splay...
Write a JAVA program which prints the root node after the "remove" procedure the the splay tree.
Write a program to remove an element from an array at the given position k and...
Write a program to remove an element from an array at the given position k and push the rest of the array elements one position back. Then insert the removed element at the beginning. Position k is entered through keyboard. For example, if the original array x is {'r', 'c', 'm', '7', 'w', '3', 'q'} and k = 3, the array will be changed to {'7', 'r', 'c', 'm', 'w', '3', 'q'}. Hint: Sequence of moving the element is important....
How do I remove a node from a linked list C++? void LinkedList::Remove(int offset){ shared_ptr<node> cursor(top_ptr_);...
How do I remove a node from a linked list C++? void LinkedList::Remove(int offset){ shared_ptr<node> cursor(top_ptr_); shared_ptr<node> temp(new node); if(cursor == NULL) { temp = cursor-> next; cursor= temp; if (temp = NULL) { temp->next = NULL; } } else if (cursor-> next != NULL) { temp = cursor->next->next; cursor-> next = temp; if (temp != NULL) { temp->next = cursor; } } }
Write a Java Program that can:​ Remove a particular element from an array.​ Add a new...
Write a Java Program that can:​ Remove a particular element from an array.​ Add a new element to an array.​ Change an element with the new one.​ Search for a particular element in the array.​ ​The code must have four separate methods for each task stated above.​ Do not use any pre-defined Java functions.​ You are free to use int or String data-type for the array.​
Write a program to remove extra blanks from text files. Your program should replace any string...
Write a program to remove extra blanks from text files. Your program should replace any string of two or more blanks with one single blank. It should work as follows: * Create a temporary file. * Copy from the input file to the temporary file, but do not copy extra blanks. * Copy the contents of the temporary file back into the original file. * Remove the temporary file. Your temporary file must have a different name than any existing...
Implement a function to remove a leaf node from a binary search tree. Using the following...
Implement a function to remove a leaf node from a binary search tree. Using the following class and function definition: class BTNode { public: int item; BTNode *left; BTNode *right; BTNode(int i, BTNode *l=nullptr, BTNode *r=nullptr):item(i),left(l),right(r){} }; BTNode *root = nullptr; BTNode * remove_leaf(int item, bool &removed) { // implement } The remove function will return the node with the item if it's present in the tree. If the node is a leaf node and is removed by the function,...
Write a program whose input is a string which contains a character and a phrase, and...
Write a program whose input is a string which contains a character and a phrase, and whose output indicates the number of times the character appears in the phrase. Ex: If the input is: n Monday the output is: 1 Ex: If the input is: z Today is Monday the output is: 0 Ex: If the input is: n It's a sunny day the output is: 2 Case matters. Ex: If the input is: n Nobody the output is: 0...
Remove the Head element from the code below: public class LinkedList {    class Node{ int...
Remove the Head element from the code below: public class LinkedList {    class Node{ int value; Node nextElement; public Node(int value) { this.value = value; this.nextElement = null; } } public Node first = null; public Node last = null; public void addNewNode(int element) { Node newValueNode = new Node(element);    if(first == null) { first = newValueNode; } else { last.nextElement = newValueNode; } last = newValueNode; } public void displayValues() { Node recent = first; if(first ==...
Insert: this method takes a value as a parameter and adds a node which contains the...
Insert: this method takes a value as a parameter and adds a node which contains the value to the end of the linked list Delete: This method deletes a node from the linked list. If an index is passed as a parameter, then the method should delete the node at this index. If no index is passed, then delete the first item in the list Find: this method takes a value as a parameter, and returns the index of the...
Write a Java program that uses the file EnglishWordList.txt which contains a collection of words in...
Write a Java program that uses the file EnglishWordList.txt which contains a collection of words in English (dictionary), to find the number of misspelled words in a story file called Alcott-little-261.txt. Please note that the dictionary words are all lower-cased and there are no punctuation symbols or numbers. Hence it is important that you eliminate anything other than a-z and A-Z and then lower case story words for valid comparisons against the WordList file. When you read each line of...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT