gpt4 book ai didi

c - 尝试使用函数基于另一个列表创建排序列表

转载 作者:太空宇宙 更新时间:2023-11-04 07:55:06 25 4
gpt4 key购买 nike

谁能帮我解决一下?

此代码创建了 2 个不同的列表 mylist,其中单词没有特定的顺序,而 yourlist 中的单词通过 insertincr 按字母顺序链接到列表。

单词是从 txt 文件中读取的,所以如果您想测试,请创建一个。

现在我要完成以下任务:

我正在尝试编写一个函数,它根据 yourlist 创建一个新列表,这是它的参数,新列表是基于输入列表 (yourlist) 的单词长度的排序版本。然后打印两个列表。

我想在不使用任何其他现成函数的情况下执行此操作,并且我(想递归地执行此操作

我想不出任何方法让它工作。

下面代码的输出是:

HELLOTHERETHEREMYFRIENDAT调用空列表

用空列表调用

00690DE8

用空列表调用

重复

在: friend :你好:我:那里:

00690F50

你好:那里:那里:我的: friend :在:

DiffrentWords==LiSTLenght: 5

TotalWords==LiSTLenghtplusOCCURS==TextWords: 6

我调用 createlenbasedlst 后的输出应该是:

在:我的:你好:那里: friend :

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct box {
char value[20] ;
struct box * next ;
int occurs;
} ;
typedef struct box Box;
typedef Box * List;

Box * createbox(char a[]);
void appendref( Box ** start_ptrptr,Box * node_ptr);
void appendrefrec( Box ** start_ptrptr,Box * node_ptr);
void insertincr( List * list_ptr, Box * node_ptr);
void report(Box *mylist_ptr);
void printall(FILE *);
int lenlist(List lst);
int lenmylist(List lst);

int main( ) {
List mylist = NULL;
List yourlist = NULL;
//List newlist =NULL;
char buffer[20]="";
FILE *text;
text=fopen("TEXT.txt","rt");
printall(text);
rewind(text);
for(;fscanf(text,"%s",buffer)!=EOF;){
Box * new_ptr ;
Box * your_new_ptr;
new_ptr = createbox(buffer) ;
your_new_ptr = createbox(buffer) ;

appendref( & mylist , new_ptr) ;
insertincr(& yourlist, your_new_ptr);


}

report(yourlist);

report(mylist);

printf("\n\nDiffrentWords==Listlenght:% d",lenlist(yourlist));
printf("\n\nTotalWords==ListlenghtplusOCCURS==TextWords:% d",lenmylist(yourlist));

return 0;
}

Box * createbox(char a[20]) {
Box * newBox_ptr;
newBox_ptr = malloc( sizeof (Box) ) ;
strcpy((newBox_ptr -> value),a);
newBox_ptr -> next = NULL;
newBox_ptr -> occurs= 0;
return newBox_ptr;
}


void report(Box *mylist_ptr) {
if (mylist_ptr == NULL) {
return ;
}

printf("%s: ", mylist_ptr->value);
report(mylist_ptr->next);

return ;
}

void appendref( Box ** start_ptrptr,
Box * node_ptr
) {
Box * iter = * start_ptrptr;
if ( iter == NULL ) {
printf("called with empty list\n");
*start_ptrptr = node_ptr;
return ; }
for ( ; iter->next != NULL ;
iter = iter->next ) ;

iter->next = node_ptr;

return ;
}

void appendrefrec( Box ** start_ptrptr,
Box * node_ptr) {
Box * iter = * start_ptrptr;

if ( iter == NULL ) {
printf("called with empty list\n");
*start_ptrptr = node_ptr;
return ;
}

appendrefrec( & (iter->next) , node_ptr);

return ;
}


void insertincr( List * list_ptr,
Box * node_ptr
) {
Box * iter = * list_ptr;
if ( iter == NULL ) {
printf("called with empty list\n");
* list_ptr = node_ptr;
return ;
}
if (strcmp(iter->value,node_ptr->value)==0){
printf("duplicated\n");
iter->occurs++;
free(node_ptr);
return ;
}
if (strcmp(iter->value,node_ptr ->value)<0) {
insertincr( &(iter->next), node_ptr);
return ;
}

if (strcmp(iter->value,node_ptr ->value)>0) {

node_ptr -> next = *list_ptr;
*list_ptr = node_ptr;

return ;
}
}
void printall(FILE *text){
char word[20]="";
for(;fscanf(text,"%s",word)!=EOF;){
printf("%s",word);
}
return;
}

int lenlist(List lst){
if(lst==NULL){
return 0;
}
return 1+lenlist(lst->next);/*or return lst?(1+lenlist(lst->next)):0;*/
}

int lenmylist(List lst){
if(lst==NULL){
return 0;
}
return (1+lenmylist(lst->next)+(lst->occurs));
}


//MY ATTEMP
void createlenbasedlst(List lst,List *newlist){
if (*newlist==NULL){
*newlist=createbox(lst->value);
(*newlist)->occurs=lst->occurs;
createlenbasedlst(lst->next,newlist);
return;
}
Box *iter = *newlist;
if (iter!=NULL&&strlen(iter->value)<strlen(lst->value))
createlenbasedlst(lst,&(iter->next));
else
if (lst!=NULL&&strlen(iter->value)>=strlen(lst->value)){
Box *temp = *newlist;
*newlist=createbox(lst->next->value);
(*newlist)->occurs=lst->next->occurs;
(*newlist)->next=temp;
createlenbasedlst(lst->next,&(iter->next));
}
return;
}

return;
}
//attemp 2
void createlenbasedlst(List *newlist,List lst){
Koutaki *iter=NULL;
while(lst&&iter==NULL){
Koutaki *temp = *newlist;
*newlist = createkoutaki(lst->value);
*newlist->occurs=lst->occurs;
*newlist->next = temp;
lst=lst->next;
}
if (iter==NULL){
iter=*newlist;
lst=iter->next;
}
while(iter){
if(strlen(iter->value)<strlen(lst->value)){
createlenbasedlst(&iter,lst->next);
}
}
}

