2

I am trying to design a program that would take input from a file(which consists of integers and words separated by a space and it would store the words in a linked list and print it in another function. My question is: How do I return the linked list struct to the main() for 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 the name of the file for 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 to return so I marked it with question marks, the call part and the return part.

1
  • In general, a linked list is passed around by passing a pointer to the head or first node. Commented Aug 26, 2013 at 19:45

2 Answers 2

1

in the createlist function create a node for each data you want, and reference it to the previous one. return the pointer to the first.

use malloc to allocate data for each node and use malloc again to allocate memory for the string you need for each node

you can use the example here and do the same as they do

here - that should do the 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 the name of the file for 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') || i == 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 with headnode as [%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 to end of list with 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 the current char is an integer you can do:

if(c>= '0' && c<= '9')
Sign up to request clarification or add additional context in comments.

6 Comments

Well Thanks for the help! but the program is actually terminating abruptly and the problem is somewhere here tempStr = (char *)malloc(30 * sizeof(char)); curr->data = tempStr; continue;
@ShrutiShwtu i've edited the answer. added the function to add items to the list. it's working now
Great! Its working! :) Would you please tell me what I can do if I have add only words and stop if I encounter an integer? If the file for example is: 1 Nature Disco Happy 2 Kite It should only add Nature Disco Happy and stop at 2?
I tried that. But it doesn't seem to work! :( And in the linked list, the last element is always left out. It is adding till the second last element only.
do another "curr = add_to_list(curr, tempStr);" to solve the second problem @ShrutiShwtu
|
0

Although I've shown you how to do the return and accept part of the call. I'd like to mention that you have not yet taken care of assigning anything to your head and curr make sure you do whatever you need to process and then return the head obj.

Here you go with the code:

using namespace std;

struct list* createlist(FILE *m);
struct list
{
    char data[30];
    struct list *next;
};

main()
{

    char a[100], ch;
    struct list* obj;
    cout<<"Enter the name of the file for 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[30];
    struct 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 head;
}

2 Comments

Like I said the code that was already present does not at all modify the declared variables curr and head. So currently you will only receive back the head pointer's content which is a reference to a garbage location. I simply show the OP a way to return and receive an object pointer.
Would you please suggest what i can do with that part? Because I am new to linked lists and don't have much idea on how to manipulate them!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.