2

我正在尝试设计一个程序,该程序将从文件中获取输入(该文件由整数和用空格分隔的单词组成,它将单词存储在链表中并将其打印在另一个函数中。我的问题是:我如何返回将链表结构连接到 main() 以进行进一步处理?

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 ????
    }

我不知道如何返回,所以我用问号、调用部分和返回部分标记了它。

4

2 回答 2

1

createlist函数中为您想要的每个数据创建一个节点,并将其引用到前一个。将指针返回到第一个。

用于malloc为每个节点分配数据并malloc再次用于为每个节点所需的字符串分配内存

您可以使用此处的示例并像他们一样做

在这里 - 这应该做的工作:

#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;
}

要知道当前 char 是否为整数,您可以执行以下操作:

if(c>= '0' && c<= '9')
于 2013-08-26T14:16:08.290 回答
0

尽管我已经向您展示了如何进行返回并接受部分呼叫。我想提一下,你还没有为你分配任何东西,headcurr确保你做任何你需要处理的事情,然后返回headobj.

在这里,您可以使用代码:

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;
}
于 2013-08-26T14:24:53.010 回答