I need clarification I have this assignment in which we have to write the insertion class of a doubly linked list
#include <iostream>
using namespace std;
// list node
struct Node {
int data; // each node holds an integer data
Node* previous; // pointer to the previous node
Node* next; // pointer to the next node
Node(int d=0, Node* prv=NULL, Node* nxt=NULL) : data(d), previous(prv), next(nxt) {}
Node* get_previous() const { return previous; }
Node* get_next() const { return next; }
Node* insert_before(int d); // insert the int before this node
// return a pointer to the inserted node
Node* insert_after(int d); // insert the int after this node
// return a pointer to the inserted node
void delete_before(); // delete the node before this node
void delete_after(); // delete the node after this node
};
// insert the int before this node
// return a pointer to the inserted node
Node* Node::insert_after(int d) {
/* Complete this function */ }
I understand that you would need to know the node before and after to insert it and if you are trying to insert it after the head you need to link them and such. My question is when I tried to put if(header == NULL) I keep getting errors that it wasn't declared and such but in the main it says:
int main() {
// Construct a linked list with header & trailer
cout << "Create a new list" << endl;
Node *header = new Node(-1);
Node *trailer = new Node(-2);
trailer->previous = header;
header->next = trailer;
cout << "list: ";
display_list(header,trailer);
cout << endl;
why do I keep getting error when the main is run it should point where the header is and where I am wrong or misunderstanding?
Aucun commentaire:
Enregistrer un commentaire