tags:

views:

131

answers:

5

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?

+8  A: 

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;
    ...
KennyTM
+2  A: 

You have to change

if (empthy)

to

if (empthy())
egrunin
+1  A: 

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

if (empthy())
MBZ
+1  A: 

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

Naveen
+1  A: 

Here's the way I would do it:

#include <list>
typedef std::list<long> firstl;
Jive Dadson
Good point. But it does not answer the question...
pascal