Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

This is the SLinkedList class we discussed in class it is in c++ // SLinkedList.

ID: 3723055 • Letter: T

Question

This is the SLinkedList class we discussed in class it is in c++

// SLinkedList.h

// Code from:
// Data Structures and Algorithms in C++, Goodrich, Tamassia, and Mount, 2nd Ed., 2011.
//

#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 removeFront(); // remove front item list
int size() const; // list size
private:
SNode<E>* head; // head of the list
int     n; // number of items
};

template <typename E>
SLinkedList<E>::SLinkedList() // constructor
: head(NULL), n(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>::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;
}

// SLinkedList.cpp

#include <stdexcept>
#include <iostream>
#include <string>

#include "SLinkedList.h"

using namespace std;

class Student {
private:
string _name;
int _nsemesters;

public:
void print() {
cout << "Name:" << _name << "; No. of semesters=" << _nsemesters << endl;
}
void setName(string name) {
_name = name;
}
void setNSemesters(int n) {
_nsemesters = n;
}
void updateNSemesters() {
_nsemesters++;
}
};

int main() {

SLinkedList<Student> students;

Student s;
for (int i = 0; i < 5; i++) {
s.setName("Student_" + to_string(i));
s.setNSemesters(2);
students.addFront(s);
}

while (!students.empty()) {
students.front().print();
students.removeFront();
}
// system("pause");
}

#2 [6 points] Use the template class SlinkedList discussed in class to implement a singly linked list of characters that store all the characters read from a line of text. The line of text will contain upper and lower case letters, digits, punctuation signs and white spaces. Add two member functions, countVowel and countConsonant that count the number of vowels in the list, respectively the number of consonants in the list. For example, if the line of text given as a input is "Is dark at 6?!", the link list looks like: The number of vowels is 3 and the number of consonants is 5.

Explanation / Answer

Student class completely removed... and main function changed completely

// SLinkedList.h

// Code from:

// Data Structures and Algorithms in C++, Goodrich, Tamassia, and Mount, 2nd Ed., 2011.

//

// #pragma once

#include <stdexcept>

#include <iostream>

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); // add to end of list

void removeFront(); // remove front item list

void print();

int size() const; // list size

private:

SNode<E> *head; // head of the list

int n; // number of items

};

template <typename E>

SLinkedList<E>::SLinkedList() // constructor

: head(NULL), n(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)

{ // add to front of list

SNode<E> *v = new SNode<E>; // create new node

v->elem = e; // store data

v->next = NULL; // head now follows v

if (head == NULL)

{

head = v;

}

else

{

SNode<E> *old = head; // save current head

while (old->next != NULL)

{

old = old->next;

}

old->next = v;

}

n++;

}

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>

void SLinkedList<E>::print()

{ // remove front item

int cons = 0, vowels = 0;

if (empty())

throw length_error("empty list");

SNode<E> *old = head; // save current head

while (old != NULL)

{

cout << old->elem << " ";

if (old->elem == 'A' || old->elem == 'E' || old->elem == 'I' || old->elem == 'O' || old->elem == 'U' || old->elem == 'a' || old->elem == 'e' || old->elem == 'i' || old->elem == 'o' || old->elem == 'u')

vowels++;

else if((old->elem>='A' && old->elem<='Z') || (old->elem>='a' && old->elem<='z'))

cons++;

old = old->next; // skip over old head

}

cout << " The number of vowels is " << vowels << " and the number of consonants is " << cons << endl;

}

template <typename E>

int SLinkedList<E>::size() const

{ // list size

return n;

}

// SLinkedList.cpp

#include <stdexcept>

#include <iostream>

#include <cstring>

#include <string>

// #include "SLinkedList.h"

using namespace std;

int main()

{

SLinkedList<char> s;

string inp;

cout << "Enter input: ";

getline(cin, inp);

for (int i = 0; i < inp.length(); i++)

{

s.addBack(inp[i]);

}

s.print();

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote