开发者

linked list from java to c++ [closed]

开发者 https://www.devze.com 2023-01-08 19:14 出处:网络
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical andcannot be reasonably answered in its current form. For help clari
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visi开发者_如何学Pythont the help center. Closed 12 years ago.

here is code to implement linked list i hope you understand main purpose of this code such kind of code is written in java and i am trying to implement in c++

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

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

    }

}; link next;


class  linked_list{
public :
    link first;

public:
     linked_list(){

         first=NULL;
     }

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






link    newlink= link(id,dd);
newlink.next=first;
 first=newlink;

}








int main(){




     return 0;
}

but it has some bugs please help me i think it is possible to rewrite written code in java 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;
        next = NULL;
    }

    void display(){
        cout<<idata<<"=>";
        cout<<ddata;
    }
};


class  linked_list{
public :
    link* first;

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


int main(){
    return 0;
}


You need to use pointers in C++ to create the connections between the list elements.

I suggest reading some single linked list example (or this) in C++ before attempting to create your own implementation.

0

精彩评论

暂无评论...
验证码 换一张
取 消