I am working in c++. I'm attempting to make my own iterator for a templated linked list class (without using the STL), but I seem to have trouble using "friends." I want to OListIterator to have access to the "Node" struct within the list class. If anyone could help it would be greatly appreciated!
OListIterator:
#ifndef pg6ec_OListIterator_h
#define pg6ec_OListIterator_h
#include "OList.h"
template <typename T>
class OListIterator
{
private:
T * value;
T * next;
public:
OListIterator()
{}
void setValue(T & val, T & n)
{
value = &val;
next = &n;
}
int operator*()
{
return *value;
}
bool operator==(OListIterator<T> other)
{
return value == other.value && next == other.next;
}
bool operator!=(OListIterator<T> other)
{
return value != other.value && next != other.next;
}
void operator+=(int x)
{
}
};
#endif
List:
#ifndef pg6OList_OListBlah_h
#define pg6OList_OListBlah_h
#include <stdio.h>
#include <stdlib.h>
#include "OListIterator.h"
template <typename T>
class list
{
private:
typedef struct node
{
T value;
struct node * next;
}Node;
Node * root;
public:
list()
{
root = NULL;
}
list(const list & other)
{
Node * temp = other.returnRoot();
Node * currSpot = NULL;
root = new Node;
root->value = temp->value;
currSpot = root;
temp = temp->next;
while (temp)
{
currSpot->next = new Node;
currSpot = currSpot->next;
currSpot->value = temp->value;
temp = temp->next;
}
}
~list()
{
clear();
};
void clear()
{
Node * delNode = root;
while (delNode)
{
root = root->next;
delete delNode;
delNode = root;
}
delete root;
};
Node * returnRoot() const
{
return this->root;
}
int size()
{
int ans = 0;
if (root == NULL)
{
return ans;
}
Node * top = root;
while (top)
{
ans++;
top = top->next;
}
return ans;
}
bool insert(T & item)
{
if (root == NULL)
{
root = new Node;
root->value = item;
return true;
}
else
{
Node * curr = root;
Node * prev = NULL;
while (curr)
{
if ( curr->value > item )
{
Node * insertion = new Node;
insertion->value = item;
if (prev)
{
insertion->next = curr;
prev->next = insertion;
}
else
{
root = insertion;
root->next = curr;
}
return true;
}
else if ( curr->value == item )
{
Node * insertion = new Node;
insertion->value = item;
insertion->next = curr->next;
curr->next = insertion;
return true;
}
prev = curr;
if (curr->next)
{
curr = curr->next;
}
else if ( curr->next == NULL )
{
curr->next = new Node;
curr->next->next = NULL;
curr->next->value = item;
return true;
}
}
}
return false;
}
T get(int x)
{
T ans = root->value;
if (x > size() || x < 0)
{
return ans;
}
Node * curr = root;
for (int i = 0; i < size(); i++)
{
if (i == x)
{
ans = curr->value;
break;
}
curr = curr->next;
}
return ans;
}
int count(T base)
{
int num = 0;
Node * curr = root;
while (curr)
{
if (curr->value == base)
{
num++;
}
curr = curr->next;
}
return num;
}
bool remove(T base)
{
Node * curr = root;
Node * prev = NULL;
if (root->value == base)
{
delete this->root;
root = root->next;
return true;
}
while (curr)
{
if (curr->value == base)
{
Node * temp = new Node;
if (curr->next)
{
T val = curr->next->value;
temp->value = val;
temp->next = curr->next->next;
}
delete curr;
delete curr->next;
prev->next = temp;
return true;
}
prev = curr;
curr = curr->next;
}
return false;
}
void uniquify()
{
Node * curr = root;
Node * next = root->next;
while (curr)
{
while (next && curr->value == next->value)
{
Node * temp = new Node;
if (next->next)
{
T val = next->next->value;
temp->value = val;
temp->next = next->next->next;
delete next;
delete next->next;
curr->next = temp;
next = curr->next;
}
else
{
delete temp;
delete temp->next;
curr->next = NULL;
delete next;
delete next->next;
break;
}
}
curr = curr->next;
if (curr)
next = curr->next;
}
}
OListIterator<T> begin()
{
OListIterator<T> it;
it.setValue(root->value, root->next->value);
return it;
}
OListIterator<T> end()
{
Node * curr = root;
for (int i = 0; i < size(); i++)
{
curr = curr->next;
}
OListIterator<T> it;
it.setValue(curr->value);
return it;
}
};
#endif
I want to OListIterator to have access to the "Node" struct within the list class.
For this, you need to forward-declare the iterator class:
template<typename T> OListIterator;
template <typename T>
class list
{
friend class OListIterator<T>;
//... the rest
}
Alternatively, you can use a template friend declaration, but then any OListIterator type has access to any list type:
template <typename T>
class list
{
template<typename U> friend class OListIterator;
//... the rest
}
Here is a more verbose but unrelated-to-your-code example:
template<typename T> struct Iterator;
template<typename T>
struct List
{
friend struct Iterator<T>;
List(T i) : somePrivateMember(i) {}
private:
T somePrivateMember;
};
template<typename T>
struct Iterator
{
Iterator(List<T> const& list) {std::cout<<list.somePrivateMember<<std::endl;}
};
int main()
{
List<int> list(1);
Iterator<int> iterator(list);
}
The constructor of the Iterator class prints the private member somePrivateMember, whose value is 1.
Related
I want to save the number of element inside an instance of linked list class object. From the code below everytime I call addNodeFront() or addNodeBack() function, member variable len should be incremented by 1. But, when I run the code, the getLen function only return 1, while the linked list has 2 element. What should I fix from the my code?
#include <iostream>
class Node {
public:
int value;
Node* next;
Node(int value, Node* next) {
this->value = value;
this->next = next;
}
};
class LinkedList {
public:
Node* head;
Node* tail;
int len = 0;
LinkedList() {
this->head = nullptr;
this->tail = nullptr;
}
LinkedList(Node* node) {
this->head = node;
this->tail = node;
}
int getLen() {
return len;
}
void addNodeFront(Node* node) {
if(head==nullptr && tail==nullptr) {
this->head = node;
this->tail = node;
return;
}
Node* secondFirst = this->head;
this->head = node;
node->next = secondFirst;
this->len++;
}
void addNodeBack(Node* node) {
if(head==nullptr && tail==nullptr) {
this->head = node;
this->tail = node;
return;
}
this->tail->next = node;
this->tail = node;
this->len++;
}
void addNodeAfterNode(Node* prevNode, Node* node) {
if(prevNode == this->tail) {
this->tail = node;
prevNode->next = node;
return;
}
node->next = prevNode->next;
prevNode->next = node;
}
bool searchVal(int val) const {
Node* s = this->head;
while(s != nullptr) {
if(s->value == val) return true;
s = s->next;
}
return false;
}
void deleteNodeFront() {
if(this->head==this->tail) {
this->head = nullptr;
this->tail = nullptr;
return;
}
this->head = this->head->next;
}
void deleteNodeBack() {
Node* secondLast = this->head;
if(this->head==this->tail) {
this->head = nullptr;
this->tail = nullptr;
return;
}
while(secondLast->next != this->tail) {
secondLast = secondLast->next;
}
secondLast->next = nullptr;
this->tail = secondLast;
}
void deleteNodeMiddle(Node* node) {
if(node==this->head || node==this->tail) return;
Node* prevNode = this->head;
while(prevNode->next != node) {
prevNode = prevNode->next;
}
prevNode->next = prevNode->next->next;
}
void traverseLinkedList() {
Node* t = this->head;
if(head==nullptr && tail==nullptr) std::cout << "Empty Linked List";
while(t != nullptr) {
std::cout << t->value << "->";
t = t->next;
}
std::cout << "\n";
}
};
int main() {
Node node1(2,nullptr);
Node node4(4,nullptr);
LinkedList ls;
ls.addNodeFront(&node1);
ls.addNodeFront(&node4);
std::cout << ls.getLen() << std::endl;
ls.traverseLinkedList();
}
In the following function:
LinkedList(Node* node)
{
this->head = node;
this->tail = node;
}
Just add one of the following lines:
len++; //1
len = 1; //2
So the updated function:
LinkedList(Node* node)
{
this->head = node;
this->tail = node;
len++; //1
len = 1; //2
}
This is because as the head node and the tail node = node now, that means there is a node in the linked list, so we should add 1 to len.
#include <iostream>
using namespace std;
template <typename Object>
struct Node
{
Object data;
Node* next;
Node(const Object &d = Object(), Node *n = (Object)NULL) : data(d), next(n) {}
};
template <typename Object>
class singleList
{
public:
singleList() { init(); }
~singleList() { eraseList(head); }
singleList(const singleList &rhs)
{
eraseList(head);
init();
*this = rhs;
print();
contains(head);
}
void init()
{
theSize = 0;
head = new Node<Object>;
head->next = (Object)NULL;
}
void eraseList(Node<Object> *h)
{
Node<Object> *ptr = h;
Node<Object> *nextPtr;
while (ptr != (Object)NULL)
{
nextPtr = ptr->next;
delete ptr;
ptr = nextPtr;
}
}
int size()
{
return theSize;
}
void print()
{
int i;
Node<Object> *current = head;
for(i=0; i < theSize; ++i){
cout << current->data << " ";
current = current->next;
}
}
bool contains(int x)
{
Node<Object> *current = head;
for (int i = 0; i < theSize; ++i){
if (current->data == x){
return true;
}
current = current -> next;
}
return false;
}
bool add(Object x){
if(!contains(x)){
Node<Object> *new_node = new Node<Object>(x);
new_node->data = x;
new_node->next = head;
head = new_node;
//head->next = new_node;
theSize++;
return true;
}
return false;
}
bool remove(int x)
{
if(contains(x)){
Node<Object> *temp = head;
Node<Object> *prev = NULL;
if(temp != NULL && temp ->data == x){
head = temp->next;
delete temp;
return 0;
}else{
while(temp != NULL && temp->data != x){
prev = temp;
temp = temp->next;
}
if(temp ==NULL){
return 0;
}
prev->next = temp->next;
delete temp;
}
return true;
//theSize--;
}
return false;
}
private:
Node<Object> *head;
int theSize;
};
int main()
{
singleList<int> *lst = new singleList<int>();
lst->add(10);
lst->add(12);
lst->add(15);
lst->add(6);
lst->add(3);
lst->add(8);
lst->add(3);
lst->add(18);
lst->add(5);
lst->add(15);
cout << "The original linked list: ";
lst->print();
cout << endl;
lst->remove(6);
lst->remove(15);
cout << "The updated linked list: ";
lst->print();
cout << endl;
cout << "The number of node in the list: " << lst->size() << endl;
return 0;
}
so the output is supposed to be the following:
The original linked list: 5 18 8 3 6 15 12 10
The update linked list: 5 18 8 3 12 10
The number of node in the list: 6
my output gives the original linked list part but then gives a segmentation fault (core dumped) error. I am not sure where my code is wrong but i think it is in my remove().
Some help will definitely be appreciated.
on line 109 i needed to decrement size.
bool remove(int x)
{
if(contains(x)){
Node<Object> *temp = head;
Node<Object> *prev = NULL;
if(temp != NULL && temp ->data == x){
head = temp->next;
delete temp;
return 0;
}else{
while(temp != NULL && temp->data != x){
prev = temp;
temp = temp->next;
}
if(temp ==NULL){
return 0;
}
prev->next = temp->next;
delete temp;
theSize--;
}
return true;
//theSize--;
}
return false;
}
Answer after second update:
had to fix the remove()
#include <iostream>
using namespace std;
template <typename Object>
struct Node
{
Object data;
Node* next;
Node(const Object &d = Object(), Node *n = (Object)NULL) : data(d), next(n) {}
};
template <typename Object>
class singleList
{
public:
singleList() { init(); }
~singleList() { eraseList(head); }
singleList(const singleList &rhs)
{
eraseList(head);
init();
*this = rhs;
print();
contains(head);
}
void init()
{
theSize = 0;
head = new Node<Object>;
head->next = (Object)NULL;
}
void eraseList(Node<Object> *h)
{
Node<Object> *ptr = h;
Node<Object> *nextPtr;
while (ptr != (Object)NULL)
{
nextPtr = ptr->next;
delete ptr;
ptr = nextPtr;
}
}
int size()
{
return theSize;
}
void print()
{
int i;
Node<Object> *current = head;
for(i=0; i < theSize; ++i){
cout << current->data << " ";
current = current->next;
}
}
bool contains(int x)
{
Node<Object> *current = head;
for (int i = 0; i < theSize; ++i){
if (current->data == x){
return true;
}
current = current -> next;
}
return false;
}
bool add(Object x){
if(!contains(x)){
Node<Object> *new_node = new Node<Object>(x);
new_node->data = x;
new_node->next = head;
head = new_node;
//head->next = new_node;
theSize++;
return true;
}
return false;
}
bool remove(int x){
Node<Object> *pCur = head;
Node<Object> *pPrev = pCur;
while (pCur && pCur->data != x) {
pPrev = pCur;
pCur = pCur->next;
}
if (pCur==nullptr) // not found
return false;
if (pCur == head) { // first element matches
head = pCur->next;
} else {
pPrev->next = pCur->next;
}
// pCur now is excluded from the list
delete pCur;
theSize--;
return true;
}
private:
Node<Object> *head;
int theSize;
};
int main()
{
singleList<int> *lst = new singleList<int>();
lst->add(10);
lst->add(12);
lst->add(15);
lst->add(6);
lst->add(3);
lst->add(8);
lst->add(3);
lst->add(18);
lst->add(5);
lst->add(15);
cout << "The original linked list: ";
lst->print();
cout << endl;
lst->remove(6);
lst->remove(15);
cout << "The updated linked list: ";
lst->print();
cout << endl;
cout << "The number of node in the list: " << lst->size() << endl;
return 0;
}
I have the following class:
template<class T>
struct Node {
Node<T>* next;
T data;
};
template<class T>
class LinkedList
{
private:
Node<T>* first;
Node<T>* last;
public:
LinkedList<T>() : first(NULL), last(NULL) {}
LinkedList<T>(const LinkedList& other)
{
operator=(other);
}
~LinkedList()
{
Node<T>* tmp = first->next;
while (!first)
{
tmp = first;
first = first->next;
free(tmp);
}
}
void add(const T& data) {
Node<T>* tmp = new Node<T>;
tmp->data = data;
tmp->next = NULL;
if (!first) {
first = tmp;
last = first;
}
else
{
last->next = tmp;
last = tmp;
}
}
Node<T>* getFirst() const
{
return first;
}
LinkedList<T>& operator=(const LinkedList<T>& other)
{
Node<T>* tmp = other.first;
while (tmp)
{
add(tmp->data);
tmp = tmp->next;
}
return *this;
}
LinkedList<T>& operator-=(T value)
{
while (first && first->data == value)
{
Node<T>* tmp = first;
first = first->next;
free(tmp);
}
for (Node<T>* curr = first; curr != NULL; curr = curr->next)
{
while (curr->next != NULL && curr->next->data == value)
{
Node<T>* tmp = curr->next;
if (tmp == last)
{
last = curr;
}
curr->next = tmp->next;
free(tmp);
}
}
return *this;
}
LinkedList<T>& operator+=(const T& value)
{
add(value);
return *this;
}
T get(int index) {
if (index == 0) {
return this->first->data;
}
else {
Node<T>* curr = this->first;
for (int i = 0; i < index; ++i) {
curr = curr->next;
}
return curr->data;
}
}
T operator[](int index) {
return get(index);
}
bool isInList(T value)
{
Node<T>* tmp = first;
while (tmp)
{
if (tmp->data == value)
return true;
tmp = tmp->next;
}
return false;
}
};
This current template implementation does not support the char* type, so I want to add a template specialization for the char* type.
Adding the following specialization:
template <>
LinkedList<char*>& LinkedList<char*>::operator+=(const char*& value)
{
}
gives the following error:
Error C2244 'LinkedList<char *>::operator +=': unable to match function definition to an existing declaration
How can I fix it?
I am having trouble implementing this with the class I have. So far as a linkedlist of type int or string it works great, but I am not sure how to navigate the list if I initialize it like
linkedlist<linkedlist<int>> nums;
From my program it seems that the head of each secondary list would be an entry in the primary, but my question is how would I navigate it?. For example how would I print all the values of each linkedlist, I am sure I am making this more difficult than it it, but any help would be appreciated.
/* Node Class */
template <typename T>
class Node {
public:
T data;
Node* next;
Node* previous;
Node(T data);
Node();
T getData();
};
template <typename T>
Node<T>::Node() {
this->next = NULL;
this->previous = NULL;
}
template <typename T>
Node<T>::Node(T data) {
this->data = data;
}
template <typename T>
T Node<T>::getData() {
return this->data;
}
/* Linked List: */
template <typename T>
class linkedlist {
private:
Node<T>* head;
Node<T>* tail;
int list_size;
public:
linkedlist();
T getHead();
T getTail();
int size();
void addnodetail(T);
void addnodehead(T);
void push(T);
T pop();
//T* peek();
bool isEmpty() const {
return head == NULL;
}
//T* get(int index);
void printlist();
void printListBackwards();
~linkedlist();
};
template <typename T>
linkedlist<T>::linkedlist() {
this->head = NULL;
this->tail = NULL;
this->list_size = 0;
}
template <typename T>
T linkedlist<T>::getHead() {
return this->head->data;
}
template <typename T>
T linkedlist<T>::getTail() {
return this->tail->data;
}
template <class T>
int linkedlist<T>::size() {
return this->list_size;
}
template <typename T>
void linkedlist<T>::addnodetail(T input) {
Node<T>* newnode = new Node<T>(input);
newnode->next = NULL;
newnode->previous = NULL;
if (this->head == NULL) {
this->head = newnode;
this->tail = this->head;
this->list_size = this->list_size + 1;
} else {
this->tail->next = newnode;
newnode->previous = this->tail;
this->tail = newnode;
this->list_size = this->list_size + 1;
}
}
template <typename T>
void linkedlist<T>::addnodehead(T input) {
Node<T>* newnode = new Node<T>(input);
newnode->next = NULL;
newnode->previous = NULL;
if (this->head == NULL) {
this->head = newnode;
this->tail = this->head;
this->list_size = this->list_size + 1;
} else {
this->head->previous = newnode;
newnode->next = this->head;
this->head = newnode;
this->list_size = this->list_size + 1;
}
}
template <typename T>
void linkedlist<T>::push(T input) {
this->addnodehead(input);
}
template <typename T>
T linkedlist<T>::pop() {
Node<T>* temp = this->head;
// T* temp = this->head;
this->head = this->head->next;
this->head->previous = NULL;
this->list_size = this->list_size - 1;
return temp->data;
}
/*
template <class T>
T* MyList<T>::peek() {
return this->head;
}
template <class T>
T* MyList<T>::get(int index) {
if (index == 0) {
return this->head;
} else if (index == this->list_size - 1) {
return this->tail;
} else if (index < 0 || index >= this->list_size) {
return NULL;
}
if (index < this->list_size / 2) {
T* temp = this->head;
int i = 0;
while (temp) {
if (i == index) { return temp; }
i++;
temp = temp->next;
}
} else {
T* temp = this->tail;
int i = this->list_size - 1;
while (temp) {
if (i == index) { return temp; }
i--;
temp = temp->previous;
}
}
return NULL;
}*/
template <typename T>
void linkedlist<T>::printlist() {
cout << "HEAD: ";
Node<T>* temp = this->head;
while(temp) {
cout << temp->data << " -> ";
temp = temp->next;
}
cout << "\b\b\b\b :TAIL" << endl;
}
template <class T>
void linkedlist<T>::printListBackwards() {
cout << "TAIL: ";
Node<T>* temp = this->tail;
while(temp) {
cout << temp->data << " -> ";
temp = temp->previous;
}
cout << "\b\b\b\b :HEAD" << endl;
}
template <typename T>
linkedlist<T>::~linkedlist() {
for(Node<T>* p;!isEmpty();){
p = head->next;
delete head;
head = p;
}
}
EDIT with copy constructor
#ifndef LINKEDLIST_H
#define LINKEDLIST_H
#include <iostream>
#include <ctype.h>
using namespace std;
using namespace std;
/*struct AdjListNode
{
int dest;
struct AdjListNode* next;
};
// A structure to represent an adjacency list
struct AdjList
{
struct AdjListNode *head; // pointer to head node of list
};
// A structure to represent a graph. A graph is an array of adjacency lists.
// Size of array will be V (number of vertices in graph)
struct Graph
{
int V;
struct AdjList* array;
};
// A utility function to create a new adjacency list node
struct AdjListNode* newAdjListNode(int dest)
{
struct AdjListNode* newNode =
(struct AdjListNode*) malloc(sizeof(struct AdjListNode));
newNode->dest = dest;
newNode->next = NULL;
return newNode;
}
// A utility function that creates a graph of V vertices
struct Graph* createGraph(int V)
{
struct Graph* graph = (struct Graph*) malloc(sizeof(struct Graph));
graph->V = V;
// Create an array of adjacency lists. Size of array will be V
graph->array = (struct AdjList*) malloc(V * sizeof(struct AdjList));
// Initialize each adjacency list as empty by making head as NULL
int i;
for (i = 0; i < V; ++i)
graph->array[i].head = NULL;
return graph;
}
// Adds an edge to an undirected graph
void addEdge(struct Graph* graph, int src, int dest)
{
// Add an edge from src to dest. A new node is added to the adjacency
// list of src. The node is added at the begining
struct AdjListNode* newNode = newAdjListNode(dest);
newNode->next = graph->array[src].head;
graph->array[src].head = newNode;
// Since graph is undirected, add an edge from dest to src also
newNode = newAdjListNode(src);
newNode->next = graph->array[dest].head;
graph->array[dest].head = newNode;
}
// A utility function to print the adjacenncy list representation of graph
void printGraph(struct Graph* graph)
{
int v;
for (v = 0; v < graph->V; ++v)
{
struct AdjListNode* pCrawl = graph->array[v].head;
printf("\n Adjacency list of vertex %d\n head ", v);
while (pCrawl)
{
printf("-> %d", pCrawl->dest);
pCrawl = pCrawl->next;
}
printf("\n");
}
}
*/
/* Node Class */
template <typename T>
class Node {
public:
T data;
Node* next;
Node* previous;
Node(T data);
Node();
T getData();
};
template <typename T>
Node<T>::Node() {
this->next = NULL;
this->previous = NULL;
}
template <typename T>
Node<T>::Node(T data) {
this->data = data;
this->next = NULL;
}
template <typename T>
T Node<T>::getData() {
return this->data;
}
/* Linked List: */
template <typename T>
class linkedlist {
private:
Node<T>* head;
Node<T>* tail;
int list_size;
public:
linkedlist();
linkedlist(linkedlist& object);
T getHead();
T getTail();
int size();
void addtail(const T& input);
void addhead(const T& input);
void push(T);
T pop();
//T* peek();
bool isEmpty() const {
return head == NULL;
}
//T* get(int index);
void printlist();
void printListBackwards();
~linkedlist();
};
template <typename T>
linkedlist<T>::linkedlist() {
this->head = NULL;
this->tail = NULL;
this->list_size = 0;
}
template <typename T>
linkedlist<T>::linkedlist(linkedlist &object){
if(object.head == NULL){
head == NULL;
}
else {
head = new Node<T>(object.head->data);
Node<T>* temp = head;
Node<T>* objecthead = object.head;
Node<T>* current = objecthead;
while(current->next != NULL){
temp->next = new Node<T>(current->next->data);
current = current->next;
temp = temp->next;
}
}
}
template <typename T>
T linkedlist<T>::getHead() {
return this->head->data;
}
template <typename T>
T linkedlist<T>::getTail() {
return this->tail->data;
}
template <class T>
int linkedlist<T>::size() {
return this->list_size;
}
template <typename T>
void linkedlist<T>::addtail(const T& input) {
Node<T>* newnode = new Node<T>(input);
newnode->next = NULL;
newnode->previous = NULL;
if (this->head == NULL) {
this->head = newnode;
this->tail = this->head;
this->list_size = this->list_size + 1;
} else {
this->tail->next = newnode;
newnode->previous = this->tail;
this->tail = newnode;
this->list_size = this->list_size + 1;
}
}
template <typename T>
void linkedlist<T>::addhead(const T& input) {
Node<T>* newnode = new Node<T>(input);
newnode->next = NULL;
newnode->previous = NULL;
if (this->head == NULL) {
this->head = newnode;
this->tail = this->head;
this->list_size = this->list_size + 1;
} else {
this->head->previous = newnode;
newnode->next = this->head;
this->head = newnode;
this->list_size = this->list_size + 1;
}
}
template <typename T>
void linkedlist<T>::push(T input) {
this->addhead(input);
}
template <typename T>
T linkedlist<T>::pop() {
Node<T>* temp = this->head;
if(temp != NULL){
this->head = this->head->next;
this->head->previous = NULL;
this->list_size = this->list_size - 1;
return temp->data;
}
else{
cout << "Error:Empty List!";
exit (EXIT_FAILURE);
}
}
/*
template <class T>
T* MyList<T>::peek() {
return this->head;
}
template <class T>
T* MyList<T>::get(int index) {
if (index == 0) {
return this->head;
} else if (index == this->list_size - 1) {
return this->tail;
} else if (index < 0 || index >= this->list_size) {
return NULL;
}
if (index < this->list_size / 2) {
T* temp = this->head;
int i = 0;
while (temp) {
if (i == index) { return temp; }
i++;
temp = temp->next;
}
} else {
T* temp = this->tail;
int i = this->list_size - 1;
while (temp) {
if (i == index) { return temp; }
i--;
temp = temp->previous;
}
}
return NULL;
}*/
template <typename T>
void linkedlist<T>::printlist() {
cout << "STACK" << endl;
cout << "-------------------" << endl;
Node<T>* temp = this->head;
while(temp) {
cout << "\t" << temp->data << endl;
temp = temp->next;
}
//cout << "\b\b\b\b :TAIL" << endl;
}
template <class T>
void linkedlist<T>::printListBackwards() {
cout << "TAIL: ";
Node<T>* temp = this->tail;
while(temp) {
cout << temp->data << " -> ";
temp = temp->previous;
}
cout << "\b\b\b\b :HEAD" << endl;
}
template <typename T>
linkedlist<T>::~linkedlist() {
for(Node<T>* p;!isEmpty();){
p = head->next;
delete head;
head = p;
}
}
#endif // LINKEDLIST_H
I am working on an assignment where I create test code to show the implementation of a linked list class in C++. Mostly all the code provided is given from a book other than what I have written for my main function and a few edited lines to add a tail instance. The only error that is showing up right now when I compile is this:
'LList::operator=' : must return a value
I am unsure as to why this error is being produced or how to fix it because it refers to a block of code that was given by the book. That block of code is:
LList& LList::operator=(const LList& source)
{
dealloc();
copy(source);
}
Any help would be greatly appreciated.
Just in case; here is the rest of the code from my source file.
// LList.cpp
#include "LList.h"
#include <iostream>
LList::LList()
{
head_ = NULL;
tail_ = NULL;
size_ = 0;
}
ListNode* LList::_find(size_t position)
{
ListNode *node = head_;
size_t i;
for (i = 0; i<position; i++) {
node = node->link_;
}
return node;
}
ItemType LList::_delete(size_t position)
{
ListNode *node, *dnode;
ItemType item;
if (position == 0) {
dnode = head_;
head_ = head_->link_;
item = dnode->item_;
delete dnode;
}
if (position == size_) {
dnode = tail_;
node = _find(position - 1);
node->link_ = NULL;
tail_ = node;
item = dnode->item_;
delete dnode;
}
else {
node = _find(position - 1);
if (node != NULL) {
dnode = node->link_;
node->link_ = dnode->link_;
item = dnode->item_;
delete dnode;
}
}
size_ -= 1;
return item;
}
void LList::append(ItemType x)
{
ListNode *node, *newNode = new ListNode(x);
if (head_ != NULL) {
node = _find(size_ - 1);
node->link_ = newNode;
tail_ = newNode;
}
else {
head_ = newNode;
tail_ = head_;
}
size_ += 1;
}
void LList::insert(size_t i, ItemType x)
{
ListNode *node;
if (i == 0) {
head_ = new ListNode(x, head_);
tail_ = head_;
}
else if (i == size_) {
tail_ = new ListNode(x, tail_);
}
else {
node = _find(i - 1);
node->link_ = new ListNode(x, node->link_);
}
size_ += 1;
}
void LList::printlist()
{
ListNode *temp = head_;
while (temp) {
std::cout << temp->item_ << std::endl;
temp = temp->link_;
}
}
ItemType LList::pop(int i)
{
if (i == -1) {
i = size_ - 1;
}
return _delete(i);
}
ItemType& LList::operator[](size_t position)
{
ListNode *node;
node = _find(position);
return node->item_;
}
LList::LList(const LList& source)
{
copy(source);
}
void LList::copy(const LList &source)
{
ListNode *snode, *node;
snode = source.head_;
if (snode) {
node = head_ = new ListNode(snode->item_);
snode = snode->link_;
while (snode) {
node->link_ = new ListNode(snode->item_);
node = node->link_;
snode = snode->link_;
}
}
size_ = source.size_;
}
LList& LList::operator=(const LList& source)
{
dealloc();
copy(source);
}
LList::~LList()
{
dealloc();
}
void LList::dealloc()
{
ListNode *node, *dnode;
node = head_;
while (node) {
dnode = node;
node = node->link_;
delete dnode;
}
}
int main()
{
LList mylist;
mylist.append(6);
mylist.append(3);
mylist.append(1);
mylist.printlist();
mylist.insert(2, 4);
mylist.printlist();
mylist.pop(1);
mylist.printlist();
mylist.size();
}
Where you have this code:
LList& LList::operator=(const LList& source)
{
dealloc();
copy(source);
}
Change it to this:
LList& LList::operator=(const LList& source)
{
dealloc();
copy(source);
return *this; // Note the addition of this return statement.
}
Explanation:
Here, you're overriding the = operator on LList, and in C++, operator overrides must return a value. At least in the case of operator=, you will most often want to just return the object itself.