Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 将字符串放入链表_C++_C_Singly Linked List - Fatal编程技术网

C++ 将字符串放入链表

C++ 将字符串放入链表,c++,c,singly-linked-list,C++,C,Singly Linked List,我的wordcount函数有一些问题。如果有人能帮忙的话。 我正在从输入文件中读取字符,并将其转换为单词。我将它们添加到链接列表中 并使用打印语句检查每个单词是否进入链接列表。当我调用打印列表时 在wordcount函数中,它只打印头部。显然head->next为空 typedef struct wordNode{ int data; char *word; struct wordNode *next; }wordNode; void wordCount(char *F

我的wordcount函数有一些问题。如果有人能帮忙的话。 我正在从输入文件中读取字符,并将其转换为单词。我将它们添加到链接列表中 并使用打印语句检查每个单词是否进入链接列表。当我调用打印列表时 在wordcount函数中,它只打印头部。显然head->next为空

typedef struct wordNode{
    int data;
    char *word;
    struct wordNode *next;
}wordNode;

void wordCount(char *FileName); // function prototype
void printList(wordNode *head);

int main(int argc, char *argv[]){
    int i,j;
    int count = argc-1; // the count basically tells the for loop how many times to runs i.e number of files
    pid_t childpid[count], wpid; // making a childpid array for forking more than one process
        int status=0; // for parent process to wait for child

        if(argc >= 1){
            for(j=1; j<argc; j++){
                    childpid[j] = fork();

                    if(childpid[j] == -1)
                        printf("The fork didn't go through for process %d\n", j);
                    else if(childpid[j] == 0){
                       // printf("got here\n");
                        wordCount(argv[j]);
                }

            }
        }

        else {
            printf("incorrect no. of argc\n"); }


        //here the following code makes parent to wait for all the child processes to terminate

        while((wpid = wait(&status)) > 0){

            if ((wpid == -1)){
                printf("Father didn't wait for child to term");
                break;
            }
        }

        printf("All %d files have been counted\n", count);
        return 0;

}


void wordCount(char *FileName){
    char prev;  // keep track of last word that was read
    char character;// to keep track of each character when reading input data
    int words=0; // keep count of words
    FILE* fp;
    char thisword[100];

    int k=0;

    wordNode *head;
    wordNode *current;
    head=NULL;

    current = NULL;

    fp=fopen(FileName,"r");
    prev='\0';

    if(fp==NULL){
        printf("unable to open the file\n");
        exit(EXIT_FAILURE); }
    else{

    while((character = fgetc(fp)) != EOF ){

        if(character == ' ' || character == '\t' || character=='\0' || character =='\n' ){
            printf("in here\n");

            words++; 
            thisword[k]='\0';
            printf("HERE THE WORD is %s\n", thisword);
            printf("\n");
            if (head==NULL){

                head = malloc(sizeof(wordNode));
                head->next = malloc(sizeof(wordNode));
                head->word = (char *)calloc(strlen(thisword) + 1, strlen(thisword));
                strcpy(head->word, thisword);

                printf("%s \n", head->word);
                current = head->next;
            }

            else{
                current = (wordNode*)malloc(sizeof(wordNode));
                current->word = (char *)calloc(strlen(thisword) + 1,  strlen(thisword));
                strcpy(current->word, thisword);
                current->next = NULL;
                printf("%s\n ", current->word);
                current = current->next;
            }

            k=0;
         }

         else {
            thisword[k++] = tolower(character);   
         }

    }
        printList(head);
        printf("Child process for %s: number of words is %d\n", FileName,words);
        exit(0);
        fclose(fp);
    }   

}



void printList(wordNode *head)
{
    while(head != NULL)
    {
        printf("%s ", head->word);
        head = head->next;
    }
typedef结构字节点{
int数据;
字符*字;
结构wordNode*下一步;
}字节点;
无效字数(字符*文件名);//功能原型
作废打印列表(wordNode*head);
int main(int argc,char*argv[]){
int i,j;
int count=argc-1;//计数基本上告诉for循环要运行多少次,即文件数
pid\u t childpid[count],wpid;//生成用于分叉多个进程的childpid数组
int status=0;//父进程等待子进程
如果(argc>=1){
对于(j=1;j 0){
如果((wpid=-1)){
printf(“父亲没有等孩子出生”);
打破
}
}
printf(“所有%d个文件都已计数\n”,计数);
返回0;
}
无效字数(字符*文件名){
char prev;//跟踪读取的最后一个单词
char character;//在读取输入数据时跟踪每个字符
int words=0;//保留字数
文件*fp;
字符这个词[100];
int k=0;
字节点*头;
wordNode*当前;
head=NULL;
电流=零;
fp=fopen(文件名,“r”);
上一个='\0';
如果(fp==NULL){
printf(“无法打开文件\n”);
退出(退出失败);}
否则{
while((character=fgetc(fp))!=EOF){
如果(字符=“”| |字符=“”\t’| |字符=“”\0’| |字符=“”\n’){
printf(“在此\n”);
words++;
这个词[k]='\0';
printf(“这里的单词是%s\n”,thisword);
printf(“\n”);
if(head==NULL){
head=malloc(sizeof(wordNode));
head->next=malloc(sizeof(wordNode));
head->word=(char*)calloc(strlen(thisword)+1,strlen(thisword));
strcpy(head->word,thisword);
printf(“%s\n”,head->word);
当前=头部->下一步;
}
否则{
当前=(wordNode*)malloc(sizeof(wordNode));
当前->单词=(char*)calloc(strlen(thisword)+1,strlen(thisword));
strcpy(当前->单词,此单词);
当前->下一步=空;
printf(“%s\n”,当前->单词);
当前=当前->下一步;
}
k=0;
}
否则{
这个词[k++]=tolower(字符);
}
}
印刷品清单(标题);
printf(“%s的子进程:字数为%d\n”,文件名,字数);
出口(0);
fclose(fp);
}   
}
无效打印列表(wordNode*head)
{
while(head!=NULL)
{
printf(“%s”,head->word);
头部=头部->下一步;
}

<代码>字符字符;<代码> >代码> int字符;< /Cord>请指定不同的语言。例如,在C++中,<代码> Type结构节点WordNoint {}}}是浪费的时间。<代码>结构节点WordNo.{}};是足够的,而在C中,C++中的代码>当前=(WordNo节点*)Maloc(siZeof)。(wordNode))“看起来不是C”,我不知道是否可以删除C++标签。如果C++是希望的,那么你就可以用<代码> STD::列表< /C> >,或者这必须是自制的?考虑把链表逻辑与其他的分开。有一个功能性的链表,那么编写使用它的代码是安全的。如果你试图将两者集成起来,调试时间通常会延长很多倍。一般来说,做一件事。证明它工作正常。继续做下一件事。你的bug:If(head==NULL)中没有任何内容
else
case将new
wordNode
链接到任何其他节点。
head
左指NULL,其余节点泄漏。