How can I return a linked list struct to main in C++? -


i trying design program take input file(which consists of integers , words separated space , store words in linked list , print in function. question is: how return linked list struct main() further process?

struct list* createlist(file *m); struct list {     char data[30];     struct list *next; };  using namespace std;  main() {     char a[100], ch;     cout<<"enter name of file obtaining input.."<<endl;     cin>>a;     file *in;      in=fopen(a,"r");     if(in!=null)     {         ch=fgetc(in);         if(ch=='1')         ????=createlist(in);         fclose(in);     }     return 0; }  struct list* createlist(file *m) {     cout<<"entered createlist function..!"<<endl;     char tempstr[30];     list *curr, *head;     char c;     int i=0;     curr=null;     while(eof!=(c=fgetc(m)))     {         if((c==' ') || (c=='\0'))         {             if(i==0)             {                 continue;             }             tempstr[i]='\0';             i=0;             continue;         }      tempstr[i]=c;     i++;     return ????     } 

i dont know how return marked question marks, call part , return part.

in createlist function create node each data want, , reference previous one. return pointer first.

use malloc allocate data each node , use malloc again allocate memory string need each node

you can use example here , same do

here - should work:

#include <iostream> #include <stdio.h> #include <stdlib.h> using namespace std;  struct list* create_list(struct list *head, char *val); struct list* add_to_list(struct list *node, char *val); struct list* createlist(file *m); struct list {     char *data;     struct list *next; }list;  main() {      char a[100], ch;     struct list* obj;     cout<<"enter name of file obtaining input.."<<endl;     cin>>a;     file *in;      in=fopen(a,"r");     if(in!=null)     {          ch=fgetc(in);         if(ch=='1')         obj=createlist(in);         fclose(in);     }     return 0; }  struct list* createlist(file *m) {     cout<<"entered createlist function..!"<<endl;     char *tempstr = (char *)malloc(30 * sizeof(char));     struct list *curr = null, *head = null;     char c;     int i=0;     curr=null;      while(eof!=(c=fgetc(m)))         {             if((c==' ') || (c=='\0') || == 29)             {                 if(i==0)                 {                     continue;                 }                 tempstr[i]='\0';                 i=0;                 curr = add_to_list(curr, tempstr);                  if(head == null)                 {                     head = curr;                 }                  tempstr = (char *)malloc(30 * sizeof(char));                 continue;             }              tempstr[i]=c;             i++;         }     return head; }   struct list* create_list(struct list *head, char *val) {     printf("\n creating list headnode [%s]\n",val);     struct list *ptr = (struct list*)malloc(sizeof(struct list));     if(null == ptr)     {         printf("\n node creation failed \n");         return null;     }     ptr->data = val;     ptr->next = null;      head = ptr;     return ptr; }  struct list* add_to_list(struct list *node, char *val) {     if(null == node)     {         return (create_list(node, val));     }      printf("\n adding node end of list value [%s]\n",val);      struct list *ptr = (struct list*)malloc(sizeof(struct list));     if(null == ptr)     {         printf("\n node creation failed \n");         return null;     }     ptr->data = val;     ptr->next = null;      node->next = ptr;     return ptr; } 

to know if current char integer can do:

if(c>= '0' && c<= '9') 

Comments

Popular posts from this blog

java - activate/deactivate sonar maven plugin by profile? -

python - TypeError: can only concatenate tuple (not "float") to tuple -

java - What is the difference between String. and String.this. ? -