保存不同的话到链表 [英] Save distinct words into linked list
问题描述
基本上我有2链表在这里:列表和独特的。有几组已经较早进入名单结构保存的话。本来打算写一个程序,是会发现是不同的/独特的话,并保存到不同的'结构。以下是我走到这一步,基于我对指针的概念。然而,当我尝试打印'不同',程序崩溃:(请纠正我,如果我错了。
Basically I have 2 linked list here: list and distinct. There are a few set of words which have been saved earlier into the 'list' struct. Was gonna write a program that is gonna find the words that are distinct/unique and save it into the 'distinct' struct. Here is what I got so far based on my concept on pointers. However when I try to print 'distinct', the program crashes :( please correct me if I'm wrong.
struct list {
char string[50];
struct list *next;
};
struct distinct {
char string[50];
struct distinct *next;
};
void checkdistinct() {
list *ori = NULL;
distinct *copy = NULL;
distinct *check = NULL;
if(ori == NULL && copy == NULL) { //first time.
ori = ori->next;
copy = copy->next;
copy = (distinct*)malloc(sizeof(distinct));
strcpy(copy->string, ori->string);
ori = ori->next;
copy = copy->next;
}
else {}
while(ori!=NULL) {
check = check->next;
while(check != NULL) {
if(strcmp(ori->string, check->string)!=0) {
check = check->next;
}
else {
ori = ori->next;
check = NULL;
}
}
//only compare same casing words, for now.
copy = (distinct*)malloc(sizeof(distinct));
strcpy(copy->string, ori->string);
ori = ori->next;
copy = copy->next;
}
}
当我尝试在主要打印,它会崩溃:(如果你需要为$ C $额外意见请回复CS,谢谢!
When I try to print in main, it will crash :( please reply if you need extra comments for the codes. Thanks!
推荐答案
在您的code,层次分明不是一个变量名的结构,但。你似乎有误解的三分球,希望这将有助于虽然它不直接关系到你的问题。
In your code, distinct is not the name of a variable but of a structure. You seem to have misunderstood pointers, hopefully this will help although it is not directly related to your question.
我留下了很多错误检查,使code看起来简单。
I've left out a lot of error checking to make the code look simpler.
typedef struct listnode {
char string[50];
struct listnode *next;
} list_node;
typedef struct listbase {
list_node *head;
int numberOfElements;
} list;
/* Add a new string at the start of the list
*/
void ListPrepend(list *myList, char *myString) {
list_node *newNode = malloc(sizeof *newNode); /* create node to store string */
strcpy(newNode->string, myString); /* copy string into node */
newNode->next = myList->head; /* New node now followed by whole list */
myList->head = newNode; /* List now starts with new node */
myList->numberOfElements++;
}
/* Add a new string at the end of the list
*/
void ListAppend(list *myList, char *myString) {
list_node *newNode = malloc(sizeof *newNode), /* create node to store string */
*currentNode = myList->head; /* pointer to node so we can find the end */
strcpy(newNode->string, myString); /* copy string into new node */
newNode->next = NULL; /* Nothing following this node */
if ( myList->head == NULL ) {
myList->head = newNode; /* we didn't have a start node so assign it */
} else {
/* if there is a next node, move to it */
while ( currentNode->next != NULL ) {
currentNode = currentNode->next;
}
/* there is no next node so add new node on the end */
currentNode->next = newNode;
}
myList->numberOfElements++;
}
/* Show the list in order head to tail
*/
void ListDisplay(list *myList) {
list_node *currentNode = myList->head;
while ( currentNode != NULL ) {
printf("%s\n", currentNode->string);
currentNode = currentNode->next;
}
}
int main() {
list distinct = {0}; /* Now there is a variable called distinct */
char name[][20] = {"Lim Zheng Yue", "Monkey", "Dave"};
ListAppend(&distinct, name[0]);
ListAppend(&distinct, name[1]);
ListPrepend(&distinct, name[2]);
ListDisplay(&distinct);
}
这篇关于保存不同的话到链表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!