0

here is full code from algorithm and data structure in java written in c++

#include <iostream>
using namespace std;
class link {
public:
    int idata;
    double ddata;
    link *next;
    link ( int id,double dd){
        idata=id;
        ddata=dd;
    }
public :
    void display(){

        cout<<idata<<"=>";
        cout<<ddata;

    }

}; 


class  linked_list{
public :
    link *first;

public:
     linked_list(){

         first=NULL;
     }
      ~linked_list(){

          while (first!=NULL){
              link *ptr=first->next;
              delete first;
              first=ptr;
          }

      }
public:
    bool isempthy(){
        return (first==NULL);
    }
    void insert(int  id,double dd){


link *newlink= new link(id,dd);
newlink->next=first;
 first=newlink;

}
public:
    link deletefirst(){

        link *temp=first;
        first=first->next;
        return *temp;

    }

    void displaylist(){
        cout<<"List (first-->last";

        link *current=first;

        while (current!=NULL){
            current->next;
            current.display();
        }



    }






}



int main(){

    linked_list ll;
    ll.insert(22,12);
    ll.insert(44,35);
    ll.insert(12,46);
    ll.insert(100,23.45);
    while (!ll.isempthy()){
        link alink=ll.deletefirst();

        alink.display();
    }


     return 0;
}

error is that this fragment

current.display();  does not work  please help
5
  • 4
    If you're using C++, perhaps you should get a copy of Algorithms and Data Structures in C++. Commented Jul 23, 2010 at 13:21
  • this line current->next; does nothing... Commented Jul 23, 2010 at 13:21
  • 1
    possible duplicate of [ linked list from java to c++](stackoverflow.com/questions/3318086/linked-list-from-java-to-c) Commented Jul 23, 2010 at 13:23
  • If you're going to use a linked list in C++, why not use std::list? That way, when you come to your senses, it'll be relatively easy to change to another data structure. Commented Jul 23, 2010 at 13:24
  • What does "does not work" mean? Do you get a compiler error? If so, then tell us exactly what the error message is. Or does it not do what you expected? If so, then what did you expect, and how is that different from what it really does? Commented Jul 23, 2010 at 14:07

2 Answers 2

1
void displaylist(){
        cout<<"List (first-->last";

        link *current=first;

        while (current!=NULL)
        {
            //display the current node
            current->display();
           //then move to the next one
            current = current->next;
          }   
    }
Sign up to request clarification or add additional context in comments.

Comments

1

link *current use current-> instead of current.

Comments