Dec 27, 2013 at 11:13pm Dec 27, 2013 at 11:13pm UTC
I'm trying to set up a simple implementation of a double linked list... But I seem to some mistakes. If anyone could tell me what I'm doing wrong...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
#include <iostream>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
using namespace std;
#define ValueType int
struct vertex {
int id;
struct vertex *prior;
struct vertex *next;
};
/*
void del(vertex vrtx_pntr){
vrtx_pntr->piror->next = vrtx_pntr->next;
vrtx_pntr->next->prior = vrtx_pntr->prior;
return;
}
*/
/*
* add_node()
* params
* vertex - use current to set prior
* int - data.
**/
vertex add_vrtx(vertex *vrtx_current, int data){
struct vertex *vrtx_new = (struct vertex *) malloc( sizeof ( struct vertex) );
// Attatch to old vertex
vrtx_current->next = vrtx_new;
// Set up for new vertex
vrtx_new->next = 0;
vrtx_new->prior = vrtx_current;
vrtx_new->id = data;
return *vrtx_new;
}
int main() {
// set up a root.
struct vertex *root = (struct vertex *) malloc( sizeof ( struct vertex) );
root->id = 0; // init all variables to zero.
root->next = 0;
root->prior = 0;
struct vertex *vrtx_pntr = root; // list pointer to first vertex.
for (int i = 0; i < 4; ++i){ // This vrtx_pntr is the prior for new vertex.
*vrtx_pntr = add_vrtx(vrtx_pntr, i); // the prior vertex.
}
vrtx_pntr = root; // reset pointer to root..
if ( vrtx_pntr != 0){
cout << "Test: root exists.\n" ;
while ( vrtx_pntr->next != 0 ){
cout << "Test: Nodes exists.\n" ;
cout << "Sweep: " << vrtx_pntr->id << "\n" ;
}
}
return 0;
}
Edit, I get 'Test: root exists' printed out, but not the loop through my linked list.
Last edited on Dec 27, 2013 at 11:15pm Dec 27, 2013 at 11:15pm UTC
Dec 27, 2013 at 11:21pm Dec 27, 2013 at 11:21pm UTC
You're not iterating through the list.
1 2 3 4 5
while ( vrtx_pntr->next != 0 ){
cout << "Test: Nodes exists.\n" ;
cout << "Sweep: " << vrtx_pntr->id << "\n" ;
vrtx_pntr = vrtx_pntr->next;
}
Last edited on Dec 27, 2013 at 11:21pm Dec 27, 2013 at 11:21pm UTC
Dec 27, 2013 at 11:27pm Dec 27, 2013 at 11:27pm UTC
Thanks for replying!
I see your point, but I still get the same result. I must have missed something else too. :-/
Dec 28, 2013 at 8:34am Dec 28, 2013 at 8:34am UTC
1 2 3 4 5
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
vertex* add_vrtx(vertex *vrtx_current, int data)
Last edited on Dec 28, 2013 at 8:50am Dec 28, 2013 at 8:50am UTC
Dec 31, 2013 at 1:55am Dec 31, 2013 at 1:55am UTC
*head->prev = new_vertex;
Last edited on Dec 31, 2013 at 1:55am Dec 31, 2013 at 1:55am UTC
Dec 31, 2013 at 12:00pm Dec 31, 2013 at 12:00pm UTC
(*head)->prev = new_vertex;
Jan 9, 2014 at 12:17pm Jan 9, 2014 at 12:17pm UTC
If you're deleting the head, then what is the value of vertex->prev
? What will happen when you try to access vertex->prev->next
?
Last edited on Jan 9, 2014 at 12:18pm Jan 9, 2014 at 12:18pm UTC