linked list from java to c++ [closed]
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.
精彩评论