Question

Please rewrite this function using recursive function #include using namespace std; struct Node { char ch;...

Please rewrite this function using recursive function

#include using

namespace std;

struct Node { char ch; Node* next; };

class LinkedList {

Node* head;

public:

LinkedList();

~LinkedList();

void add(char ch);

bool find(char ch);

bool del(char ch);

friend std::ostream& operator<<(std::ostream& out, LinkedList& list);

};

LinkedList::LinkedList() {

head = NULL;

}

LinkedList::~LinkedList() {

Node* cur = head, * tmp;

while (cur != NULL) {

tmp = cur->next; delete cur; cur = tmp;

}

}

void LinkedList::add(char ch) {

Node* cur = head, * newNode;

newNode = new Node;

newNode->ch = ch; newNode->next = NULL;

if (head == NULL) {

head = newNode;

} else {

while (cur->next != NULL)

cur = cur->next;

cur->next = newNode;

} }

bool LinkedList::find(char ch) {

Node* cur = head;

bool found = false;

while (cur != NULL) {

if (cur->ch == ch) {

found = true; break;

} cur = cur->next;

} return found;

}

bool LinkedList::del(char ch) {

Node* cur = head, * prev, * next = NULL, * tmp; int found = 0;

if (head->ch == ch) {

tmp = head;

head = head->next;

delete tmp;

return true;

} prev = cur;

while (cur->next != NULL) {

prev = cur;

next = cur->next->next;

if (cur->next->ch == ch) {

found = 1;

break;

}

cur = cur->next;

} if (!found)

{ cout << "Given ch is not in the list" << endl; return false;

} else { tmp = prev->next;

prev->next = next;

free(tmp);

} }

std::ostream& operator<<(std::ostream& out, LinkedList& list) {

Node* cur = list.head;

out << "List contains: ";

while (cur != NULL) { out << cur->ch << " "; cur = cur->next;

} out << "\n";

return out;

}

void find(LinkedList& list, char ch) {

if (list.find(ch)) cout << "found ";

else cout << "did not find ";

cout << ch << endl;

}

