I'm building my own linked list class and I'm having some issues figuring out how to write some functions to help me traverse this list. This is my first time building a linked list from scratch, so if my approach is unconventional please let me know what might be more conventional.
I'd like write a function, within the List class that allows me to increment to the next element called getNext() as well as one that getPrev();
I wrote getNext like this:
T* getNext(){return next;}
However it tells me next is not declared within the scope. I'd also like to write a function that lets me access and modify the object within the list. I was considering using the bracket operator, but first I need to write a function to return the data member. Perhaps If I take a similar approach as I did within my pop functions.. thinking about it now. However, I'd still appreciate any advice.
Here is my List class:
#ifndef LIST_H
#define LIST_H
//List Class
template <class T>
class List{
struct Node {
T data;
Node *next;
Node *prev;
//Constructs Node Element
Node(T t, Node* p, Node* n) { data = (t); prev = (p); next = (n); }
// T *getNext() {return next;}
};
Node *head;
Node *tail;
public:
//Constructor
List() { head = NULL; tail=NULL; }
//Destructor
~List() {
while(head){
Node * temp(head);
head = head->next;
delete temp;
}
}
//is empty
bool empty() const {return (!head || !tail ); }
operator bool() const {return !empty(); }
//Push back
void push_back(T data) {
tail = new Node(data, tail, NULL);
if(tail->prev) //if the node in front of tail is initilized
tail->prev->next = tail;
if( empty() )
head = tail;
}
//Push front
void push_front(T data) {
head = new Node(data, NULL, head);
if(head->next)//if the node following head is initilized
head->next->prev = head;
if( empty() )
tail = head;
};
T pop_back() {
if( empty() )
throw("Error in List: List is empty\n");
Node* temp(tail);
T data(tail->data);
tail = tail->prev;
if( tail )
tail->next = NULL;
else
head = NULL;
delete temp;
return data;
}
T pop_front() {
if (empty())
throw("Error in List: List is empty\n");
Node* temp(head);
T data(head->data);
head = head->next;
if(head)
head->prev=NULL;
else
tail = NULL;
delete temp;
return data;
}
T getNext(){return next;}
};
#endif
getNext should be part of the struct Node and return a Node*
Node* getNext() { return next; }
Then from that you can get the value.
If you have to have it part of the list itself, which I would not recommend it will need to take a parameter of what Node you would like the next of:
Node* getNext(Node* n) {return n->next;}
Again, I recommend the first option.
Here is an approximate whole class with both of these:
template<typename T>
class List {
public:
struct Node {
Node* next, prev;
T data;
//some constructor and stuff
Node* Next() {return next;}
}
//some constructors and other functions
Node* getNext(Node* _n) {return _n->Next();}
}
then to use:
int main() {
List<int> l;
//add some stuff to the list
//get the head of the list
List<int>::Node* head = l.head; //or some corresponding function
//then
List<int>::Node* next = head->Next();
//or
List<int>::Node* next2 = l.getNext(head);
}
for starters getNext() should not return a pointer to the template class, it should return a pointer to the Node structure.
So it should be
Node* getNext(){return next;}
Because it's a member of Node struct and getNext is member of List. You should access it from an object of type Node.
Related
I read some of the other posts on this topic because there were quite a few, but they didn't really help my situation.
I am getting memory leaks in my implementation of a doubly linked list. I have to make my own so using list is not an option.
here are the two push functions I am using...
template <class T>
void dllist<T>::push_front(T val) {
node* new_node = new node;
new_node->value = val;
new_node->forward = head;
new_node->backward = nullptr;
if (head != nullptr)
head->backward = new_node;
head = new_node;
}
and...
template <class T>
void dllist<T>::push_back(T val) {
node* new_node = new node;
new_node->value = val;
new_node->forward = nullptr;
if (!head)
head = new_node;
else {
node* traveller = head;
while (traveller->forward != nullptr)
traveller = traveller->forward;
traveller->forward = new_node;
new_node->backward = traveller;
}
}
finally, here is my destructor
template <class T>
dllist<T>::~dllist() {
node* current = head;
while (current != nullptr) {
node* forward = current->forward;
delete current;
current = forward;
}
}
In main, I declare an object of type dllist called mylist and I make a few calls to push_front with some integer values and then push_back.
I am using the CRT library to check for leaks and there is a leak at each call to push_back or push_front.
I am confused because I thought I made my destructor correctly. Is there something else Im not seeing?
If anyone could point me in the right direction I'd appreciate it!
Thanks.
MRE
template<class T>
class dllist {
struct node {
T value;
node* forward;
node* backward;
};
node* head;
public:
dllist(); // default constructor
~dllist(); // default destructor
void push_front(T); // push element to the front of the list
void push_back(T); // push element to the back of the list
};
int main() {
{
dllist<int> mylist;
mylist.push_front(10);
mylist.push_front(12);
mylist.push_front(14);
mylist.push_front(16);
mylist.push_front(18);
mylist.push_front(19);
mylist.push_back(11);
mylist.push_back(21);
mylist.push_back(31);
mylist.push_back(41);
mylist.push_back(31);
mylist.push_back(41);
mylist.push_back(222);
}
_CrtDumpMemoryLeaks();
return 0;
}
template <class T>
dllist<T>::dllist() {
head = nullptr;
}
I have a linked list and I need to insert a node at the location where the "iterator" is.
below is the insert function(which i have templated):
template <class T>
void List<T>::insert(T x)
{
if (size=0)
{
cout << "adding postion to head because empty list" << endl;
NodeRef newNode = new Node(x);
tail = head = newNode;
}
else
{
NodeRef temp = new Node(x);
temp->previous = iterator;
iterator->next = iterator;
}
}
Below is the linked list class (which i have also templated):
class List
{
private:
struct Node
{
T data;
Node* next;
Node* previous;
Node() : next(NULL), previous(NULL) {} //define our own default constuctor
Node(T data) : next(NULL), previous(NULL), data(data) {}
};
typedef struct Node* NodeRef;
NodeRef head;
NodeRef tail;
NodeRef iterator; //points to one node at a time
NodeRef current1;//temp
int size;
public:
void insert(T);Inserts a new element into the list in the position after the "iterator"
void scroll() {iterator = iterator->next;}
when i call the insert function, i run into problems. the scroll function works fine.
iterator->next = iterator is incorrect and creates a loop. Since the previous line is temp->previous = iterator, iterator->next should point back at temp. Once you've updated both links, you should have n->next->prev == n for any valid node in the list.
I have the following code to insert in the bst however, it fails to insert all the nodes except for the root. Any idea what I am doing wrong?
class Node
{
public:
int data;
Node* right;
Node* left;
Node(int data)
{
this->data = data;
}
Node() {}
};
class BST
{
public:
Node* head;
void insert(int data)
{
if (head == nullptr)
{
head = new Node(data);
head->data = data;
}
else
{
// head = new Node(data);
insertNode(data, head);
}
}
void insertNode(int data, Node* head)
{
if (head == nullptr)
{
head = new Node(data);
return;
}
if (head)
{
Node* temp = head;
if (temp->data > data)
{
insertNode(data, temp->left);
}
else if (temp->data <= data)
insertNode(data, temp->right);
}
}
};
The parameter head in insertNode shadows the member variable named head.
However, while that's a really bad practice, the other answer is the true reason for your error, so please select his answer instead (once you get it working, of course).
I'd recommend changing the signature of insertNode to
void insertNode(int data, Node*& node)
Also, you don't need to check for head == nullptr in insert. You have a duplicate check in insertNode
So insert could look like this:
void insert(data) {
insertNode(data, head);
}
Finally, you're not initializing head within the constructor. It's possible that head will be initialized to something other than nullptr, especially if you compile this in release mode. Add a constructor like this:
BST() : head(nullptr) {
// Other init stuff here if necessary
}
You'll also want to make Node* head a private data member instead of public.
insertNode() takes a copy of the pointer, so changes made inside the function have no effect on the actual pointer in the tree. What you want to do is take a reference to the pointer:
void insertNode(int data, Node*& head)
In your function " insertNode" you are using if(head) , this if will work only if head == 1 , and head is never equals to 1 because its a pointer , so this "if" is not working.!
I am very new to C++ templates. I am currently working on a project where I need to implement a Doubly Linked List using a template. Here is currently what I have so far:
template<class ItemType>
class SortedList
{
public:
SortedList();
~SortedList();
bool Insert (ItemType toAdd);
bool Delete (ItemType toDelete);
void Print();
private:
SortedList ( const SortedList & copyFrom );
SortedList & operator= ( const SortedList & assignFrom );
struct Node
{
Node ( ItemType item, Node * p = NULL, Node * n = NULL )
{ data = item; prev = p; next = n; }
ItemType data;
Node * prev, * next;
};
Node * list;
};
template<class ItemType>
SortedList<ItemType>::SortedList()
{
list == NULL;
}
template<class ItemType>
SortedList<ItemType>::~SortedList()
{
Node * curr = list;
while ( curr != NULL )
{
Node * tempNext = curr->next;
delete current;
current = tempNext;
}
}
However, in my destructor for example, why can't I access the node elements? The code that is inside that method right now compiled, but does not throw errors. However if I try to use -> on curr, next or prev do not appear. Why do I not have access to these? I feel like I am missing something very obvious here to get started.
Also, how can I initialize list == NULL in the function head, instead of doing it outside of the class?
Don't know why it compiles, but you are using
delete current;
current = tempNext;
Instead of:
delete curr;
curr = tempNext;
Use inline initializer syntax:
class SortedList
{
public:
SortedList()
:
list(nullptr)
{ }
Use the same syntax to initialize Node (it's more optimal)
I am new to templates in C++ and am working on a project where I need to implement a Doubly Linked List using a template. However, I can't seem to access the node elements next and previous.
For example, in my destructor, I cannot use curr-> to bring up my options of using next or prev. IntelliSense just says, "No members available." Also, I can only find errors during build time...no red lines, warnings, anything appear beforehand. I am curious as to why this is not working....is it a bug or intended? If it is, where is my template incorrect thus far?
template<class ItemType>
class SortedList
{
public:
SortedList();
~SortedList();
bool Insert (ItemType toAdd);
bool Delete (ItemType toDelete);
void Print();
private:
SortedList ( const SortedList & copyFrom );
SortedList & operator= ( const SortedList & assignFrom );
struct Node
{
Node ( ItemType item, Node * p = NULL, Node * n = NULL )
{ data = item; prev = p; next = n; }
ItemType data;
Node * prev, * next;
};
Node * list;
};
template<class ItemType>
SortedList<ItemType>::SortedList()
{
list = NULL;
}
template<class ItemType>
SortedList<ItemType>::~SortedList()
{
Node * curr = list;
while ( curr != NULL )
{
Node * tempNext = curr->next;
delete curr;
curr = tempNext;
}
}
Put the Node struct out of the SortedList like this:
template<typename ItemType>
struct Node
{
Node(ItemType item, Node * p = NULL, Node * n = NULL)
{
data = item; prev = p; next = n;
}
ItemType data;
Node * prev, *next;
};
and then instantiate the template (create the type) inside SortedList like this:
template<class ItemType>
class SortedList
{
public:
//... More code here.
private:
// ... More code here.
Node<ItemType> * list;
};
template<class ItemType>
SortedList<ItemType>::SortedList()
{
list = NULL;
}
template<class ItemType>
SortedList<ItemType>::~SortedList()
{
Node<ItemType> *curr = list;
while (curr != NULL)
{
Node * tempNext = curr->next; // Now this will work.
delete curr;
curr = tempNext;
}
}
The logic I have followed
A template, is not a type. You get the type instantiating the template. Hence the type Node Does not exists until you instantiate the template StortedList since the former is inside the latter.
Also the exact type for Node would be SortedList<ItemType>::Node, and there you can see you can't talk about Node, before compiling the code. Thats why IntelliSense don't "see it".