I am just starting a homework assignment and this is my first time using seperat
ID: 3737033 • Letter: I
Question
I am just starting a homework assignment and this is my first time using seperate .cpp and .hpp files for my code, and I am getting a lot of errors in the .cpp file and am and not sure why. Here is my code
----------------------
//.hpp file
#ifndef SLL_HPP_
#define SLL_HPP_
#include "SNode.hpp"
#include <stdlib.h>
#include <iostream>
using namespace std;
class SLL {
friend class WebTopic;
SNode *first;
SNode *last;
SNode *p2;
int size;
public:
SLL();
~SLL();
void printSLL();
void priorityInsert(string s, int p);
void push(string s, int p);
void addAtFront(string s, int p);
void addFirst(string s, int p);
void addAtP2(string s, int p);
int removeAll(string w);
string pop();
};
#endif /* SLL_HPP_ */
------------------
//.cpp file
#include "SLL.hpp"
#include "SNode.hpp"
#include <stdlib.h>
#include <iostream>
using namespace std;
void SLL::printSLL() {
SNode *tmp = first;
while (tmp != NULL) {
cout << tmp->data << "->";
tmp = tmp->next;
}
cout << endl;
}
Explanation / Answer
SLL.hpp
#ifndef SLL_HPP_
#define SLL_HPP_
#include "SNode.hpp"
#include <stdlib.h>
#include <iostream>
using namespace std;
class SLL {
friend class WebTopic;
SNode *first;
SNode *last;
SNode *p2;
int size;
public:
SLL();
~SLL();
void printSLL();
void priorityInsert(string s, int p);
void push(string s, int p);
void addAtFront(string s, int p);
void addFirst(string s, int p);
void addAtP2(string s, int p);
int removeAll(string w);
string pop();
};
#endif /* SLL_HPP */
SLL.cpp
#include "SLL.hpp"
using namespace std;
SLL::SLL() {
first = NULL;
last = NULL;
size = 0;
p2 = NULL;
}
SLL::~SLL() {
delete first;
delete last;
delete p2;
}
void SLL::printSLL() {
SNode *tmp = first;
for(int i = 0; i < size; i++){
cout << tmp << "->";
tmp = tmp->next;
}
cout << endl;
}
void SLL::priorityInsert(string s, int p) {
if(p == 1){
addAtFront(s,p);
}else if(p == 2){
addAtP2(s,p);
}else{
push(s,p);
}
}
void SLL::push(string s, int p) {
if(size==0){
addFirst(s,p);
}else{
SNode *newNode = new SNode(s,p);
last->next = newNode;
last = newNode;
size++;
}
}
void SLL::addAtFront(string s, int p) {
if(size==0){
addFirst(s,p);
}else{
SNode *newNode = new SNode(s,p);
newNode->next = first;
first = newNode;
size++;
}
}
void SLL::addFirst(string s, int p) {
SNode *newNode = new SNode(s,p);
first = newNode;
last = newNode;
size++;
}
void SLL::addAtP2(string s, int p) {
SNode *newNode = new SNode(s,p);
if(size==0){
addFirst(s,p);
p2 = newNode;
}
else{
newNode->next = p2->next;
p2->next = newNode;
size++;
}
}
int SLL::removeAll(string w) {
int numTimes = 0;
SNode *tmp = first;
for (int i = 0; i < size; i++){
if(tmp->word == w){
numTimes++;
}
tmp = tmp->next;
}
SNode *tmp1 = first;
SNode *tmp2 = first->next;
while(tmp2 != NULL){
if(tmp2->word == w){
if(tmp2 == last){
pop();
tmp2 == NULL;
}
else{
tmp1->next = tmp2->next;
tmp2 = tmp1->next;
size--;
}
}else{
tmp1 = tmp2;
tmp2 = tmp2->next;
}
}
if(first->word == w){
last->next = first;
first = first->next;
delete last->next;
last->next = NULL;
size--;
}
return numTimes;
}
string SLL::pop() {
SNode *tmp = first;
for(int i = 0; i < size-1; i++){
tmp = tmp->next;
}
string ret = last->word;
delete last;
last = tmp;
size--;
return ret;
}
SNode.cpp
#include "SNode.hpp"
using namespace std;
SNode::SNode(string w, int p) {
word = w;
priority = p;
next = NULL;
}
SNode::~SNode() {
if(next != NULL){
cout << "Deleting may cause memory leak!" << endl;
}
}
void SNode::printNode() {
cout << word << endl;
}
SNode.hpp
#ifndef SNODE_HPP_
#define SNODE_HPP_
#include <stdlib.h>
#include <iostream>
using namespace std;
class SNode {
friend class SLL;
friend class WebTopic;
string word; // instead of int data, now the data is a string
int priority; // the priority of a node (1,2, or 3)
SNode *next;
public:
SNode(string w, int p);
~SNode();
void printNode();
};
#endif /* SNODE_HPP_ */
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.