Hi - more linked lists issues
I am working on a simple prepend function for a doubly linked list - but I have hit a snag. Many of the examples I have seen use a similar algorithm to what I am using here, but I don't understand how to get around the problem when pointing the old head of the list to the new node. I am getting a memory violation issue on that line...
Code:#include <iostream> using namespace std; typedef struct Node { int *data; Node *next; Node *prev; } NODE; // Function prototypes void output_fwd(void); void insert_beg(int *data); // The list NODE *head; NODE *tail; int main(void) { int num; cout << "Enter a number: "; cin >> num; while (num != 999) { insert_beg(&num); cout << "Enter a number: "; cin >> num; } output_fwd(); return 0; } void output_fwd(void) { NODE *current; current = head; while (current != NULL) { cout << "Number: " << *current->data << endl; current = current->next; } } void insert_beg(int *data) { NODE *temp = new NODE; temp->data = new int; *temp->data = *data; temp->next = head; temp->prev = NULL; // This is the problem line.. head->prev = temp; head = temp; }



LinkBack URL
About LinkBacks



