I want to implement insertion of element at tail of linked list, I want to do it using member function , currently I'm able to do it by creating a function outside of struct, please help me what should be modified to implement it as member function.
my conventional approach:
#include<iostream>
using namespace std;
struct node{
int data;
node *next;
};
node * Insert(int v,node *head){
if(head==NULL){
node *temp=new node;
head=temp;
temp->data=v;
temp->next=NULL;
return head;
}
else{
node *temp=head;
while(temp->next!=NULL){
temp=temp->next;
}
node *temp_new=new node;
temp_new->data=v;
temp_new->next=NULL;
temp->next=temp_new;
return head;
}
}
This is a working example. The idea is that, from the member function, you recursively call node::Insert
until you reach the last element, and then only there you create the next one.
#include <iostream>
struct node{
int data;
node *next = nullptr;
node *Insert(int v);
};
node *node::Insert(int v)
{
if (next == nullptr)
{
next = new node;
next->data = v;
return next;
}
else
{
return next->Insert(v);
}
}
int main()
{
node a{3};
a.Insert(2);
a.Insert(5);
std::cout << a.data << ' ' << a.next->data << ' ' << a.next->next->data << ' ' << std::endl;
return 0;
}
See it live on Coliru .
Also: avoid using namespace std
.
Addition
As noted in the comment, it is probably a good idea to unroll the recursion. Here is a nonrecursive version of the above code:
#include <iostream>
struct node{
int data;
node *next = nullptr;
node *Insert(int v);
};
node *node::Insert(int v)
{
if (next == nullptr)
{
next = new node;
next->data = v;
return next;
}
else
{
auto p = next;
while (p->next != nullptr)
p = p->next;
p->next = new node;
p = p->next;
p->data = v;
return p;
}
}
int main()
{
node a{3};
a.Insert(2);
a.Insert(5);
std::cout << a.data << ' ' << a.next->data << ' ' << a.next->next->data << ' ' << std::endl;
return 0;
}
See it live on Coliru .
Just by looking at what your insert
currently does, it does not make sense to make it a member of node
. You can call your insert
like this:
node* root = nullptr;
root = Insert(42,root);
to create a list with a single element. On the other hand, you cannot call a member function of node
before you have an instance of type node
.
The transition is simpler if you write a class that represents the list, not only a single node. Start with this:
struct node{
int data;
node *next;
};
struct linked_list {
node* head;
};
Now the above two lines could look like this:
linked_list l;
l.insert(42);
All you need to do is to move the free function into the class and instead of using the parameter head
you use the member head
. Also there is no need to return the head anymore:
#include<iostream>
using namespace std;
struct node{
int data;
node *next;
};
struct linked_list {
node* head = nullptr;
void Insert(int v){
if(head==nullptr){
head =new node;
head->data=v;
head->next=nullptr;
}
else{
node *temp=head;
while(temp->next!=nullptr){
temp=temp->next;
}
node *temp_new=new node;
temp_new->data=v;
temp_new->next=nullptr;
temp->next=temp_new;
}
}
};
Note that I did not change anything on the implementation (only NULL
-> nullptr
and void
return, if it had a bug then it still has a bug now ;). You should also provide appropriate constructors for node
and linked_list
and destructors, because currently this leaks all allocated memory. And last but not least you need to read about the rule of 3/5: What is The Rule of Three?
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.