I have a recommendation. That is to make yourself one function that creates a new node item. Currently you have the code essentially duplicated in two places and that is not a good idea. Try something like this :
node * createNode()
{
cout << "enter data : ";
int data;
cin >> data;
node* newnode = new node;
newnode->data = data;
return newnode;
}
and then
pushhead
becomes :
node* pushhead()
{
node* newnode = createNode();
newnode->next = head;
head = newnode;
return head;
}
The node class needs a constructor :
class node
{
public:
int data;
node* next;
node* prev;
public:
node() {
data = 0;
next = nullptr;
prev = nullptr;
}
};
Another thing : you really don't need the head node to be a global variable. You can pass it where it's needed and if it needs to be modified then you can pass its address or a reference to it. For example,
pushhead
would be modified to look like this :
node * pushhead( node ** phead )
{
node* newnode = createNode();
newnode->next = * phead; * phead = newnode; return newnode;
}
The function
pushhead
doesn't look right to me. With a doubly-linked list the previous node item should be set but in that function it is never set it. It is always null. It is set in
pushtail
but I think it should also be set in
pushhead
. In
pushhead
, the old list head's previous will be the new node. That is, in a typical doubly-linked list implementation it is. Note that you set both next and previous item pointers in
pushtail
but not in
pushhead
so I think you should do it in
pushhead
also.