C++! Having a really hard time comipling this. Here is the main that i have so f
ID: 3725826 • Letter: C
Question
C++! Having a really hard time comipling this. Here is the main that i have so far. Was able to get number 2 done but i need help getting the third. The image shows my main and my attempt. The code at the bottom is the Slinkedlist.h which i have tried to get to work. Any help is great. If you can get this to complie even better! this is for C++! thank you! Again not number 2 but number 3! number 2 is done! THANK YOU!
#ifndef Slinkedlist_h
#define Slinkedlist_h
#include <stdio.h>
#pragma once
#include <stdexcept>
using namespace std;
template <typename E> class SLinkedList; // forward declaration to be used when declaring SNode
template <typename E>
class SNode { // singly linked list node
private:
E elem; // linked list element value
SNode<E> *next; // next item in the list
friend class SLinkedList<E>; // provide SLinkedList access
};
template <typename E>
class SLinkedList { // a singly linked list
public:
SLinkedList(); // empty list constructor
~SLinkedList(); // destructor
bool empty() const; // is list empty?
E& front(); // return front element
void addFront(const E& e); // add to front of list
void addBack(const E& e);
void removeFront(); // remove front item list
int size() const; // list size
void countVowel(const E& e);
int numVowels() const;
int numConsonants() const;
//void reverse(const E& ptr);
private:
SNode<E>* head; // head of the list
int n; // number of items
int vowelCount;
int consonantCount;
};
template <typename E>
SLinkedList<E>::SLinkedList() // constructor
: head(NULL), n(0), vowelCount(0), consonantCount(0) { }
template <typename E>
bool SLinkedList<E>::empty() const // is list empty?
{
return head == NULL; // can also use return (n == 0);
}
template <typename E>
E& SLinkedList<E>::front() // return front element
{
if (empty()) throw length_error("empty list");
return head->elem;
}
template <typename E>
SLinkedList<E>::~SLinkedList() // destructor
{
while (!empty()) removeFront();
}
template <typename E>
void SLinkedList<E>::addFront(const E& e) { // add to front of list
SNode<E>* v = new SNode<E>; // create new node
v->elem = e; // store data
v->next = head; // head now follows v
head = v; // v is now the head
n++;
}
template <typename E>
void SLinkedList<E>::addBack(const E& e) {
// create node
SNode<E>* v = new SNode<E>;
v->elem = e;
v->next = NULL;
countVowel(e);
if(!head) { // empty list becomes the new node
head = v;
return;
} else { // find last and link the new node
SNode<E>* last = head;
while(last->next) last=last->next;
last->next = v;
}
}
template <typename E>
void SLinkedList<E>::reverse(const E& ptr) {
if (empty()) throw length_error("empty list");
SNode<E>* old = head; // save current head
while(old->next){
old->next = reverse(this);
}
head = old->next;
delete old;
}
template <typename E>
void SLinkedList<E>::removeFront() { // remove front item
if (empty()) throw length_error("empty list");
SNode<E>* old = head; // save current head
head = old->next; // skip over old head
delete old; // delete the old head
n--;
}
template <typename E>
int SLinkedList<E>::size() const { // list size
return n;
}
template <typename E>
int SLinkedList<E>::numVowels() const {
return vowelCount;
}
template <typename E>
int SLinkedList<E>::numConsonants() const {
return consonantCount;
}
template <typename E>
void SLinkedList<E>::countVowel(const E& e){ // list size
if (isalpha(e)){
if(e == 'a' || e == 'A' || e == 'e' || e == 'E' || e == 'i' || e == 'I' || e == 'o' || e == 'O' || e == 'u' || e == 'U'){
vowelCount++;
}
// if (e == ' ') {
// return;
// }
else{
consonantCount++;
}
}
}
#endif /* Slinkedlist_hpp */
Explanation / Answer
I didn't added main. I just replaced reverse method in your code. If you need entire code, give me a comment.
#ifndef Slinkedlist_h
#define Slinkedlist_h
#include <stdio.h>
#pragma once
#include <stdexcept>
using namespace std;
template <typename E>
class SLinkedList; // forward declaration to be used when declaring SNode
template <typename E>
class SNode
{ // singly linked list node
private:
E elem; // linked list element value
SNode<E> *next; // next item in the list
friend class SLinkedList<E>; // provide SLinkedList access
};
template <typename E>
class SLinkedList
{ // a singly linked list
public:
SLinkedList(); // empty list constructor
~SLinkedList(); // destructor
bool empty() const; // is list empty?
E &front(); // return front element
void addFront(const E &e); // add to front of list
void addBack(const E &e);
void removeFront(); // remove front item list
int size() const; // list size
void countVowel(const E &e);
int numVowels() const;
int numConsonants() const;
void reverse(const E& ptr);
private:
SNode<E> *head; // head of the list
int n; // number of items
int vowelCount;
int consonantCount;
};
template <typename E>
SLinkedList<E>::SLinkedList() // constructor
: head(NULL), n(0), vowelCount(0), consonantCount(0)
{
}
template <typename E>
bool SLinkedList<E>::empty() const // is list empty?
{
return head == NULL; // can also use return (n == 0);
}
template <typename E>
E &SLinkedList<E>::front() // return front element
{
if (empty())
throw length_error("empty list");
return head->elem;
}
template <typename E>
SLinkedList<E>::~SLinkedList() // destructor
{
while (!empty())
removeFront();
}
template <typename E>
void SLinkedList<E>::addFront(const E &e)
{ // add to front of list
SNode<E> *v = new SNode<E>; // create new node
v->elem = e; // store data
v->next = head; // head now follows v
head = v; // v is now the head
n++;
}
template <typename E>
void SLinkedList<E>::addBack(const E &e)
{
// create node
SNode<E> *v = new SNode<E>;
v->elem = e;
v->next = NULL;
countVowel(e);
if (!head)
{ // empty list becomes the new node
head = v;
return;
}
else
{ // find last and link the new node
SNode<E> *last = head;
while (last->next)
last = last->next;
last->next = v;
}
}
template <typename E>
void SLinkedList<E>::reverse(const E &ptr)
{
if (empty())
throw length_error("empty list");
SNode<E> *prev = NULL, *current = head, *next;
while (current != NULL)
{
next = current->next;
current->next = prev;
prev = current;
current = next;
}
head = prev;
}
template <typename E>
void SLinkedList<E>::removeFront()
{ // remove front item
if (empty())
throw length_error("empty list");
SNode<E> *old = head; // save current head
head = old->next; // skip over old head
delete old; // delete the old head
n--;
}
template <typename E>
int SLinkedList<E>::size() const
{ // list size
return n;
}
template <typename E>
int SLinkedList<E>::numVowels() const
{
return vowelCount;
}
template <typename E>
int SLinkedList<E>::numConsonants() const
{
return consonantCount;
}
template <typename E>
void SLinkedList<E>::countVowel(const E &e)
{ // list size
if (isalpha(e))
{
if (e == 'a' || e == 'A' || e == 'e' || e == 'E' || e == 'i' || e == 'I' || e == 'o' || e == 'O' || e == 'u' || e == 'U')
{
vowelCount++;
}
// if (e == ' ') {
// return;
// }
else
{
consonantCount++;
}
}
}
#endif /* Slinkedlist_hpp */
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.