我正在尝试设计一个程序,该程序将从文件中获取输入(由整数和由空格分隔的单词组成,它会将单词存储在链接列表中并将其打印在另一个文件中我的问题是:如何将链表结构返回到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 ????
}
我不知道如何返回所以我用问号,调用部分和返回部分标记它。
答案 0 :(得分:1)
createlist
函数中的为您想要的每个数据创建一个节点,并将其引用到前一个节点。将指针返回到第一个。
使用malloc
为每个节点分配数据,并再次使用malloc
为每个节点所需的字符串分配内存
您可以使用示例here并执行相同的操作
这里 - 应该做的工作:
#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')
答案 1 :(得分:0)
虽然我已经向您展示了如何返回并接受部分通话。我想提一下你还没有注意为你的head
和curr
分配任何内容,确保你做任何你需要处理的事情,然后返回head
obj
在这里你可以使用代码:
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;
}