Dynamically allocating memory...
So i wrote a function which creates a circularly linked list. But for some reason it cant allocate memory dynamically =(
What am i doin wrong?
Code:
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;
struct node
{
int data;
node *next;
node *prev;
};
struct circle
{
node *start;
};
void createCircle(circle *circ, vector<int> v);
int main()
{
//Main
circle first;
first = new circle;
vector<int> numbers;
for(int i=0; i<10; i++)
{
numbers.push_back(i+1);
}
createCircle(first, numbers);
cin.ignore();
//End
return 0;
}
void createCircle(circle *circ, vector<int> v)
{
//create intitial node
node *start;
start = new node;
start->data = v.at(0);
//create the rest of the nodes
int size = v.size();
size--;
//data
node *nodes[size];
for(size; size>0; size--)
{
nodes[size] = new node;
}
size = v.size();
size--;
//Insert Data
for(int i=0; i<size-1; i++)
{
nodes[i]->data = v.at(i);
}
//Connect nodes
circ->start = start;
start->next = nodes[0];
for(int i=1; i<size-1; i++)
{
nodes[i-1]->next = nodes[i];
}
//do prevs
start->prev = nodes[size-1];
for(size-2; size>0; size--)
{
nodes[size]->prev = nodes[size-1];
}
//DONE
}
Thanks!! =D