I am learning how to program in C++, and have looked at linked lists. I have found many code snippets to get me started.
The code I am playing with is from studytonight. I understand how to insert nodes into a list. But what if I want to create a linked list and fill x-many nodes at once in a loop? The for
loop in my main()
works fine, but when I try turning it into a function, such as arrayToList()
, I am running into problems.
The function would simply use the entries of an array and turn them into the data (like an index) and a second value stored in the node. Obviously, the function needs to return a list to continue any manipulation, like deleting or replacing, etc, but also needs to receive a list as a parameter as the created list inside won't exist outside this function. The call to traverse the list inside the for
loop is just to highlight that inside the function that list isn't empty.
I am a little lost as to make the function work, and would appreciate any help regarding a method to iteratively add x-many at once nodes to a linked list. I hope I have explained my problem well enough.
#include <iostream>
#include <stdlib.h>
using namespace std;
struct node
{
int data; // Data
node *prev; // A reference to the previous node
node *next; // A reference to the next node
double x;
};
class Doubly_Linked_List
{
node *front; // points to first node of list
node *end; // points to first las of list
public:
Doubly_Linked_List()
{
front = NULL;
end = NULL;
}
void add_front(int, double );
void forward_traverse();
void arrayToList(int[], int);
};
void Doubly_Linked_List :: add_front(int d, double x)
{
// Creating new node
node *temp;
temp = new node();
temp->data = d;
temp->x = x;
temp->prev = NULL;
temp->next = front;
// List is empty
if(front == NULL)
end = temp;
else
front->prev = temp;
front = temp;
}
void Doubly_Linked_List :: forward_traverse()
{
node *trav;
trav = front;
while(trav != NULL)
{
cout<<trav->data<<" (" << trav->x << " )" << endl;
trav = trav->next;
}
}
void Doubly_Linked_List :: arrayToList(int arr[], int n)
{
Doubly_Linked_List list;
for(int i=n-1; i>=0;i--)
{
cout << i << endl;
list.add_front(arr[i], arr[i]);
list.forward_traverse();
}
}
int main()
{
Doubly_Linked_List list;
int arr[] = { 1, 2, 3, 4, 5 };
int n = sizeof(arr) / sizeof(arr[0]);
// for (int i= 1; i< 10; i++)
// {
// list.add_front(i, i*2);
// }
list.arrayToList(arr, n);
cout << "after filling arrayList " << endl;
list.forward_traverse();
}