通过链表储存文本

题目:读取一段文本内容,将每个文本内的单词保存在一个链表中,并插入指定单词到指定位置,最后显示整个文本内容出来。

思路:按行读取文本内容,通过定义两个指针,一个指向头,一个指向尾截取字母长度。链表再根据长度定义相同大小的空间进行memcpy。

运行环境:windows vs2019 Dev-c  linux gcc

相关头文件

#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define judge(letter) (!((letter>='a' && letter<='z') || (letter>='A' && letter<='Z')))

指针域,地址域,链表定义

struct linknode
{

    struct linknode* next;
    void* data;
};
struct linklist
{
    struct linknode pheader;
    int size;

};

链表初始化

//初始化
struct linklist* firstlist()
{
    struct linklist* mylist = (struct linklist*)malloc(sizeof(struct linklist));
    mylist->pheader.next = NULL;
    mylist->pheader.data = NULL;
    mylist->size = 0;
    return mylist;
}

链表添加,插入,销毁,遍历

//添加
void addlist(struct linklist* mylist, void*address,int wordsize)
{
    if (mylist == NULL || address == NULL)
    {
        return;
    }
    struct linknode* node = (struct linknode*)malloc(sizeof(struct linknode));
    (char*)node->data 
    node->data= (char*)malloc(sizeof(char) * (wordsize+1));
   memset(node->data, 0, wordsize + 1);

    struct linknode* temp = &mylist->pheader;  
    while (1)
    {
        if (temp->next == NULL)
        {
            memcpy(node->data, (char*)address, wordsize);
            node->next = temp->next;
            temp->next = node;
            mylist->size++;
            return;
        }
        temp = temp->next;
    }
}
//插入
void insetlist(struct linklist* mylist, void* address, int wordsize,int seat)
{
    struct linknode* temp = NULL;
    struct linknode* node = (struct linknode*)malloc(sizeof(struct linknode));
    (char*)node->data ;
    node->data= (char*)malloc(sizeof(char) * (wordsize + 1));
    memset(node->data, 0, wordsize + 1);

    temp = &mylist->pheader;
    if (seat > (mylist->size) || seat < 0)
    {
        seat = (mylist->size)+1;
    }
    int i;
    for (i = 1; i < seat; i++)
    {
        temp = temp->next;
    }
    memcpy(node->data, (char*)address, wordsize);
    node->next = temp->next;
    temp->next = node;
}
    //遍历
    void scanlist(struct linklist* mylist, void(*myprintf)(void*))
    {
        if (mylist == NULL || myprintf == NULL)
        {
            return;
        }
        struct linknode* temp = NULL;
        temp = mylist->pheader.next;
        while (temp)
        {
            myprintf((temp->data));
            temp = temp->next;
        }
        return;
    }
  //销毁链表
    void freelist(struct linklist* mylist)
    {
        struct linknode* node = NULL;
        struct linknode* temp = NULL;
        node = mylist->pheader.next;
        while (node)
        {
            temp = node;
            node = node->next;
            free(temp->data);
            free(temp);
        }
        free(mylist);
        mylist = NULL;
        temp = NULL; 
    }

自定义回调函数

 //打印回调函数
    void myprintf(void* data)
    {
        if (data == NULL)
        {
            return;
        }
      
        printf("%s\n", (char*)data);
        return;
    }

测试函数

//测试
    test4()
    {


            FILE* stream;
            char line[100];
            struct linklist* mylist = firstlist();
            if ((stream = fopen("C:\\Users\\mint\\Desktop\\list\\list\\Project1\\test.txt", "r")) != NULL)
            {
                while (fgets(line, 100, stream) != NULL)
                {
                    char* pstrat = line;
                    char* pend = line;
                   
                    while (*pend != '\0')//结束以\0为标志
                    {
                        if (judge(*pend))
                        {
                            unsigned int wordsize = pend - pstrat;
                            addlist(mylist, pstrat, wordsize);
                            pstrat = pend + 1;
                        }
                        //换行符结束,跳出循环
                        if (*pend == '\n' || *pstrat == '\n')//防止末尾换行符号前存在多个非字母的符号
                        {
                            break;
                        }
                        //防止多个空格,或其他非字母干扰
                        while (judge(*pstrat))
                        {
                            pend = pstrat;
                            pstrat++;
                            if (*pend == '\n' || *pstrat == '\n')//防止末尾换行符号前存在多个非字母的符号
                            {
                                break;
                            }
                        }
                        if (*pend == '\n' || *pstrat == '\n')//防止末尾换行符号前存在多个非字母的符号
                        {
                            break;
                        }
                         if (*(pend + 1) == '\0')//读取结束标志前的字符
                        {
                            if (!judge(*pend))
                            {
                                unsigned int wordsize = pend - pstrat + 1;
                                addlist(mylist, pstrat, wordsize);
                                break;
                            }
                        }
                        pend++;
                    }
                   
                    //printf("%s", line);
                }
              fclose(stream);
            }
          char c[] = { "abc" };
                int seat = 4;
                int wordsize = 3;
               // insetlist(mylist, c, wordsize, seat);
                scanlist(mylist, myprintf);
                printf("\n");
                freelist(mylist);
                //scanlist(mylist, myprintf);
    }

主函数

int mian()
{
test4();
return 0;
}


版权声明:本文为qq_62567244原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。