Reputation: 649
I want to just create the doubly linked list and check if it is empty. Please tell the mistake. ERROR shown is : In function empty(), head and tail are out of scope. Didn't work when define as struct in class Dict.
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
class node
{ public:
string data;
node* next;
node* prev;
friend class Dict;
};
class Dict
{ public:
bool empty();
Dict();
node* head;
node* tail;
};
Dict::Dict()
{ head=new node;
tail= new node;
head->next=tail;
tail->prev=head;
}
bool empty()
{
return head->next==tail;
}
int main()
{
Dict my_list;
if(my_list.empty())
{cout<<"empty list"<<endl;}
else
{cout<<"Not empty"<<endl;}
}
Upvotes: 1
Views: 3368
Reputation: 12604
I think you just need to include the empty method in your class:
bool Dict::empty()
{
return head->next==tail;
}
Upvotes: 2
Reputation: 1389
t think that
s what you want, because it will always return true if you call dict() constructor. If you don`t call dict() it will return false or you might even get an handle error, "null reference".Upvotes: 0
Reputation: 12604
Well, first off, you're not creating an empty list, because your constructor creates new nodes.
In your "empty" method, you're trying to reference a "head" variable definied in the Dict class
possible fix:
Dict::Dict()
{
head = tail = null;
}
bool Dict::empty()
{
return head == null;
}
Upvotes: 1