In: Computer Science
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;
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_
//app.cpp
#include<bits/stdc++.h>
#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('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<iostream>
#include<ostream>
using namespace std;
class LinkedList{
char v[1000];
int count;
public:
LinkedList(){
count=0;
}
void add(char ch){
v[count]=ch;
count++;
}
bool find(char ch,int i=0){
if(i==count)
return false;
if(v[i]==ch)
return true;
return find(ch,i+1);
}
bool del(char ch,int i=0){
if(i==count)
return false;
if(v[i]==ch){
for(int j=i+1;j<count;j++)
v[j-1]=v[j];
count=count-1;
return true;
}
return del(ch,i+1);
}
~LinkedList(){
count=0;
}
friend ostream& operator << (ostream& out,const LinkedList& list);
};
ostream& operator << (ostream& out,const LinkedList& list){
for(int i=0;i<list.count;i++){
out<<list.v[i]<<" ";
}
out<<endl;
return out;
}
#endif
The code is working and the output is attached below: