HELP ME PLEASE TO CONVERT CODE C++ TO C

#define _CRTDBG_MAP_ALLOC
#include <stdlib.h>
#include <crtdbg.h>
#include<iostream>
#include<string>
using namespace std;

class node{
string data;
node* next;
public:
node(const string& a):data(a),next(0){}
void setnext(node* a){next=a;}
node* getnext(){return next;}
string getdata(){return data;}
};

int main()
{
node *h,*t,*p;
string car;
char c='\0';
int count=0;
while(c!='Q'&&c!='q'){
cin>>c;
if(c=='A'||c=='a'){
cin>>car;
count++;
if(count==1){
h=new node(car);
t=h;
}
else{
p=new node(car);
t->setnext(p);
t=p;
}
}
else if(c=='P'||c=='p'){
if(count==0)cout<<"nocar!!"<<endl;
else{
p=h;
while(p!=NULL){
cout<<p->getdata()<<' ';
p=p->getnext();
}
cout<<endl;
}
}
else if(c=='G'||c=='g'){
int x=0;
while(x<5&&count!=0){
p=h;
h=h->getnext();
delete p;
count--;
x++;
}
}
else if(c=='Q'||c=='q'){cout<<"bye!!"<<endl;}
}
p=h;
while(p!=NULL){
h=h->getnext();
delete p;
p=h;
}
_CrtDumpMemoryLeaks();
_CrtSetDbgFlag ( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF );
return 0;
}

//I want to use this in .c file.
Last edited on
Homework?
YES, I'am can,t. TT^TT
Let's start with that class:
1
2
3
4
5
6
7
8
9
class node{
    string data;
    node* next;
public:
    node(const string& a):data(a),next(0){}
    void setnext(node* a){next=a;}
    node* getnext(){return next;}
    string getdata(){return data;}
};


You have to represent it as a struct, and provide a set of functions to do what the members do. You have the added complication of having a string object in that struct. It's easiest to declare a fixed length buffer for that. But you have to use C string operations on it.

So you end up with:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#define DATA_SIZE 64

struct node{  // EDIT: changed class to struct
    char data[64];
    node* next;
};

// constructor
void init(node* obj, const char* a)
{
    strncpy(obj->data, a, DATA_SIZE);
    obj->next = NULL;
}

void setnext(node* obj, node* a)
{
    obj->next = a;
}
... and so on. Clearly no one is going to do the whole thing for you. You'll have to finish it from here.
Last edited on
THANK U SO MUCH, ^^
Topic archived. No new replies allowed.