Tuesday, 20 August 2013

C++ pointer return struct implementation : Linked List

C++ pointer return struct implementation : Linked List

I know that the code below is quite simple, but I've been stuck on how to
implement it for proper output. Feel so frustrated.
struct node
{
node* p_next;
int p_data;
node(node* head, int data)
{
p_next = head;
p_data = data;
}
explicit node(int data)
{
node(nullptr, data);
}
};
So I have this struct in C++ to construct some linked list.
Then I have insert function to insert some node to that linked list
node* insert_node(node* head, int data)
{
return new node(head, data);
}
Here I start getting dumb. How do I actually make some linked list with
actual values out of this? I was confused how to construct a list first
and add some values.
I've been trying the following but get errors.
struct node node_01(1);
node* node_ptr_01 = new node(1);
What I want to do.
Create a head node with a value 10
Keep adding values for other nodes with 20, 34, 32, 123, etc... random
values for node
I do not know how to approach to initialize the pointer for head and add
values on them.
Please help me. I would greatly appreciate it.

No comments:

Post a Comment