Could you please write \"linkedlist.cpp\" for the given files below? -----------
ID: 3703507 • Letter: C
Question
Could you please write "linkedlist.cpp" 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_
Explanation / Answer
#pragma once
//linkedlist.h
#ifndef _LINKED_LIST_
#define _LINKED_LIST_
#include <ostream>
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);
};
#endif // _LINKED_LIST_
----------------------------------------
//linkedlist.cpp
#include"linkedlist.h"
#include<iostream>
using namespace std;
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) //if ch found at head
{
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);
}
return true;
}
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 << " ";
return out;
}
---------------------------------------------------
//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');
//added by CHEGG EA
list.add('a');
list.add('b');
list.add('c');
list.add('d');
list.add('e');
cout << list<<endl;
list.del('d');
cout << list << endl;
list.del('e');
cout << list << endl;
return 0;
}
/*
List contains: x y z
found y
List contains: x z
did not find y
List contains: z
did not find y
List contains:
did not find y
//output2
List contains: x y z
found y
List contains: x z
did not find y
List contains: z
did not find y
List contains:
did not find y
List contains: a b c d e
List contains: a b c e
List contains: a b c
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.