I keep getting compiler errors specifically about my functions not being apart of a class.
Here is my header file declaration
#ifndef LL_H
#define LL_H
// include this library to use NULL, otherwise use nullptr instead
#include <cstddef>
// include iostream so anything that includes this file can use cout
#include <iostream>
// Struct which will be the building block of our list
struct node{
int val;
node* next;
node* prev;
};
Here is the linked list class definition
// Linked list class definition
class LL{
public:
LL();
void prepend(int);
void append(int);
void remove(int);
bool removeFront();
bool removeBack();
node* search(int);
void print();
void deleteList();
private:
node* head;
};
#endif
Here is the header file that contains the linked list functions.
#include "ll.h"
LL::LL()
{
head = NULL;
}
void LL::prepend(int num)
{
node* newNode = new node;
newNode->val = num;
newNode->next = NULL;
if (head == NULL)
{
head = newNode;
return;
}
else
{
newNode->next = head;
head = newNode;
newNode = NULL;
}
}
void LL::append(int num)
{
node* newNode = new node;
newNode->val = num;
newNode->next = NULL;
//If there are no nodes, make newNode the first node
if (head == NULL)
{
head = newNode;
newNode->next = NULL;
}
else
{
newNode->next = head;
}
}
void LL::remove(int num){
//This function searches for a node with num as its value, if found, it is deleted from the list
node* second = head;
node* first = head->next;
if (head == NULL)
{
return;
}
else if (head->val == num)
{
node* temp = head;
head = head->next;
delete temp;
}
while (first&&first->val != num)
{
second = first;
first = first->next;
}
if (first)
{
second->next = first->next;
delete first;
}
}
bool LL::removeFront()
{
node* newNode = new node;
head = head->next;
if (head == NULL)
{
newNode = head;
head = head->next;
delete newNode;
}
}
bool LL::removeBack()
{
if (head == NULL)
{
return (false);
}
else
{
node* newNode = new node;
while(newNode->next)
{
newNode->next = newNode;
}
delete newNode;
return (true);
}
}
node* LL::Search(int num)
{
node* newNode = head;
while (newNode == NULL)
{
if (newNode->num == num)
{
return newNode;
}
newNode = newNode->next;
}
return NULL;
}
void LL::print()
{
node* temp = head;
while(temp !=NULL)
{
temp = temp->next;
}
}
void LL::deleteList()
{
node* temp = head->next;
while(temp != NULL)
{
delete(head);
head = temp;
temp = temp->next;
}
}
Do I turn my header bool functions into integer passes? or do I do something else to prevent the compile errors? I can't use any standard functions in a standard library.
I'm using this main.cpp to test the code where we're inputting values from the first column followed by a number which indicates the function followed by an integer that gets passed to the function to manipulate the linked list.
#include <fstream> // Include to use ifstream
#include "ll.h" // Include so can access our class
using namespace std; // Include so we don't need to put std:: infront
// of cout and endl
int main()
{
LL myList;
ifstream input;
int cmd, argument, ret;
node* searchResult = NULL;
input.open("cmd.txt");
// while there is something to read from the file, read
while (input >> cmd)
{
// switch on the command we read from the file
switch (cmd)
{
// if the cmd requires a parameter, read it from the file and call the
// associated function
case 1:
input >> argument;
myList.prepend(argument);
cout << "Prepended " << argument << endl;
break;
case 2:
input >> argument;
myList.append(argument);
cout << "Apended " << argument << endl;
break;
case 3:
if(myList.removeFront())
{
cout << "Succesfully removed the front of the list\n";
}
else
{
cout << "List is empty, nothing to remove\n";
}
break;
case 4:
if(myList.removeBack())
{
cout << "Succesfully removed the back of the list\n";
}
else
{
cout << "List is empty, nothing to remove\n";
}
break;
case 5:
input >> argument;
searchResult = myList.search(argument);
if(NULL != searchResult)
cout << "Found " << searchResult->val <<" in list!"<<endl;
else
cout << "Did not find " << argument << " in the list"<<endl;
break;
case 6:
myList.print();
break;
case 7:
input >> argument;
cout << "Attempting to remove " << argument << endl;
if(myList.remove(argument))
{
cout << "Succesfully removed the element from the list\n";
}
else
{
cout << "Could not remove the element from the list\n";
}
break;
}
}
input.close();
return 0;
}