if(strcmp(p1->data,p2->data)!=0) { //当有不相同的元素时,则指向下一结点,否则结束 p1 = p1->next; } else { break;什么意思
时间: 2024-04-23 18:23:54 浏览: 36
这段代码是一个链表中查找两个节点是否相同的过程。其中 `p1` 和 `p2` 分别指向两个节点,`strcmp(p1->data,p2->data)` 是比较两个节点的数据是否相同,如果不相同,则将 `p1` 指向下一个节点,继续比较直到找到不同的节点。如果相同,则跳出循环,表示已经找到相同的节点。需要注意的是,这段代码中并没有对相同节点的处理,可能需要根据实际情况进行相应的处理。
相关问题
while(p2) { //遍历集合B, p1 = s->next; //p1指向新建的链表 while(p1) { if(strcmp(p1->data,p2->data)!=0) { //当有不相同的元素时,则指向下一结点,否则结束 p1 = p1->next; } else { break; } } if(!p1) { //将集合B中的元素放到链表s中去 LinkList inter = (LinkList)malloc(sizeof(LLinkList));//头插法插入 strcpy(inter->data,p2->data); inter->next = s->next; s->next = inter; } p2 = p2->next; } if(i) Output(s); return s; }
这段代码实现了两个集合的交集运算,并将结果存储在一个新的链表中。
假设集合 A 和集合 B 分别用链表 `La` 和 `Lb` 表示,且它们的数据类型定义如下:
```c
struct LNode {
char data[20];
struct LNode *next;
};
typedef struct LNode LNode;
typedef struct LNode *LinkList;
```
那么,代码的实现过程如下:
1. 首先,新建一个空链表 `s`,用于存储集合 A 和集合 B 的交集。
```c
LinkList s = (LinkList)malloc(sizeof(LNode));
s->next = NULL;
```
2. 遍历集合 B 的每个元素,对于每个元素,都在集合 A 中查找是否存在相同的元素:
```c
while (p2) {
p1 = s->next;
while (p1) {
if (strcmp(p1->data, p2->data) != 0) {
p1 = p1->next;
} else {
break;
}
}
// ...
}
```
其中,`p1` 和 `p2` 分别指向链表 `s` 和 `Lb` 的头结点。
3. 如果集合 A 中不存在该元素,则将该元素放到链表 `s` 中:
```c
if (!p1) {
LinkList inter = (LinkList)malloc(sizeof(LNode));
strcpy(inter->data, p2->data);
inter->next = s->next;
s->next = inter;
}
```
其中,`inter` 是一个新建的结点,用于存储该元素。
4. 最后,输出链表 `s` 中的所有元素,即为集合 A 和集合 B 的交集。
```c
Output(s);
```
下面是完整的代码实现:
```c
void Intersection(LinkList La, LinkList Lb) {
LinkList p1, p2, s;
int i = 0;
p2 = Lb->next;
s = (LinkList)malloc(sizeof(LNode));
s->next = NULL;
while (p2) {
p1 = La->next;
while (p1) {
if (strcmp(p1->data, p2->data) != 0) {
p1 = p1->next;
} else {
break;
}
}
if (!p1) {
LinkList inter = (LinkList)malloc(sizeof(LNode));
strcpy(inter->data, p2->data);
inter->next = s->next;
s->next = inter;
}
p2 = p2->next;
}
Output(s);
}
```
其中,`Output` 函数用于输出链表中的所有元素。
#pragma GCC optimize ("O3") #pragma pack (16)//所有的存储都是以16个字节为单位的 #include <stdio.h> #include <stdlib.h> #include <string.h> #define tolower(c) (c>='A'&&c<='Z')?c-'A'+'a':c #define DATA 5200000 #define SIZE 1000005 int trie[4200000][26]; typedef struct node { int cnt; int logo; struct node *child[26]; } Node; Node *root; char str[35000000]; typedef struct word { char wor[85]; int cnt; } Word; Word w[300000]; struct node *creat() { Node *Root = (Node *)malloc(sizeof(Node)); Root->logo = 0; Root->cnt = 0; for (int i = 0; i < 26; i++) { Root->child[i] = NULL; } return Root; } void insert(Node *root, char *word, int flag) { struct node *leaf = root; for (int i = 0; word[i] != '\0'; i++) { int index = word[i] - 'a'; if (!leaf->child[index]) { leaf->child[index] = creat(); } leaf = leaf->child[index]; } if (leaf->logo != -1) leaf->logo = flag; leaf->cnt++; } int count = 0; void dfs(Node *leaf, char *word, int level) { if (leaf->logo == 1) { word[level] = '\0'; strcpy(w[count++].wor, word); w[count - 1].cnt = leaf->cnt; } for (int i = 0; i < 26; i++) { if (leaf->child[i]) { word[level] = i + 'a'; dfs(leaf->child[i], word, level + 1); } } } int cmp(const void *p1, const void *p2) { Word *v1, *v2; v1 = (Word *)p1; v2 = (Word *)p2; if (v1->cnt != v2->cnt) return v2->cnt - v1->cnt; else return strcmp(v1->wor, v2->wor); } int main(int argc, char *argv[]) { char s[1024]; int temp; int n, m;//读入n,m; //n = atoi(argv[1]); //m = atoi(argv[2]); scanf("%d%d", &n, &m); //读入stopwords中的元素,并令末序数组值为0,即该单词不计入 root = creat(); FILE *stopwords = fopen("stopwords.txt", "r"); while (fscanf(stopwords, "%s", s) != EOF) { insert(root, s, -1); } int cnt; FILE *article = fopen("article.txt", "r"); cnt = fread(str, sizeof(char), 35000000, article); char word[85]; int w_cnt = 0; for (int i = 0; i < cnt; i++) { char c = tolower(str[i]); if (c >= 'a' && c <= 'z') { word[w_cnt++] = c; } else { word[w_cnt] = '\0'; p = 0; w_cnt = 0; if (strlen(word) > 0) { insert(root, word, 1); } }//对article中的所有单词进行计数 } dfs(root, word, 0); qsort(w, count, sizeof(w[0]), cmp); printf("%s", w[0].cnt); return 0; }
这段代码是一个基于Trie树的单词统计程序,在给定的文章中统计出现频率最高的单词。程序从文件中读入stopwords(不计入统计的常用单词)和article(待统计的文章),使用Trie树统计文章中出现的单词频率,并按照出现频率从高到低输出结果。
具体实现中,程序使用了一个结构体Node表示Trie树中的节点,其中包含了该节点出现次数cnt和单词编号logo。同时,程序定义了一个结构体Word表示单词及其出现次数。程序中的insert函数用于向Trie树中插入单词,dfs函数用于遍历Trie树并统计单词出现次数,cmp函数用于按照出现频率从高到低排序输出结果。
注意,程序中的一些细节需要注意,比如将所有字母转换为小写字母,以及对于长度为0的单词不进行计数等。