Question

In: Computer Science

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

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 <<...
How to write code for stack with singly linked list using C? Please show examples for...
How to write code for stack with singly linked list using C? Please show examples for create, free, isempty, push, top, pop functions.
using C++. edit this code down below so that it will implement stack with linked list...
using C++. edit this code down below so that it will implement stack with linked list contains a default constructor, a copy constructor, and a destructor. #include <iostream> #include <vector> #include <string> #include <stack> #include <limits> using namespace std; class Stack { public: bool isEmpty(); int top(); int pop(); void push(int); void printList(); private: vector<int> elements; }; bool Stack::isEmpty() { return elements.empty(); } int Stack::top() { if(isEmpty()) { throw runtime_error("error: stack is empty"); } return elements.back(); } int Stack::pop() {...
Write a recursive function in C++ that creates a copy of an array of linked lists....
Write a recursive function in C++ that creates a copy of an array of linked lists. Assuming: struct node { int data; node * next; }; class arrayList { public: arrayList(); ~arrayList(); private: node ** head; int size; //(this can equal 10) }
Write a code to implement a python queue class using a linked list. use these operations...
Write a code to implement a python queue class using a linked list. use these operations isEmpty • enqueue. • dequeue    • size Time and compare the performances of the operations ( this is optional but I would appreciate it)
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
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...
Solve this Write a C++ class that implements a stack using a linked list. The type...
Solve this Write a C++ class that implements a stack using a linked list. The type of data contained in the stack should be double. The maximum size of the stack is 30. Implement the following methods: . · Constructor and destructor; // 5 pts · void push (double value); // pushes an element with the value into the stack. 5 pts. · double pop (); // pops an element from the stack and returns its value. 5 pts. ·...
Write the code for postfix expression in C++ using a linked stack that can take numbers...
Write the code for postfix expression in C++ using a linked stack that can take numbers bigger than 9 (any size the user gives) and pushes the final result onto the top of the stack
Write a code to implement a python stack class using linked list. use these operations isEmpty...
Write a code to implement a python stack class using linked list. use these operations isEmpty   • push. • pop.   • peek. • size Time and compare the performances ( this is optional but I would appreciate it)
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT