有人可以帮我解决吗?
此代码会创建2个不同的列表mylist
,其中的单词没有特定的顺序,yourlist
其中的单词通过insertincr按字母顺序链接到列表。
从txt
文件中读取单词,如果您想测试创建单词。
现在我想做以下任务:
我正在尝试编写function
,根据new list
yourlist
创建argument
,新列表是基于lenght这个词的排序版本,输入列表(yourlist
)。然后打印两个列表。
我想在不使用任何其他现成功能的情况下这样做(我想做recursively
。
我无法想办法让它发挥作用。
以下代码的输出是:
HELLOTHERETHEREMYFRIENDAT用空列表
用空列表调用
00690DE8
用空列表调用
复制
AT:FRIEND:你好:我:那里:
00690F50
你好:那里:那里:我的:朋友:在:
DiffrentWords == Listlenght:5
TotalWords == ListlenghtplusOCCURS == TextWords:6
和我调用createlenbasedlst
后的输出应为:
AT:MY:你好:那里:朋友:
#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);
}
}
}
答案 0 :(得分:2)
在这种情况下,使用case 0:
TabFragment tabFragment = new TabFragment();
tabFragment.setArguments(bundle);
return tabFragment;
循环代替while
循环。只需浏览源列表for
并插入yourlist
,无需另外排序。
newlist
的额外typedef
令人困惑,您可以使用List
或选择更好的名称,例如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;
}
和copy
)中使用递归函数。在实践中应该避免这种情况。
insert
<小时/> 修复你自己的功能
#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;
}