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;
}
#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;
}
Please rewrite this function using recursive function #include using namespace std; struct Node { char ch;...
#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...
CONVERT THE FOLLOWING C/C++ PROGRAM INTO JAVA: //LinkedString.h #pragma once #include<iostream> #include<string> using namespace std; //declare a node datastruct struct Node { char c; Node *next; }; class LinkedString { Node *head; public: LinkedString(); LinkedString(char[]); LinkedString(string); char charAt(int) const; string concat(const LinkedString &obj) const; bool isEmpty() const; int length() const; LinkedString substring(int, int) const; //added helper function to add to linekd list private: void add(char c); };...
#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...
In C++, change this code to a circular linked list #include <iostream> using namespace std; struct node { string data; node *next; }; class linked_list { private: node *head,*tail; public: linked_list() { head = NULL; tail = NULL; } void add_node(string n) { node *tmp = new node; tmp->data = n; tmp->next = NULL; if(head == NULL) { head = tmp; tail = tmp; } else { tail->next = tmp; tail = tail->next; } } void display() { while (tmp...
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 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) {...
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...
C++ comment code Comment the following code #include <iostream> using namespace std; class Node { public: Node(int val); int value; Node* next; }; Node::Node(int val){ value = val; } class List { public: List(); // Uncomment the line below once you're ready List(List &other); void push_front(int value); bool pop_front(int &value); void push_back(int value); bool pop_back(int &value); int at(int index); void insert_at(int index, int value); void remove_at(int index); int size(); private: // other members you may have used Node* head; Node*...
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...
Using the provided Linked List template, add the following recursive functions and demonstrate them in a separate cpp file. Write a recursive function to print the list in order. Write a recursive function to print the list in reverse order. Write a recursive function to print every other node in the list in order. Write a recursive function to return the number of nodes in the list. Write a Boolean function that implements the recursive version of sequential search. THIS...