Re: Problems with liked list
Usually head would be a member of Solution then .insert() and .display() then doesn't require head as a parameter. When my VS has finished installing update 3 I'll post some example code.
Re: Problems with liked list
Consider
Code:
#include <iostream>
using namespace std;
class Node
{
private:
int data;
Node *next;
public:
Node(int d) : data(d), next(nullptr) {}
Node* getNext() const
{
return next;
}
void setNext(Node* const n)
{
next = n;
}
int getData() const
{
return data;
}
};
class Solution
{
private:
Node* head = nullptr;
public:
void insert(int data)
{
if (head == nullptr)
head = new Node(data);
else
{
Node *curr = head;
while (curr->getNext() != nullptr)
curr = curr->getNext();
curr->setNext(new Node(data));
}
return;
}
void display() const
{
for (Node* start = head; start; start = start->getNext())
cout << start->getData() << " ";
}
};
int main()
{
Solution mylist;
int T, data;
cin >> T;
while (T--)
{
cin >> data;
mylist.insert(data);
}
mylist.display();
}
First note that the class Solution doesn't have a destructor which it should have to free the allocated memory for the list.
This is a single forward linked list where each node points to the following node in the list. Nodes are added at the end of the list. A node consists of the data and a pointer to the memory address of the next node in the linked list. If there is no next node (ie it is the end node of the list) then the next node is nullptr. There is a pointer called head that points to the first node in the linked list. If the linked list is empty then head will be nullptr.
At first, the linked list is empty so head is nullptr. When a node is added to the list, head is nullptr is head is set to point to the first allocated node of the list. If head is not nullptr when a node is added then this means that there is already at least one node in the linked list. Therefore to add a new node at the end of the list the last node in the list needs to be determined. So the code loops through all the nodes from the one pointed to by head by following the link to the next node until the next link is nullptr. At this point we have the last node in the linked list. This next element of the last node is then set to point to the newly allocated node.
Adding a new node at the end this way is expensive as for each insertion the end of the list has to be found. A better way is to main a pointer that points to the end node of the list (similar to how head points to the start of the list). This way an insertion can be done immediately without having to first traverse the list.
Re: Problems with liked list
Quote:
Originally Posted by
2kaud
Consider
Code:
#include <iostream>
using namespace std;
class Node
{
private:
int data;
Node *next;
public:
Node(int d) : data(d), next(nullptr) {}
Node* getNext() const
{
return next;
}
void setNext(Node* const n)
{
next = n;
}
int getData() const
{
return data;
}
};
class Solution
{
private:
Node* head = nullptr;
public:
void insert(int data)
{
if (head == nullptr)
head = new Node(data);
else
{
Node *curr = head;
while (curr->getNext() != nullptr)
curr = curr->getNext();
curr->setNext(new Node(data));
}
return;
}
void display() const
{
for (Node* start = head; start; start = start->getNext())
cout << start->getData() << " ";
}
};
int main()
{
Solution mylist;
int T, data;
cin >> T;
while (T--)
{
cin >> data;
mylist.insert(data);
}
mylist.display();
}
First note that the class Solution doesn't have a destructor which it should have to free the allocated memory for the list.
This is a single forward linked list where each node points to the following node in the list. Nodes are added at the end of the list. A node consists of the data and a pointer to the memory address of the next node in the linked list. If there is no next node (ie it is the end node of the list) then the next node is nullptr. There is a pointer called head that points to the first node in the linked list. If the linked list is empty then head will be nullptr.
At first, the linked list is empty so head is nullptr. When a node is added to the list, head is nullptr is head is set to point to the first allocated node of the list. If head is not nullptr when a node is added then this means that there is already at least one node in the linked list. Therefore to add a new node at the end of the list the last node in the list needs to be determined. So the code loops through all the nodes from the one pointed to by head by following the link to the next node until the next link is nullptr. At this point we have the last node in the linked list. This next element of the last node is then set to point to the newly allocated node.
Adding a new node at the end this way is expensive as for each insertion the end of the list has to be found. A better way is to main a pointer that points to the end node of the list (similar to how head points to the start of the list). This way an insertion can be done immediately without having to first traverse the list.
But my problem is that when the head is not null,then according to the .insert function,we set a curr pointer points to where head points,right?So what is curr->next now? Because we I need to judge if it is null, then if it is not null, by " curr = curr->getNext(); " ,we make curr points to where curr->next points to,am I right?
Re: Problems with liked list
Quote:
we set a curr pointer points to where head points,right
Yes.
Quote:
So what is curr->next now
This points to the address of the next node in the list, or nullptr if there is no next node.
Quote:
if it is not null, by " curr = curr->getNext(); " ,we make curr points to where curr->next points to
Yes.