如何修复链表打印/ insert_end函数? [英] How can I fix my linked list printing/insert_end function?

查看:77
本文介绍了如何修复链表打印/ insert_end函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经让我的链表很好地处理整数,但是,将它转换为通用链表并存储字符串会导致逻辑错误我无法破解。这是键入Hello和Goodbye的输出:



列表再见再见。



即它打印最后一个字两次。



谢谢你。



我尝试了什么:



I have gotten my linked list working with integers fine, however, converting it to a generic linked list and storing strings results in a logic error i cannot crack. Here is the output to typing "Hello" and "Goodbye":

List is Goodbye Goodbye.

I.e it prints the last word twice.

Thankyou.

What I have tried:

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

/*Typedef declarations*/

typedef struct Node
{
	void* data;
	struct Node* next;
}Node;

typedef struct 
{
	Node* head;
}Linked_List;

/*------------------------------------------------------------------*/

Linked_List* create_empty_list(void)
{
	Linked_List* new_head; 
	new_head = (Linked_List*)malloc(sizeof(Linked_List)); 
	new_head->head = NULL;
	
	return new_head;
}

/*------------------------------------------------------------------*/
	
Node* insert_end(Linked_List* list, void* data)
{
	Node* new_node;
	Node* temp;
	new_node = (Node*)malloc(sizeof(Node));
	/*new_node->data = (char**)malloc(sizeof(char*)); need to free this*/
	new_node->data = data;
	new_node->next = NULL;
	if(list->head == NULL)
	{
		list->head = new_node;
	}
	else
	{
		temp = list->head;
		while(temp->next != NULL)
		{
			temp = temp->next;
		}
		
		temp->next = new_node;
	}
	return(list->head);
}
	
/*------------------------------------------------------------------*/	

void display(Linked_List* list)
{
	Node* temp;
	temp = list->head;
	while(temp != NULL)
	{
		printf("%s ", (char*)(temp->data));
		temp = temp->next;
	}
}

/*------------------------------------------------------------------*/
		
void free_list(Linked_List* list)
{
	while(list->head != NULL)
	{
		Node* temp = list->head;
		list->head = temp->next;
		free(temp);
	}
}

/*------------------------------------------------------------------*/
/*Main code to test linked list*/	
	
int main(void)
{
	
	/*int num_elements, i;*/
	char str[20];
	int i;
	
	Linked_List* char_list = create_empty_list();
	
	for(i=0;i<2;i++)
	{
		printf("Enter a word\n");
		scanf("%s", str);
		char_list->head = insert_end(char_list, str);
	}
	
	printf("The list is: ");
	display(char_list);
	printf("\n\n");
	
	
	free_list(char_list);
	
	return(0);
	
}

推荐答案

现在是时候开始查看代码运行时发生的事情了!我们不能为你做到这一点,但你有一个很棒的工具可以帮助你做到这一点:调试器。



调试器的作用是让你慢慢地执行你的代码,这样你就可以检查变量并查看内容,并弄清楚它为什么会这样做。



首先查看你的代码确实如此,以及它与你想要的有何不同。这很重要,因为它可以为您提供有关其原因的信息。例如,如果程序旨在让用户输入一个数字并将其翻倍并打印答案,那么如果输入/输出是这样的:

Now is the time to start looking at what is happening while the code is running! And we can't do that for you, but you have a wonderful tool available to you to help you do that: the debugger.

What the debugger does is lets you execute your code slowly, so you can examine variables and look at the content, and work out why it does what it does.

Start by looking at what your code does do, and how that differs from what you wanted. This is important, because it give you information as to why it's doing it. For example, if a program is intended to let the user enter a number and it doubles it and prints the answer, then if the input / output was like this:
Input   Expected output    Actual output
  1            2                 1
  2            4                 4
  3            6                 9
  4            8                16

然后很明显问题出在将它加倍的位 - 它不会将自身加到自身上,或者将它乘以2,它会将它自身相乘并返回输入的平方。

所以,你可以查看代码和很明显,它在某处:

Then it's fairly obvious that the problem is with the bit which doubles it - it's not adding itself to itself, or multiplying it by 2, it's multiplying it by itself and returning the square of the input.
So with that, you can look at the code and it's obvious that it's somewhere here:

int Double(int value)
   {
   return value * value;
   }



一旦你知道可能出现的问题,就开始使用调试器找出原因。在你的线上设一个断点:


Once you have an idea what might be going wrong, start using the debugger to find out why. Put a breakpoint on your line:

display(char_list);



并运行你的应用程序。当调试器到达该行时,它将停止,并为您提供控制权。看看你的清单:它包含什么?看起来它是否按正确的顺序保留了正确数量的元素?你想要的所有字符串都在吗?如果是这样,那么可能是打印功能有问题。如果没有,那就是列表构造代码 - 所以停止调试,将断点移到前面,然后查找列表构造错误的原因。


想想每个代码中的行应该在执行之前执行,并将其与使用Step over按钮依次执行每一行时实际执行的操作进行比较。它符合您的期望吗?如果是这样,请转到下一行。

如果没有,为什么不呢?它有什么不同?



我无法给你如何设置断点,单步代码等的明确指示,因为我不知道你是哪个IDE正在使用 - 但是一个快速谷歌的调试器和你的IDE名称将找到你所需要的。



这是一项技能,它是值得的发展,因为它可以帮助您在现实世界和发展中。和所有技能一样,它只会通过使用来改善!

试一试,看看你能找到什么!


and run your app. When the debugger reaches that line, it will stop, and give you control. Look at your list: what does it contain? Does it look like it holds the right number of elements, in the right order? Are all the strings you expected there? If so, then it's probably the print function that has a problem. If not, it's the list construction code - so stop the debug, move the breakpoint to earlier, and look for why the list is built wrong.

Think about what each line in the code should do before you execute it, and compare that to what it actually did when you use the "Step over" button to execute each line in turn. Did it do what you expect? If so, move on to the next line.
If not, why not? How does it differ?

I can't give you explicit instructions on how to set breakpoints, single step code and so forth, because I have no idea which IDE you are using - but a quick Google of "debugger" and your IDE name will find you what you need.

This is a skill, and it's one which is well worth developing as it helps you in the real world as well as in development. And like all skills, it only improves by use!
Give it a try, and see what you can find out!


这篇关于如何修复链表打印/ insert_end函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