XOR双链表...打印元素 [英] XOR doubly linked list ...printing elements

查看:79
本文介绍了XOR双链表...打印元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<pre>#include<stdio.h>
typedef struct node_type{
	int data;
	struct node_type *link;
}node;

typedef node *list;
list head,temp,tail,next;

list xor(list a, list b)
{
	return (list) ((unsigned) a ^ (unsigned) b);
}

int main()
{
	char ch;
	int n;
	list head,tail,temp,prev,next,temp1;
	printf("Enter y:\n");
	scanf("%c",&ch);
	printf("Enter data:\n");
	scanf("%d",&n);
	head=tail=NULL;
	temp=(list)malloc(sizeof(node));
	temp->link=NULL;
	temp->data=n;

	head=tail=temp;
	printf("Enter again char:\n");
	scanf("\n%c",&ch);
	while(ch=='y'){
		temp1=(list)malloc(sizeof(node));
		printf("Again input data:\n");
		scanf("%d",&n);
		temp1->data=n;
		temp1->link=xor (temp->link,tail->link);
		tail=temp1;
		printf("again input char:\n");
		scanf("\n%c",&ch);
	}
	printf("\nThe list\n");


	prev=NULL;
	temp=tail;
	while(temp!=NULL)
	{
		printf("%d ",temp->data);
		next=xor( prev,temp->link);

		prev=temp;

		temp=next;

	}

}





我的尝试:



专家,这里的代码片段似乎有误。我在这里可以做什么:



What I have tried:

Hi expert, here the code fragment that seems wrong. What can I do here:

prev=NULL;
temp=tail;
while(temp!=NULL)
{
    printf("%d ",temp->data);
    next=xor( prev,temp->link);
    prev=temp;
    temp=next;
}







因为如果你输入1 2 3 4 5 44,那么输出只有44个。除了最后一个数字,每次输出都丢失了。




Because if u input 1 2 3 4 5 44 , then the output comes as only 44. Every times all the output got missing except the last number.

推荐答案

你猜错了。错误发生在另一个地方:

You guess was wrong. The error is at another place:
temp=(list)malloc(sizeof(node)); // here you overwrite temp!
printf("Again input data:\n");
scanf("%d",&n);
temp->data=n;
temp->link=xor (temp->link,tail->link); // and here you try to use the old temp!
tail=temp;



无论如何,temp->链接的计算都是错误的。重新阅读您提到的文章,并注意插入一个新节点,您也需要更改相邻节点的链接字段!


And the computation of temp->link is wrong anyway. Re-read the article that you mentioned and note that insert a new node you need to change the link fields of the adjacent nodes as well!


使用调试器查看到底发生了什么 - 但坦率地说,我不认为XORing指针并试图将中间结果用作有效链接是一个好主意。

是的,你可以使用XOR来交换值,但你需要使用超过保持良好数据的一个XOR:使用XOR交换两个变量BetterExplained [ ^ ] ,我不会用它作为指针!

只需异步两次指针就不会产生好结果。如果要交换指针,请使用临时中间指针:

Use the debugger to see exactly what is happening - but frankly, I don;t think XORing pointers and trying to use the intermediate results as valid links is a good idea.
Yes, you can use XOR to swap values, but you need to use more than one XOR to keep good data: Swap two variables using XOR | BetterExplained[^], and I wouldn't use it for pointers!
Just XORing two pointers once is not going to produce a good result. If you want to swap pointers, use a temporary intermediate pointer:
pTemp = pOne;
pOne = pTwo;
pTwo = pTemp;

它更清楚到底发生了什么,它不会产生无用的中间值。

It's a lot clearer what exactly is going on, and it doesn't generate useless intermediate values.


你应该学会尽快使用调试器。而不是猜测你的代码在做什么,现在是时候看到你的代码执行并确保它完成你期望的。



调试器允许你跟踪执行逐行检查变量,你会看到有一点它会停止你所期望的。

在Visual Studio 2010中掌握调试 - 初学者指南 [ ^ ]

http://docs.oracle.com/javase/7/docs/technotes/tools/windows/jdb.html [ ^ ]

https://www.jetbrains.com/idea/help/debugging-your-first-java-application.html [ ^ ]



解决方案1仍适用于此处。使用调试器。

调试器将准确显示您的代码正在执行的操作,您将确保正确保存列表。

确保在构建时正确创建指针清单。



建议:拿一张纸,记下填写清单时每个字段必须包含的内容,然后将其与问题联系起来(仅限于看看你是否理解算法。
You should learn to use the debugger as soon as possible. Rather than guessing what your code is doing, It is time to see your code executing and ensuring that it does what you expect.

The debugger allow you to follow the execution line by line, inspect variables and you will see that there is a point where it stop doing what you expect.
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
http://docs.oracle.com/javase/7/docs/technotes/tools/windows/jdb.html[^]
https://www.jetbrains.com/idea/help/debugging-your-first-java-application.html[^]

Solution 1 still apply here. Use the debugger.
The debugger will show you exactly what your code is doing, you will ensure the list is saved correctly.
Make sure that pointers are created correctly while building the list.

Advice: take a sheet of paper and write down what every fields must contain as you fill the list and a&dd it to the question (just to see if you understand the algorithm.


这篇关于XOR双链表...打印元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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