我正在尝试设计一个程序,该程序将从文件(由整数和单词组成,用空格分隔)中获取输入,并将这些单词存储在链表中,然后打印到另一个函数中。我的问题是:如何将链表结构返回到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==' '))
{
if(i==0)
{
continue;
}
tempStr[i]=' ';
i=0;
continue;
}
tempStr[i]=c;
i++;
return ????
}
我不知道如何返回,所以我用问号、调用部分和返回部分标记了它。
在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==' ') || i == 29)
{
if(i==0)
{
continue;
}
tempStr[i]=' ';
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;
}
要知道当前字符是否为整数,可以执行以下操作:
if(c>= '0' && c<= '9')
尽管我已经向您展示了如何执行返回和接受部分调用。我想指出的是,您还没有考虑为您的head
和curr
分配任何内容。请确保您执行了需要处理的任何操作,然后返回head
对象。
给你代码:
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==' '))
{
if(i==0)
{
continue;
}
tempStr[i]=' ';
i=0;
continue;
}
tempStr[i]=c;
i++;
}
return head;
}