1

I have tried to implement doubly linked list in C++. Here is the code,

#include <iostream>
using namespace std;
//double linked list
class Link{
    public:
        long data;
        Link *next;

    public:
        Link(long d) {
            data=d;
        }
        void displaylink() {
            cout<<data<<" "<<"\n";
        }
};

class firstl {
    private:
        Link *first;
        Link *last;

    public:
        firstl() {
            first=NULL;
            last=NULL;
        }
    public:
        bool empthy() {
            return (first==NULL);
        }

    public:
        void insertfirst(long dd) {
            Link *newlink=new Link(dd);
            if (empthy)
                last=newlink;
            newlink->next=first;
            first=newlink;
        }

    public :
        void insertlast(long dd) {
            Link *newlink=new Link(dd);
            if (empthy)
                 first=newlink;
            else
                last->next=newlink;
            last=newlink;
        }


    public :
        long deletefirst() {
            long temp=first->data;
            if (first->next==NULL) //if only one item
                last=NULL;//null<-last;
            first=first->next; //first-->old next;
            return temp;
        }
    public:
        void displaylist() {
            Link *current=first;
            while (current!=NULL) {
                current->displaylink();
                current=current->next;
            }
        }
};

int main() {
    firstl linked;
    linked.insertfirst(22);
    linked.insertfirst(44);
    linked.insertfirst(66);
    linked.insertlast(11);
    linked.insertlast(33);
    linked.insertlast(55);
    linked.displaylist();
    linked.deletefirst();
    linked.deletefirst();
    linked.displaylist();
    return 0;
}

But here are the compile errors:

1>------ Build started: Project: linked)list, Configuration: Debug Win32 ------
1>  linked_list.cpp
1>c:\users\david\documents\visual studio 2010\projects\linked)list\linked_list.cpp(40): error C3867: 'firstl::empthy': function call missing argument list; use '&firstl::empthy' to create a pointer to member
1>c:\users\david\documents\visual studio 2010\projects\linked)list\linked_list.cpp(51): error C3867: 'firstl::empthy': function call missing argument list; use '&firstl::empthy' to create a pointer to member
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

How can this be fixed?

6
  • 3
    It's spelled as "empty", not "empthy". Commented Jul 24, 2010 at 7:16
  • 2
    I simply couldn't bring myself to answer this question since it would have involved misspelling "empty" in a most horrible way. Commented Jul 24, 2010 at 7:19
  • how come you're doing linked-lists on C++ and you don't know how to call a funcion? Commented Jul 24, 2010 at 7:20
  • 1
    @Greg: Is “bringt” intentional? :p Commented Jul 24, 2010 at 7:21
  • 1
    That's a singly-linked list. A doubly-linked list would have a "previous" pointer as well as a "next" in each link. Commented Jul 24, 2010 at 12:10

5 Answers 5

8

empthy is a method, therefore an ending () is needed to call it.

void insertfirst(long dd){

    Link *newlink=new Link(dd);
    if (empthy())      // <-----
        last=newlink;
    ...
Sign up to request clarification or add additional context in comments.

Comments

2

You have to change

if (empthy)

to

if (empthy())

Comments

1

if (empthy) is wrong, empthy is a function so it should be if (empthy()).BTW, method name should be empty.

Comments

1

empthy is a function, you should call it like this:

if (empthy())

Comments

1

Here's the way I would do it:

#include <list>
typedef std::list<long> firstl;

Comments

Your Answer

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