#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;
// }
//}
史山代码,纪念一下自己对链表的第一次尝试
最新推荐文章于 2025-05-23 08:39:28 发布