史山代码,纪念一下自己对链表的第一次尝试

本文介绍了如何使用C语言实现链表数据结构,包括链表的创建、销毁、查找元素以及插入新节点。还涉及了字符串处理和哈希映射算法的应用。
摘要由CSDN通过智能技术生成
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define eleType long long

typedef struct ListNode
{
    eleType data;
    struct ListNode* next; 
}ListNode;

typedef struct LinkedList
{
    struct ListNode* head;
    size_t size;
}LinkedList;

void LinkedListCreate(LinkedList* list){
    ListNode* headNode=(ListNode *)malloc(sizeof(ListNode));
    
    headNode->next=NULL;
    headNode->data=-1;
    
    list->head =headNode;
    list->size =0;
}

void LinkedListDestroy(LinkedList* list){
    while (list->head)
    {
        ListNode* temp=list->head;
        list->head=list->head->next;
        free(temp);
    }
    list->size =0;
}

int LinkedListFind(LinkedList* list,eleType value){
    ListNode* current= list->head;
    while (current)
    {
        if (current->data==value)
        {
            return 1;
        }
        current=current->next;
    }
    return 0;
}

void LinkedListInsert(LinkedList* list,eleType value){
    ListNode* current=list->head;//如果head是NULL呢?这种情况我还没处理。ps:已经在初始化处已经处理。
    ListNode* newNode=(ListNode*)malloc(sizeof(ListNode));

    newNode->data=value;
    newNode->next=NULL;

    for (size_t i = 0; i <= list->size; i++)
    {
        if (i==list->size)
        {
            current->next=newNode;
            break;
        }
        current=current->next;
    }
    list->size++;
}

long long poww(int n,int m){
    long long tmp=1;
    for (size_t i = 1; i < m; i++)
    {
        tmp*=n;
    }
    return tmp;
}

int main(){
    long long tmp;
    int tmp1;
    char str[10000];
    int length;
    int i;
    int cot;
    int anw;
    LinkedList list8;
    
    //printf("1");
    while (gets(str))
    {
        //getchar();
        //printf("%s",str);
        if (str[0]=='#')
        {
            break;
        }
        LinkedListCreate(&list8);
        //printf("%s",str);
        int list[10000]={0};
        anw=0;
        
        length=strlen(str);
        tmp=0;
        cot=0;
        for ( i = 0; i < length; i++)
        {
            if (str[i]==' '||i==length-1)
            {
                tmp1=tmp%10000;//第二次映射。
                if (list[tmp1]==0)
                {
                    LinkedListInsert(&list8,tmp);//newListNode add;
                    list[tmp1]++;
                    anw++;
                }
                else//注释后移
                {
                    if (LinkedListFind(&list8,tmp))
                    {
                        continue;
                    }
                    LinkedListInsert(&list8,tmp);
                    anw++;
                }
                cot=0;
                tmp=0;
                continue;
            }
            tmp+=((int)str[i]-96)*poww(28,cot);//字母-数字第一次映射。
            cot++;
        }
        printf("%d\n",anw);
        LinkedListDestroy(&list8);
    }
    return 0;
}
                                                                                            //for ( i = 0; i < nodelength; i++)
                                                                                            //{
                                                                                            //    if (LinkedListFind())//ListNode.yicihash==tmp1)
                                                                                            //    {
                                                                                            //        break;用break是错的,只有一个for循环,会跳出未完成的工作。应用continue,进入到下一个词。
                                                                                            //    }
                                                                                            //    if (i==nodelength-1)
                                                                                            //    {
                                                                                            //        anw++;
                                                                                            //        LinkedListInsert();//newlistnode add;
                                                                                            //    }
                                                                                            //}

  • 10
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值