1

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;}
}

3 Answers 3

2

I think you just need to include the empty method in your class:

bool Dict::empty()
{
    return head->next==tail;
}
Sign up to request clarification or add additional context in comments.

3 Comments

OK. but can u tell me why this is not working? ( if you want book , download: temp-share.com/show/KdP03058h This is given on page no. 123 of PDF)
The reason you were getting the message head and tail are out of scope is because you were trying to access them outside of the appropriate class. the empty method belongs in the Dict class (which should probably be renamed to DoublyLinkedList or something similar)
Sorry for strange name. The name was part of my assignment. Thanks for quick help.
1

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;
}

10 Comments

Also, if head->next==tail, you clearly have a "head" node, and do not have an empty list.
This is method by creating two nodes for head and tail without containing data. They contain only head->next and tail->prev
Yeah, it looks like he's not understanding the concept of null. that there's got to be something in those heads, so if it's the same as the tail, it's got to be that empty thing I put in there to begin with.
So do not include head and tail nodes in list.
Some of these comments (@ChrisCM) seem like they belong on the OP?
|
0
  • There are a couple of things you need to work on. First you did not include your node.cpp there is only node.h, what does your node constructor do? On the Dict() constructor you should call node constructor i.e node() this will initialize the node class to whatever your node constructor does i.e set the variable string data to some input value.
  • What is the empty() output. According to your definition. empty() method, is doing the same thing, checking if head->next is sitting at the same memory location with the tail. I dont think thats 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".
  • To create a list of nodes or linked list, You need to define ADD method, this method should be able to add a new node or link the current node with the next node.
  • You also need to define remove node, that removes or delete any given node. This will probably be the hardest method you have written so far.

1 Comment

Well thank you for suggestions but this is not complete code. To know complete code downlaod book: download from : temp-share.com/show/KdP03058h) This is given on page no. 123 of PDF

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.