Start New Discussion within our Software Development Community

Can't find out the error.

#include <iostream>
using namespace std;

struct node{
    int id;
    int *next;
};

node * get_data(int );
node * get_data(void);
void insert_node (node **, node **, node *);
void display_data(node *);

int main(){
    node *head=0, *tail=0, *newPtr=0;
    newPtr = get_data();
    insert_node (&head,&tail,newPtr);
    display_data (head);
    return 0;
}

node * get_data(void){
    node *newPtr = new node;
    newPtr->id = 1;
    newPtr->next = NULL;
    return newPtr;
}

void insert_node (node **h, node **t, node *newPtr){
    if (*h!=NULL && *t!=NULL){
    newPtr = (*h)->next;
    *h = newPtr;
    }
    else{
        *h = newPtr;
        *t = newPtr;
    }
}

void display_data(node *p){
    while (p!=NULL){
        cout << p->id << endl;
        p = p->next;
    }
}

I don't know about you, but the error I got was complaining that your node structure is incorrect. The next node should be a pointer to node, not a pointer to int:

struct node{
    int id;
    node *next;
};

I didn't look any further since that right there was a deal breaker for the program. Apply the fix and see if it helps.

This question has already been answered. Start a new discussion instead.