[英]How can I return a linked list struct to main in C++?
我正在嘗試設計一個程序,該程序將從文件(包含整數和單詞,並用空格分隔,然后將單詞存儲在鏈接列表中並將其打印到另一個函數中)中接收輸入。我的問題是:如何返回鏈表結構到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 ????
}
我不知道如何返回,所以用問號 ,調用部分和返回部分對其進行了標記 。
在createlist
函數中,為所需的每個數據創建一個節點,並將其引用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;
}
要知道當前字符是否為整數,可以執行以下操作:
if(c>= '0' && c<= '9')
盡管我已經向您展示了如何退貨並接受部分通話。 我想提一提,您尚未處理將任何東西分配給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;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.