int main() {

LinkedList list;

list.add('x');

list.add('y');

list.add('z');

cout << list;

find(list, 'y');

list.del('y');

cout << list;

find(list, 'y');

list.del('x');

cout << list;

find(list, 'y');

list.del('z');

cout << list; find(list, 'y');

return 0;

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <bits/stdc++.h>
using namespace std;

struct Node {
   char ch;
   Node* next;
};

Node *newNode(char ch);
{
   Node *new_node = new Node;
   new_node->ch = ch;
   new_node->next = NULL;
   return new_node;
}

Node* insertRecursive(Node* head, char ch)
{
   if(head == NULL)
       return newNode(ch);

   else
       head->next = insertRecursive(head->next, ch);
   return head;
}

void deleteRecursive(struct Node* head)
{
   if(head == NULL)
       return;
   deleteRecursive(head->next);
   free(head);
}

bool findRecursive(struct Node* head, char ch)
{
   if(head == NULL)
       return false;

   if(head->ch == ch)
       return true;

   return searchRecursive(head->next, ch);
}

int main()
{
   Node* head = NULL;
  
   head = insertRecursive(head, 'w');
   head = insertRecursive(head, 'x');
   head = insertRecursive(head, 'y');
   head = insertRecursive(head, 'z');
   findRecursive(head, 'x')? cout<<"Yes" : cout<<"No";
   deleteRecursive(head);

   return 0;
}
  

Add a comment
Know the answer?
Add Answer to:
Please rewrite this function using recursive function #include using namespace std; struct Node { char ch;...
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
  • #include <iostream> using namespace std; struct ListNode { float value; ListNode *next; }; ...

    #include <iostream> using namespace std; struct ListNode { float value; ListNode *next; }; ListNode *head; class LinkedList { public: int insertNode(float num); void deleteNode(float num); void destroyList(); void displayList(); LinkedList(void) {head = NULL;} ~LinkedList(void) {destroyList();} }; int LinkedList::insertNode(float num) { struct ListNode *newNode, *nodePtr = head, *prevNodePtr = NULL; newNode = new ListNode; if(newNode == NULL) { cout << "Error allocating memory for new list member!\n"; return 1; } newNode->value = num; newNode->next = NULL; if(head==NULL) { cout << "List...

  • #include "name.h" #include "contact.h" using namespace std; class ContactList; typedef class Node* NodePtr; class Node {...

    #include "name.h" #include "contact.h" using namespace std; class ContactList; typedef class Node* NodePtr; class Node {     Contact item;     NodePtr next;     friend class ContactList; }; class ContactList { public:     ContactList(char* clfile) ;     ~ContactList();     void display       (ostream & output) const;     int   insert        (Contact record_to_insert);     int   insert        (ContactList contact_list);     int   remove        (Contact record_to_delete);     int   size          () const;     int   save          () const;     void find_by_lname (ostream & output, string lname) const;     void...

  • For the LinkedList class, create a getter and setter for the private member 'name', constructing your...

    For the LinkedList class, create a getter and setter for the private member 'name', constructing your definitions based upon the following declarations respectively: std::string get_name() const; and void set_name(std::string); In the Main.cpp file, let's test your getter and setter for the LinkedLIst private member 'name'. In the main function, add the following lines of code: cout << ll.get_name() << endl; ll.make_test_list(); ll.set_name("My List"); cout << ll.get_name() << endl; Output should be: Test List My List Compile and run your code;...

  • C++ program: Convert the classes to template classes #include <iostream> #include <string> using namespace std; class Node { private: int data; Node* next; public: Node(int...

    C++ program: Convert the classes to template classes #include <iostream> #include <string> using namespace std; class Node { private: int data; Node* next; public: Node(int data) { this->data=data; this->next = 0; } int getData(){return data;} Node* getNext(){return next;} void setNext(Node* next){this->next=next;} }; class LinkedList { private: Node* head = 0; public: int isEmpty() {return head == 0;} void print() { Node* currNode = head; while(currNode!=0) { cout << currNode->getData() << endl; currNode = currNode->getNext(); } } void append(int data) {...

  • C++ - I have a doubly linked list, but I haven't been able to get the...

    C++ - I have a doubly linked list, but I haven't been able to get the "reverse list" option in the code to work(It's option #in the menu in the program). I received this guidance for testing: Test 4 cases by entering (in this order) c,a,z,k,l,m This tests empty list, head of list, end of list and middle of list. Then delete (in this order) a,z,l. This tests beginning, end and middle deletes. This exhaustively tests for pointer errors. #include...

  • Hi I got error C2660 : 'valuein' : function does not take 2 parameters in visual...

    Hi I got error C2660 : 'valuein' : function does not take 2 parameters in visual studio 2013 this is my code so far #include <cstdlib> #include <list> #include <iostream> using namespace std; struct node {    int data;    node* next; }; void initialize(node*p); void insert(int data,int n); void remove(int b,int pos); bool empty(node*); void length(); bool valuein(int c); int reportvalue(int value); void ptintlist(); int menu(); int main() {    struct node*head=NULL;    char commands;    int value;   ...

  • // Code is buggy. Fix bugs. Do NOT re-write the whole code! #include <iostream> using namespace...

    // Code is buggy. Fix bugs. Do NOT re-write the whole code! #include <iostream> using namespace std; class LinkedList; class Node { private: string data; Node* next; public: Node(string s, Node* n); string getData() {return data;} Node* getNext() {return next;} } Node::Node(string s, Node* n) { data = s; next = n; } class LinkedList { private: Node* head; public: LinkedList(); bool insert(string s); friend ostream& operator<<(ostream& os, LinkedList l); }; LinkedList::LinkedList() : head(nullptr) { } bool LinkedList::insert(string s) {...

  • Double linked list implementation of PutItem function. How to fix my code to get desired output b...

    Double linked list implementation of PutItem function. How to fix my code to get desired output below: Output: 2 5 8 #ifndef ITEMTYPE_H #define ITEMTYPE_H enum RelationType { LESS, GREATER, EQUAL}; class ItemType { public:     ItemType();     void setValue(int newValue);     int getValue() const;     RelationType ComparedTo(ItemType newItem); private:     int value; }; #endif // ITEMTYPE_H // ItemType.cpp #include "ItemType.h" ItemType::ItemType() {     value = 0; } void ItemType::setValue(int newValue) {     value = newValue; } int ItemType::getValue() const {     return value; } RelationType ItemType::ComparedTo(ItemType newItem)...

  • I need help with todo line please public class LinkedList { private Node head; public LinkedList()...

    I need help with todo line please public class LinkedList { private Node head; public LinkedList() { head = null; } public boolean isEmpty() { return head == null; } public int size() { int count = 0; Node current = head; while (current != null) { count++; current = current.getNext(); } return count; } public void add(int data) { Node newNode = new Node(data); newNode.setNext(head); head = newNode; } public void append(int data) { Node newNode = new Node(data);...

  • C++ LinkedList I need the code for copy constructor and assignment operator #include <iostream> #include <string> using namespace std; typedef string ItemType; struct Node {    ItemType va...

    C++ LinkedList I need the code for copy constructor and assignment operator #include <iostream> #include <string> using namespace std; typedef string ItemType; struct Node {    ItemType value;    Node *next; }; class LinkedList { private:    Node *head;    // You may add whatever private data members or private member functions you want to this class.    void printReverseRecursiveHelper(Node *temp) const; public:    // default constructor    LinkedList() : head(nullptr) { }    // copy constructor    LinkedList(const LinkedList& rhs);    // Destroys all the dynamically allocated memory    //...

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