Click here to Skip to main content
15,029,010 members
Please Sign up or sign in to vote.
1.00/5 (4 votes)
See more:
C++
#include<bits/stdc++.h>
#include<iostream>
using namespace std;
struct linkedlist
{
    int info;
    struct linkedlist *next;
};
typedef struct linkedlist node;
void createlist(node *ptr)
{
 cout<<"Enter value (-1) to exit : ";
 cin>>ptr->info;
 if(ptr->info==-1)
 {
     ptr->next=0;
 }
 else
 {
     node *newnode;
     newnode=(node*)malloc(sizeof(node));
     ptr->next=newnode;
     createlist(newnode);
 }
}
void printlist(node *ptr)
{
    while(ptr->next!=0)
    {
        cout<<ptr->info<<" ";
        ptr=ptr->next;
    }
}
void insertbeg(node *ptr)
{
    node *newnode;
    newnode=(node*)malloc(sizeof(node));
    cout<<endl;
    cout<<"Enter insert value = ";
    cin>>newnode->info;
    newnode->next=ptr;
    ptr=newnode;
    cout<<endl;
    cout<<"inserted list = ";
    printlist(ptr);

}
int main()
{
    node *start;
    start=(node*)malloc(sizeof(node));
    createlist(start);
    printlist(start);
    insertbeg(start);
}


What I have tried:

#include<bits/stdc++.h>
#include<iostream>
using namespace std;
struct linkedlist
{
    int info;
    struct linkedlist *next;
};
typedef struct linkedlist node;
void createlist(node *ptr)
{
 cout<<"Enter value (-1) to exit : ";
 cin>>ptr->info;
 if(ptr->info==-1)
 {
     ptr->next=0;
 }
 else
 {
     node *newnode;
     newnode=(node*)malloc(sizeof(node));
     ptr->next=newnode;
     createlist(newnode);
 }
}
void printlist(node *ptr)
{
    while(ptr->next!=0)
    {
        cout<<ptr->info<<" ";
        ptr=ptr->next;
    }
}
void insertbeg(node *ptr)
{
    node *newnode;
    newnode=(node*)malloc(sizeof(node));
    cout<<endl;
    cout<<"Enter insert value = ";
    cin>>newnode->info;
    newnode->next=ptr;
    ptr=newnode;
    cout<<endl;
    cout<<"inserted list = ";
    printlist(ptr);

}
int main()
{
    node *start;
    start=(node*)malloc(sizeof(node));
    createlist(start);
    printlist(start);
    insertbeg(start);
}
Posted
Updated 17-Mar-21 5:54am
v2
Comments
Richard Deeming 17-Mar-21 11:36am
   
This is not a code conversion service!

1 solution

Such a code is very very near to standard C one. Your task is simple, just convert the C++ console I/O (cin, cout) to the corresponding C one (printf, scanf and the like).
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900