create a doubly linked list in C++ - c++

I'm creating a programme that needs to read data from a spreadsheet in CSV form and assigns it to a doubly linked list in C++.I have created a singly linked list but I'm at a loss as to how to use this idea to make a doubly linked list. I understand you require a previous pointer but I am unsure as to actually implementing the code.
code for my singly linked list:
to add to the list:
if (!m_head)
{
m_head = new Node(name, reference,latitude,longitude);
}
else
{
Node *current = m_head;
while (current->getNext() != 0)
{
current = current->getNext();
}
current->setNext(new Node(name, reference,latitude,longitude));
}
please note: Node is a separate class to store data about the node e.g. name.

Each List Node must a have a pointer to the previous and to the next List Node.
The List is then the container of List Nodes , linked together as a chain.
struct ListNode;
typedef struct ListNode {
struct ListNode *next;
struct ListNode *prev;
void *value;
} ListNode;
typedef struct List {
int count;
ListNode *first;
ListNode *last;
} List;
You need then to implement the methods push and pop accordingly.
The tricky part is the remove method. Store the two pointers of the previous and next Node that you want to delete, and then assign them accordingly to the previous and following node:
ListNode *after = node->next;
ListNode *before = node->prev;
after->prev = before;
before->next = after;
This article may help you, full code and explanation
http://c.learncodethehardway.org/book/ex32.html

Related

How to create nodes in c++?

Write a function to insert a node to the head of the linked list. The function takes two arguments: a pointer to the first node in the double linked list and a string value. It should create a new node with the given value to the head of the double linked list.
This is the question I need to answer. I'm not looking for the answer I'm just unsure how to create a node in C++.
That should be what you wanted.
struct node{
node * next;
std::string value;
};
node * addToTheFront(node * front, std::string value){
node * temp = new node;
temp->next = front;
temp->value = value;
return temp;
}

Dereferencing head of empty linked list for next node

I was trying to implement linked list in C++, when this idea struck my mind. With standard node definition as
class node {
public:
int data;
node *next;
};
I created an empty list node *head; then tried this
if(head->next == nullptr)
cout<<"Stores nullptr";
if(! head->next)
cout<<"Returns bool values";
But there is no output, so what is stored inside head->next ?
First of all, you should create some space/allocate memory for the node class in main.
Note that
node *head; is only a declaration not a definition. For further details have a look at What is the difference between a definition and a declaration?.
You allocate space for the object
Initialize its values, to be more elegant define a constructor method
node *head = new node;
head->next = nullptr;
head->data=0;
I would still consider this as a duplicate of Linked lists in C++
If you declare node *head;, head is an uninitialized pointer that contains a random address. Dereferencing it is Undefined Behavior?
You need to a) initialize head: node *head = nullptr;, and b) test for that condition: if (head == nullptr) { head = new node; node->head = nullptr; ...

Program is crashing from creating push function for Linked List

I am trying to implement a stack using a Linked List. My program keeps crashing and when trying to print the new Linked List, it prints an unsigned integer. My print function works fine, so it is this function below.
Please help.
void LinkedList::Push (int val)
{
Node* newHead = new Node;
Node* oldHead = new Node;
newHead->value = val;
oldHead = head;
head = newHead;
oldHead->prev = head;
head->next = oldHead;
delete newHead;
}
One issue is that the Node that you've newed in the definition of oldHead is never deleted. Since you set oldHead to head immediately after creating it, I would suggest this as your definition:
Node* oldHead = head;
The main issue, though, is that you delete newHead, which is now what head points to. Therefore, when you go to print head, you are reading invalid data.
I would highly recommend leaving the resource handling to objects like std::shared_ptr instead of newing and deleteing yourself.
I'm not sure I understood your question.
Your fixed method:
void Push( const int val )
{
Node* newNode { new Node };
newNode->value = val;
newNode->next = head;
head = newNode;
}
Read more about Linked List operations here. You do not need a doubly linked list to implement a stack - you only need to push/pop at one end.
[EDIT]
I didn't notice you are using a doubly linked list (this is why a complete/verifiable example is required). As I said, for a stack implementation, a singly linked list is enough.

Nested Doubly Linked Lists in C++

What is the best way to go about storing doubly linked lists inside of one doubly linked list? Preferably, I would like to use only one struct, like so:
struct node{
string data;
node* next = NULL;
node* prev = NULL;
};
and use this to be able to store a doubly linked list inside of a doubly linked list to hold all of my doubly linked lists.
I have an ordinary insert function, but that wouldn't work because the parameters are (node*& start, string data). So I've created another insertion method that attempts to store a doubly linked list into a node of a larger doubly linked list, but it gets cloudy from there. Any help would be appreciated.
Your node consists of data and links.
You could insert a linked list as part of the data section:
struct Node
{
std::string Data
std::list</*...*/> nested_list;
Node * previous;
Node * next;
};
If you don't like using std::list you could go with this:
struct Nested_Node
{
Nested_Node * previous;
Nested_Node * next;
/* add data if necessary */
};
struct Node
{
std::string data;
Nested_Node * nested_head;
Nested_Node * nested_tail;
Node * previous;
Node * next;
};
The above structures depend on how you want your nested linked list organized.
Each Node contains the head and tail of a linked list. If you declare the nested list as a separate structure, you could replace the Nested_Node pointers with the list class.

Creating a new instance of a class for a doubly linked list

I have been provided with a starter code for creating a doubly linked list. The problem I'm having is implementing a function that inserts a newly created node at the 'head'.
A node in the linked list is the following struct:
template <class T>
struct ListItem
{
T value;
ListItem<T> *next;
ListItem<T> *prev;
ListItem(T theVal)
{
this->value = theVal;
this->next = NULL;
this->prev = NULL;
}
};
The code for insertion at head is as under:
void List<T>::insertAtHead(T item)
{
ListItem<int> a(item); //create a node with the specified value
if(head==NULL)
{
head=&a; //When the list is empty, set the head
//to the address of the node
}
else
{
//when the list is not empty, do the following.
head->prev=&a;
a.next=head;
head=&a;
}
}
Now the problem is, when I'm supposed to create a new class object with a different memory address whenever I insert an item. What I'm doing above updates the same memory location.
I need to know how to create a new class object.
What you are doing is wrong, and potentially dangerous (using pointers to local variables). You need to allocate a new node with the new expression:
ListItem<int>* a = new ListItem<int>(item);
Of course, when done with the list, you have to remember to free the memory with delete.