在链表上使用 strcmp 进行比较
我对链表不太了解,我不知道是否可能,但我需要这样做:)我有一个加载到结构的链表,我需要比较所有字符在结构上......最好有一个例子:
这是没有链表
结构
typedef struct x{
char name[100];
}x;
typedef x Info;
typdef struct Elem{
Info node;
struct Elem*next;
}Element;
for(i=0;i<100;i++){
if(strcmp(a.name[i],a.name[i+1])==0){
printf("Same name\n");
}
}
else
printf("Diff name\n");
现在我需要做这样的事情,但有链表
I'm not a great understanding on linked-list, i don't know if it's possible, but i need to do it :) I have a linked list that are load to the struct, and i need to compare a all the chars on the struct....It's better with an example:
This is without linked lists
struct
typedef struct x{
char name[100];
}x;
typedef x Info;
typdef struct Elem{
Info node;
struct Elem*next;
}Element;
for(i=0;i<100;i++){
if(strcmp(a.name[i],a.name[i+1])==0){
printf("Same name\n");
}
}
else
printf("Diff name\n");
Now i need to do something like this but with linked-list
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
首先: int strcmp ( const char * str1, const char * str2 ) 比较两个 C 字符串(char 指针)。这意味着 a.name[i] 应该是一个 char 指针而不是一个 char!确保情况确实如此(即确保 a.name 是一个 c 字符串数组的数组,而不是一个字符数组)。
其次,如果是前一种情况,您的代码只会将字符串 i 与字符串 i+1 进行比较。它不会将所有字符串相互比较。
无论如何,看起来你并没有以正确的方式做你想做的事情。我猜您想要一个定义如下的结构:
名称的占位符、其他成员和下一个指针以启用链接列表数据类型。这样,
如果您想将所有字符串相互比较,您可以将名称与: 或双循环进行比较。
First of all: int strcmp ( const char * str1, const char * str2 ) compares two C-strings (char pointers). This means that a.name[i] should be a char pointer and not a char! Make sure this is the case (i.e. make sure a.name is an array of c-string arrays, and not an array of chars).
Secondly, if the previous is the case, your code will only compare string i with string i+1. It will not compare all strings with each other.
In any case, it looks like you are not doing whatever it is you want to do the right way. I'm guessing you want a struct that is defined like this:
A placeholder for a name, other members, and a next pointer to enable the linked list data type. That way you can compare names with:
or with a double loop if you want to compare all strings with each other.
因此,您需要做的第一件事就是了解链表的基础知识。您可以在这里详细阅读: http://www.codeproject.com/KB/cpp/ linked_list.aspx
注意:在了解指针之前,您确实无法理解链表。 http://www.cplusplus.com/doc/tutorial/pointers/
本质上是链表由许多相互链接的“节点”组成。每个节点至少有两个数据,一个是数据(在您的情况下是字符),另一个是指向列表中下一个节点的指针。
定义一个结构看起来像(用伪代码):
您将有一个指向链表的第一个节点的指针。就像这样:
然后循环整个列表是小菜一碟:
但同样,要理解这一点,您需要了解指针的基础知识。
So the first thing you need to do is understand the fundamentals of linked-list. You can read in detail here: http://www.codeproject.com/KB/cpp/linked_list.aspx
NOTE: You really can't undersand linked lists until you understand pointers. http://www.cplusplus.com/doc/tutorial/pointers/
Essentially a linked-list is composed of numerous "nodes" that link to each other. At a minimum each node will have two pieces of data, one being the data (in your case a character) and the other being a pointer to the next node in the list.
Defining a struct would look like (in pseudocode):
You would have a pointer to the first node of the linked list. Something like:
Then cycling through the entire list is a piece of cake:
But again, to understand this you need to understand the fundamentals of pointers.
这是一个遍历链表并比较相邻元素名称的程序。我冒昧地重命名了一些东西,但除此之外,数据结构的代码与您的相同。
程序的输出:
Here is a program that traverses the linked list and compares the names of adjacent elements. I have taken the liberty of renaming a couple of things, but otherwise the code for the data structures is the same as yours.
The output of the program: