C分拣故障
如何按降序排列每个单词的出现情况 我目前的代码是:C分拣故障,c,sorting,C,Sorting,如何按降序排列每个单词的出现情况 我目前的代码是: #include <stdio.h> #include <stdlib.h> #include <stddef.h> #include <ctype.h> #include <string.h> #define WORDLEN 50 typedef struct node_struct NODE; struct node_struct { char *word; i
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include <ctype.h>
#include <string.h>
#define WORDLEN 50
typedef struct node_struct NODE;
struct node_struct {
char *word;
int count;
NODE *next;
};
/*compare and adds words to the list*/
NODE *new_node(char *word, NODE *head) {
NODE *p = head;
char *s;
if (p == NULL) {
p = (NODE*)malloc(sizeof(NODE));
s = (char*)malloc(sizeof(strlen(word) + 1));
strcpy(s, word);
p->word = s;
p->count = 1;
p->next = head;
return p;
}
for (; p->next != NULL && strcmp(p->word, word) != 0; p = p->next);
if (strcmp(p->word, word) == 0) {
p->count += 1;
return head;
}else{
p->next = (NODE*)malloc(sizeof(NODE));
p = p->next;
s = (char*)malloc(sizeof(strlen(word) + 1));
strcpy(s, word);
p->count = 1;
p->word = s;
p->next = NULL;
return head;
}
}
/*gets words*/
char *getword(char *w, int n)
{
int i = 0;
int c;
if (n <= 0 || feof(stdin))
return NULL;
c = getchar();
while (c != EOF && ! isalpha(c)) {
c = getchar();
}
if (c == EOF)
return NULL;
while (isalpha(c)) {
if (i < n - 1) {
w[i] = toupper(c);
i++;
}
c = getchar();
}
w[i] = '\0';
return w;
}
/*print*/
void print(NODE *p) {
for (; p != NULL; p = p->next) {
printf("%s %d\n",p->word, p->count);
}
printf("\n");
}
int main()
{
char w[WORDLEN];
NODE *p = NULL;
int i;
while (getword(w, WORDLEN) != NULL) {
p = new_node(w, p);
}
print(p);
return 0;
}
#包括
#包括
#包括
#包括
#包括
#定义WORDLEN 50
类型定义结构节点\u结构节点;
结构节点{
字符*字;
整数计数;
节点*下一步;
};
/*比较并将单词添加到列表中*/
节点*新节点(字符*字,节点*头){
节点*p=头部;
char*s;
if(p==NULL){
p=(NODE*)malloc(sizeof(NODE));
s=(char*)malloc(sizeof(strlen(word)+1));
strcpy(s,word);
p->word=s;
p->count=1;
p->next=头部;
返回p;
}
对于(;p->next!=NULL&&strcmp(p->word,word)!=0;p=p->next);
if(strcmp(p->word,word)==0){
p->count+=1;
回流头;
}否则{
p->next=(NODE*)malloc(sizeof(NODE));
p=p->next;
s=(char*)malloc(sizeof(strlen(word)+1));
strcpy(s,word);
p->count=1;
p->word=s;
p->next=NULL;
回流头;
}
}
/*获得话语权*/
char*getword(char*w,int-n)
{
int i=0;
INTC;
如果(n下一个){
printf(“%s%d\n”,p->word,p->count);
}
printf(“\n”);
}
int main()
{
字符w[WORDLEN];
NODE*p=NULL;
int i;
while(getword(w,WORDLEN)!=NULL){
p=新的_节点(w,p);
}
印刷品(p);
返回0;
}
您可以使用任何排序算法对链接列表进行排序。你可以在维基百科上找到排序算法的解释。下面是一些最简单的排序算法
这些Wikipedia页面还包含这些算法的伪代码,因此将这些算法翻译成C应该非常简单。您的起点是main()中的“p”,即,您可以使用这些算法按升序或降序进行排序。您可以使用任何排序算法对链接列表进行排序。你可以在维基百科上找到排序算法的解释。下面是一些最简单的排序算法
这些Wikipedia页面还包含这些算法的伪代码,所以将这些算法翻译成C应该非常简单。您的起点是main()中的“p”也就是说,您可以使用这些算法按升序或降序进行排序。问题有点模糊——您是想学习排序函数,还是只想让程序正常工作?:-) 如果你只想工作的话,你可以考虑使用一个动态扩展的数组,而不是一个链表。这样,您可以在添加新条目时使用realloc(),并使用普通的qsort()进行排序 作为一个副作用,与使用链表相比,您可能会看到更少的内存碎片,而链表可能会对总体性能产生良好的影响,尤其是如果您执行更少和更大的重新分配。代码也将更加紧凑,这可以提高可读性 更新:啊,我忍不住要表达我的意思。如果使用动态扩展数组,函数new_node可以如下实现:
NODE * nodes = NULL;
unsigned node_count = 0;
void new_node(char word) {
unsigned n;
for (n = 0; n < node_count && strcmp(nodes[n], word) != 0; n++);
if (n < node_count) {
nodes[n].count++;
} else {
nodes = realloc(nodes, sizeof(NODE) * node_count + 1);
assert(nodes != NULL);
nodes[n].word = strdup(word);
assert(nodes[n].word != NULL);
nodes[n].count = 1;
}
}
NODE*nodes=NULL;
无符号节点计数=0;
无效新节点(字符字){
无符号n;
对于(n=0;n
你喜不喜欢这是个人喜好的问题。这是否可用实际上取决于用例 问题有点模糊——您是想学习排序函数的知识,还是只想让程序正常工作?:-) 如果你只想工作的话,你可以考虑使用一个动态扩展的数组,而不是一个链表。这样,您可以在添加新条目时使用realloc(),并使用普通的qsort()进行排序 作为一个副作用,与使用链表相比,您可能会看到更少的内存碎片,而链表可能会对总体性能产生良好的影响,尤其是如果您执行更少和更大的重新分配。代码也将更加紧凑,这可以提高可读性 更新:啊,我忍不住要表达我的意思。如果使用动态扩展数组,函数new_node可以如下实现:
NODE * nodes = NULL;
unsigned node_count = 0;
void new_node(char word) {
unsigned n;
for (n = 0; n < node_count && strcmp(nodes[n], word) != 0; n++);
if (n < node_count) {
nodes[n].count++;
} else {
nodes = realloc(nodes, sizeof(NODE) * node_count + 1);
assert(nodes != NULL);
nodes[n].word = strdup(word);
assert(nodes[n].word != NULL);
nodes[n].count = 1;
}
}
NODE*nodes=NULL;
无符号节点计数=0;
无效新节点(字符字){
无符号n;
对于(n=0;n
你喜不喜欢这是个人喜好的问题。这是否可用实际上取决于用例 我终于解决了,谢谢大家的帮助 我的代码是:
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include <ctype.h>
#include <string.h>
#define WORDLEN 50
typedef struct node_struct NODE;
struct node_struct {
char *word;
int count;
NODE *next;
};
void new_node(char *word, NODE **head);
void print(NODE *p);
void mergesort_node(NODE **head);
void split_list(NODE *source, NODE **p, NODE **q);
NODE *merge_sorted_lists(NODE *source1, NODE *source2);
char *getword(char *word);
int main(int argc, char *argv[])
{
char w[WORDLEN];
NODE *p = NULL;
while (getword(w)) {
new_node(w, &p);
}
mergesort_node(&p);
print(p);
return 0;
}
//adds words to the list
void new_node(char *word, NODE **head) {
NODE *p = *head;
char *s;
if (p == NULL) {
p = (NODE*)malloc(sizeof(NODE));
s = (char*)malloc(sizeof(strlen(word) + 1));
strcpy(s, word);
p->word = s;
p->count = 1;
p->next = NULL;
*head = p;
}else{
for (; p->next != NULL && strcmp(p->word, word) != 0; p = p->next);
if (strcmp(p->word, word) == 0) {
p->count += 1;
}else{
p->next = (NODE*)malloc(sizeof(NODE));
p = p->next;
s = (char*)malloc(sizeof(strlen(word) + 1));
strcpy(s, word);
p->count = 1;
p->word = s;
p->next = NULL;
}
}
}
//gets words
char *getword(char *w)
{
int i = 0;
int c;
if (WORDLEN <= 0 || feof(stdin)){
return NULL;}
c = getchar();
while (c != EOF && ! isalpha(c)) {
c = getchar();
}
if (c == EOF)
return NULL;
while (isalpha(c)) {
if (i < WORDLEN - 1) {
w[i] = toupper(c);
i++;
}
c = getchar();
}
w[i] = '\0';
return w;
}
//print
void print(NODE *p) {
for (; p != NULL; p = p->next) {
printf("%s %d\n",p->word, p->count);
}
printf("\n");
}
//mergesort
void mergesort_node(NODE **head) {
NODE *p, *q, *newhead;
newhead = *head;
if (newhead == NULL || newhead->next == NULL) {
return;
}
split_list(newhead, &p, &q);
mergesort_node(&p);
mergesort_node(&q);
*head = merge_sorted_lists(p, q);
}
//Splits list in middle, using fast/slow method
void split_list(NODE *source, NODE **p, NODE **q) {
NODE *f, *s;
s = source;
f = source->next;
while (f != NULL) {
f = f->next;
if (f != NULL) {
s = s->next;
f = f->next;
}
}
*p = source;
*q = s->next;
s->next = NULL;
}
//Merges two individually sorted lists into one sorted list.
NODE *merge_sorted_lists(NODE *source1, NODE *source2) {
NODE *p, *q, *e, *newhead;
p = source1;
q = source2;
e = newhead = NULL;
while (p != NULL && q != NULL) {
if (p->count >= q->count) { //Compares the top items from the lists,
if (e == NULL) { //and puts the largest as next in the new list.
e = p;
newhead = e;
}else{
e->next = p;
e = e->next;
}
p = p->next;
}else if (p->count < q->count) {
if (e == NULL) {
e = q;
newhead = e;
}else{
e->next = q;
e = e->next;
}
q = q->next;
}
}
if (p == NULL) { //If one list ends, the rest of the other is added to the new list.
if (e == NULL) {
e = q;
newhead = e;
}else{
e->next = q;
}
}else if (q == NULL) {
if (e == NULL) {
e = p;
newhead = e;
}else{
e->next = p;
}
}
return newhead;
}
#包括
#包括
#包括
#包括
#包括
#定义WORDLEN 50
类型定义结构节点\u结构节点;
结构节点{
字符*字;
整数计数;
节点*下一步;
};
作废新节点(字符*字,节点**头);
作废打印(节点*p);
无效合并排序节点(节点**头);
无效拆分列表(节点*源、节点**p、节点**q);
节点*合并\排序\列表(节点*源1,节点*源2);
char*getword(char*word);
int main(int argc,char*argv[])
{
字符w[WORDLEN];
NODE*p=NULL;
while(getword(w)){
新节点(w和p);
}
合并排序节点(&p);
印刷品(p);
返回0;
}
//将单词添加到列表中
作废新节点(字符*字,节点**头){
节点*p=*头;
char*s;
if(p==NULL){
p=(NODE*)malloc(sizeof(NODE));
s=(char*)malloc(sizeof(strlen(word)+1));
strcpy(s,word);
p->word=s;
p->count=1;
p->next=NULL;
*水头=p;
}否则{
对于(;p->next!=NULL&&strcmp(p->word,word)!=0;p=p->next);
if(strcmp(p->word,word)==0){
p->count+=1;