Linked lists - trying to revive...
I'm trying to set up a simple implementation of a double linked list. I can't make it fly. Here's my code:
Code:
#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 delete_vrtx(vertex vrtx_pntr){
vrtx_pntr->piror->next = vrtx_pntr->next; NOTE: FIND OUT IF SYNTAX WORKS
vrtx_pntr->next->prior = vrtx_pntr->prior;
return;
}
*/
/*
* add_node()
* params
* vertex - use current to set prior
* int - data.
**/
vertex add_vrtx(vertex *prior_vrtx, int data){
struct vertex *new_vrtx = (struct vertex *) malloc( sizeof( struct vertex) );
new_vrtx->next = 0;
new_vrtx->prior = prior_vrtx;
new_vrtx->id = data;
return *new_vrtx;
}
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; // pointer to first vertex.
for(int i = 0; i < 3; ++i){ // Trying to connect more nodes into my list.
*vrtx_pntr = add_vrtx(vrtx_pntr, i); // the prior vertex.
}
vrtx_pntr = root;
if( vrtx_pntr != 0){
cout << "Test: root exists.\n";
while( vrtx_pntr->next != 0 ){
cout << "Test: Nodes exists.\n"; // Won't print out this.
cout << "Sweep: " << vrtx_pntr->id << "\n";
}
}
return 0;
}
I seem to create a root vertex, but I can't establish if I connect sub nodes to my list. Please help me and bear in mind that I haven't really code stuff since I was a student...