for(int i = 0; i < 4; ++i){ // This vrtx_pntr is the prior for new vertex.
//*vrtx_pntr = add_vrtx(vrtx_pntr, i);
vrtx_pntr = add_vrtx(vrtx_pntr, i);
// the prior vertex.
}
modify
vertex add_vrtx(vertex *vrtx_current, int data)
to return pointer
There is an article that you might want to look at. It constructs a linked list template that I think you will like. I haven't tried it, but you should be able to use it for vectors. The article is located at: http://www.cplusplus.com/articles/Lw6AC542/
#include <iostream>
#include <string.h>
#include <stdlib.h>
usingnamespace std;
#define ValueType int
struct vertex {
int data;
vertex * next;
vertex * prev;
};
void init_vertex(struct vertex * vertex, int n){
vertex->data = -1;
vertex->next = NULL;
vertex->prev = NULL;
}
void insert_vertex(struct vertex ** vertex, int n) {
struct vertex * new_vertex = (struct vertex *)malloc(sizeof(struct vertex));
// vertex * new_vertex = new vertex;
(* vertex)->prev = new_vertex;
new_vertex->data = n;
new_vertex->next = *vertex;
*vertex = new_vertex;
}
/*
* delete_this(&head); Maybe better actually..?
* */
void delete_this(struct vertex * vertex){
cout << "TEST";
vertex->next->prev = vertex->next;
vertex->prev->next = vertex->prev;
delete(vertex);
// kill(head);
}
void display(struct vertex * head) {
cout << "List: " ;
vertex * list = head;
while(list) {
cout << list->data << " ";
list = list->next;
}
cout << "\nList end\n";
}
int main(){
// Init a linked list for uncoloured vertices.
// struct vertex *newHead;
struct vertex * head = (struct vertex *)malloc(sizeof(struct vertex));
// create a null head node.
init_vertex(head,-1);
//Make a test list by inserting nodes
for(int i=1; i<3; ++i){
insert_vertex(&head,i); // Works
}
display(head);
while(head) {
if(head->data > 0){
cout << "\nIndex in array: " << head->data << "";
// remove the current node from list NOT WORKING
delete_this(head);
}
head = head->next;
}
return 0;
}
The program breaks down with a segmentation error in the delete loop. Deleting nodes in a double linked list, how is it done? Please feel free to point out issues.