最佳答案

在这种情况下使用while 循环而不是for 循环。只需遍历源列表 yourlist 并插入到 newlist 而无需额外排序。

List 的附加 typedef 令人困惑,您可以只使用 Box* 或选择一个更好的名称,如 node*

void copy(Box* source, Box **dest)
{
if(!source)
return;

Box *ptr = malloc(sizeof(Box));
strcpy(ptr->value, source->value);
ptr->occurs = source->occurs;
ptr->next = *dest;
*dest = ptr;

copy(source->next, dest);
}

int main()
{
List yourlist = NULL;
char buffer[20] = "";
FILE *text = fopen("TEXT.txt", "rt");

printall(text);
rewind(text);
while(fscanf(text, "%s", buffer) != EOF)
{
Box *ptr = createbox(buffer);
insertincr(&yourlist, ptr);
}

List newlist = NULL;
copy(yourlist, &newlist);

report(yourlist);
report(newlist);
return 0;
}

这是带有排序插入和递归函数的清理版本复制列表。请注意,这是在另一个递归函数(copyinsert)中使用递归函数。这在实践中应该避免。

#include <stdio.h>
#include <stdlib.h>

typedef struct node_t
{
char value[20];
struct node_t* next;
int occurs;
} node;

void insert(node **list, node* ptr)
{
node *iter = *list;
if(iter == NULL)
{
*list = ptr;
return;
}

//call strcmp only once, use `test` value several times
int test = strcmp(iter->value, ptr->value);

if(test == 0)
{
iter->occurs++;
free(ptr);
}
else if(test < 0)
{
insert(&iter->next, ptr);
}
else if(test > 0)
{
ptr->next = *list;
*list = ptr;
}
}

//recursive copy
void copy(node **dest, node* source)
{
if(!source)
return;
node *ptr = malloc(sizeof(node));
strcpy(ptr->value, source->value);
ptr->occurs = source->occurs;
ptr->next = NULL;

//recursive insert, don't expand this function
insert(dest, ptr);

copy(dest, source->next);
}

void report(node *list)
{
for(node *it = list; it; it = it->next)
printf("%s, ", it->value);
printf("\n");
}

int main()
{
//call srand() too
node* list1 = NULL;
node* list2 = NULL;
char buffer[20];
for(int i = 0; i < 10; i++)
{
sprintf(buffer, "%d", rand() % 100);
node *ptr = malloc(sizeof(node));
strcpy(ptr->value, buffer);
ptr->next = NULL;
ptr->occurs = 0;
insert(&list1, ptr);
}

copy(&list2, list1);

report(list1);
report(list2);
return 0;
}


修复你自己的功能

void insert_sorted(List *list, Box* ptr)
{
Box *iter = *list;
if(iter == NULL)
{
*list = ptr;
return;
}

//sort by length:
int test = strlen(iter->value) - strlen(ptr->value);

//sort by length:
//int test = strcmp(iter->value, ptr->value);

if(test == 0)
{
iter->occurs++;
free(ptr);
}
else if(test < 0)
{
insert_sorted(&iter->next, ptr);
}
else if(test > 0)
{
ptr->next = *list;
*list = ptr;
}
}

//recursive function:
void createlenbasedlst(List *dest, Box* source)
{
if(!source)
return;
Box *ptr = malloc(sizeof(Box));
strcpy(ptr->value, source->value);
ptr->occurs = source->occurs;
ptr->next = NULL;

//another recursive function. Don't expand in to this function
insert_sorted(dest, ptr);

createlenbasedlst(dest, source->next);
}

关于c - 尝试使用函数基于另一个列表创建排序列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50749593/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com