Question

In: Computer Science

Could you please write "linkedlist.cpp" using recursive approach to manage a linked list for the given...

Could you please write "linkedlist.cpp" using recursive approach to manage a linked list for the given files below as well as an updated makefile.

// app.cpp

#include <iostream>
#include "linkedlist.h"

using namespace std;

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;
}

//-------------------

//linkedlist.h

#ifndef _LINKED_LIST_
#define _LINKED_LIST_

#include <ostream>

class LinkedList
{
public:
LinkedList();
~LinkedList();

void add(char ch);
bool find(char ch);
bool del(char ch);

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

#endif // _LINKED_LIST_

//-------------------------
//makefile

CC = g++
CPPFLAGS = -Wall -g -std=c++11

app: app.o linkedlist.o

app.o: linkedlist.h

linkedlist.o: linkedlist.h

.PHONY: clean
clean: # clean the directory
$(info -- cleaning the directory --)
rm -f *.o
rm -f app
//-----------------------

Solutions

Expert Solution

// Recursive CPP program to recursively insert

// a node and recursively print the list.

#include <bits/stdc++.h>

using namespace std;

struct Node {

    int data;

    Node* next;

};

// Allocates a new node with given data

Node *newNode(int data)

{

    Node *new_node = new Node;

    new_node->data = data;

    new_node->next = NULL;

    return new_node;

}

// Function to insert a new node at the

// end of linked list using recursion.

Node* insertEnd(Node* head, int data)

{

    // If linked list is empty, create a

    // new node (Assuming newNode() allocates

    // a new node with given data)

    if (head == NULL)

         return newNode(data);

    // If we have not reached end, keep traversing

    // recursively.

    else

        head->next = insertEnd(head->next, data);

    return head;

}

void traverse(Node* head)

{

    if (head == NULL)

       return;

     

    // If head is not NULL, print current node

    // and recur for remaining list   

    cout << head->data << " ";

    traverse(head->next);

}

// Driver code

int main()

{

    Node* head = NULL;

    head = insertEnd(head, 6);

    head = insertEnd(head, 8);

    head = insertEnd(head, 10);

    head = insertEnd(head, 12);

    head = insertEnd(head, 14);

    traverse(head);

}


Related Solutions

write code to manage a linked list using recursive approach. (Using this code) C++ IN Unix....
write code to manage a linked list using recursive approach. (Using this code) C++ IN Unix. // app.cpp #include <iostream> #include "linkedlist.h" using namespace std; 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...
Could you please write a working program "linkedlist.cpp" using recursive approach for the given files below...
Could you please write a working program "linkedlist.cpp" using recursive approach for the given files below -------------------------------------------------------------------------------------------- // app.cpp #include <iostream> #include "linkedlist.h" using namespace std; 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;...
write a recursive method that returns the product of all elements in java linked list
write a recursive method that returns the product of all elements in java linked list
Could you write a c- program that reads a text file into a linked list of...
Could you write a c- program that reads a text file into a linked list of characters and then manipulate the linked list by making the following replacements 1. In paragraph 1 Replace all “c” with “s” if followed by the characters “e”, “i” or “y”; otherwise 2. In pragraph 2 Replace "We" with v"i" This is the text to be manipulated: Paragraph1 She told us to take the trash out. Why did she do that? I wish she would...
Could you write a c- program that reads a text file into a linked list of...
Could you write a c- program that reads a text file into a linked list of characters and then manipulate the linked list by making the following replacements 1. Replace all “c” with “s” if followed by the characters “e”, “i” or “y”; otherwise 2. Replace "sh" with ph This is the text to be manipulated: Paragraph1 She told us to take the trash out. Why did she do that? I wish she would not do that Paragraph 2 We...
Write a recursive algorithm in pseudo-code to compute the “power list” of a given list of...
Write a recursive algorithm in pseudo-code to compute the “power list” of a given list of integers. Assume that the List type has members: int List.length returns the length of the list. void List.push(T n) pushes an element n to the front of the list T List.pop() pops an element from the front of the list. List$$ List$$.concat(List$$ other) returns the concatenation of this list with other. Explain in plain English the reasoning behind your algorithm. Power Lists should be...
Implement a non-recursive reverse print of linked list using stack and the main function to test:...
Implement a non-recursive reverse print of linked list using stack and the main function to test: You will need to finish the printReversed_nonrecursive method in ch04.LinkedStack2 class, and the ch04.UseStack2 is the main function to test. public class LinkedStack2<T> extends LinkedStack<T> { private void revPrint(LLNode<T> listRef) { if (listRef != null) { revPrint(listRef.getLink()); System.out.println(" " + listRef.getInfo()); } } public void printReversed() { revPrint(top); } /* use stack to implement non-recursive reverse print */ public void printReversed_nonrecursive() { } public...
Can you fix please? this is adding the given location of doubly linked list in java...
Can you fix please? this is adding the given location of doubly linked list in java but if I make reverse is not adding the new node that I added but is printing forward correctly. just fix to me that part public void addAtLocation(E newNode, int location) {        Node node = new Node(newNode);               node.data = newNode;               Node previous = head;                      int counter = 1;...
JAVA Write a class for a Stack of characters using a linked list implementation. Write a...
JAVA Write a class for a Stack of characters using a linked list implementation. Write a class for a Queue of characters using a linked list implementation. Write a class for a Queue of integers using a circular array implementation.
TITLE Updating Accounts Using Doubly Linked List TOPICS Doubly Linked List DESCRIPTION General Write a program...
TITLE Updating Accounts Using Doubly Linked List TOPICS Doubly Linked List DESCRIPTION General Write a program that will update bank accounts stored in a master file using updates from a transaction file. The program will maintain accounts using a doubly linked list. The input data will consist of two text files: a master file and a transaction file. See data in Test section below.  The master file will contain only the current account data. For each account, it will contain account...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT