473,779 Members | 1,873 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

how to implement doubly linked lists using only one pointer?

I'm reading MIT's book "Introducti on to Algorithms".
The following is one of the excercises from it:
<
10.2-8
Explain how to implement doubly linked lists using only one pointer
value np[x] per item instead of the usual two (next and prev). Assume
that all pointer values can be interpreted as k-bit integers, and
define np[x] to be np[x] = next[x] XOR prev[x], the k-bit
"exclusive-or" of next[x] and prev[x]. (The value NIL is represented by

0.) Be sure to describe what information is needed to access the head
of the list. Show how to implement the SEARCH, INSERT, and DELETE
operations on such a list. Also show how to reverse such a list in O(1)

time.
/>

Could anybody tell me how to solve it? Thank you!!!

Dec 23 '06 #1
8 3947
to************* @hotmail.com wrote:
I'm reading MIT's book "Introducti on to Algorithms".
The following is one of the excercises from it:
<
10.2-8
Explain how to implement doubly linked lists using only one pointer
value np[x] per item instead of the usual two (next and prev). Assume
that all pointer values can be interpreted as k-bit integers, and
define np[x] to be np[x] = next[x] XOR prev[x], the k-bit
"exclusive-or" of next[x] and prev[x]. (The value NIL is represented by

0.) Be sure to describe what information is needed to access the head
of the list. Show how to implement the SEARCH, INSERT, and DELETE
operations on such a list. Also show how to reverse such a list in O(1)

time.
/>

Could anybody tell me how to solve it? Thank you!!!
The book all but told you how to solve it.
If anything XOR'd with itself is zero, solve
the equation

np[x] = next[x] XOR prev[x]

for next[x].

To reverse the list, how would you revers a regular doubly
linked list with next and prev pointers?


--
Joe Seigh

When you get lemons, you make lemonade.
When you get hardware, you make software.
Dec 23 '06 #2
"to************ *@hotmail.com" <to************ *@hotmail.comwr ites:
Explain how to implement doubly linked lists using only one pointer
value np[x] per item instead of the usual two (next and prev). Assume
that all pointer values can be interpreted as k-bit integers, and
define np[x] to be np[x] = next[x] XOR prev[x], the k-bit
"exclusive-or" of next[x] and prev[x]. (The value NIL is represented by

0.) Be sure to describe what information is needed to access the head
of the list. Show how to implement the SEARCH, INSERT, and DELETE
operations on such a list. Also show how to reverse such a list in O(1)
time.
http://en.wikipedia.org/wiki/XOR_linked_list

--
Best regards, _ _
.o. | Liege of Serenly Enlightened Majesty of o' \,=./ `o
..o | Computer Science, Michal "mina86" Nazarewicz (o o)
ooo +--<mina86*tlen.pl >---<jid:mina86*chr ome.pl>--ooO--(_)--Ooo--
Dec 23 '06 #3
The book all but told you how to solve it.
If anything XOR'd with itself is zero, solve
the equation

np[x] = next[x] XOR prev[x]

for next[x].
That's the point! It seems that I know nothing about bit computations,
so silly questions are raised here.

Dec 24 '06 #4
If anything XOR'd with itself is zero, solve
the equation

np[x] = next[x] XOR prev[x]

for next[x].
how to solve that equation? i'm confused!

Dec 24 '06 #5
<to************ *@hotmail.comwr ote in message
news:11******** **************@ 73g2000cwn.goog legroups.com...
>If anything XOR'd with itself is zero, solve
the equation

np[x] = next[x] XOR prev[x]

for next[x].

how to solve that equation? i'm confused!
Play with bit math.

int X = 1;
int Y = 3;

now output X and Y, X or Y, X xor Y, etc... Find out why they come out the
way they do.
Dec 24 '06 #6
Play with bit math.
>
int X = 1;
int Y = 3;

now output X and Y, X or Y, X xor Y, etc... Find out why they come out the
way they do.
i've known that:
if A ^ B = C, then B = A ^ C, A = B^C.
but anyway how to solve that equation with only np[x] given?

Dec 24 '06 #7
Hello!

The original question is fun!

to************* @hotmail.com wrote:
>
i've known that:
if A ^ B = C, then B = A ^ C, A = B^C.
What's X ^ X? What's (X ^ X) ^ Y?
but anyway how to solve that equation with only np[x] given?
You won't have "only np[x] given".

The answer, as they say, is in the (original) question. It's all a
matter of knowing where to begin. You /will/ know where to begin. Just
use your head. Start at the beginning. After all, nothing will come
from nothing, as they say.

:-)

Simon

--
What happens if I mention Leader Kibo in my .signature?
Dec 29 '06 #8

<to************ *@hotmail.comwr ote in message
news:11******** **************@ 48g2000cwx.goog legroups.com...
>
i've known that:
if A ^ B = C, then B = A ^ C, A = B^C.
but anyway how to solve that equation with only np[x] given?
I think this is an easy way to communicate the concept:

Instead of XOR, use addition and subtraction (they are
essentially equivalent when it comes to implementing the
trick of this method, but much easier to comprehend).

First, every node has a field to hold the distance from its
previous-node to its next-node. Note that this is exactly
equal to the sum of the distances from its previous-node
to itself, and from itself to its next-node.

When you iterate, you need pointers to two successive
nodes. Call them A and B. It is easy to compute the
distance between the nodes (B-A), and from there it is
easy to compute the address of A's previous-node [A's
sum-of-distances is reduced by (B-A) and the remainder
applied as an offset to A's address] or of B's next-node
[left as an exercise].

- Risto -
Jan 2 '07 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
2211
by: surrealtrauma | last post by:
I want to ask what's the differences between doubly liked list and linear liked list, and also the circular doubly liked list in terms of implementation. THX
4
2932
by: dssuresh6 | last post by:
Whether browsing forward or backward can be done using a singly linked list. Is there any specific case where a doubly linked list is needed? For people who say that singly linked list allows traversal only in one direction, I would say that using appropriate loops/recursion, traversal in opposite direction is also possible. Then why the need for doubly linked list? --
8
4170
by: sudhirlko2001 | last post by:
How to swap two nodes of doubly Linklist
1
1799
by: drewy2k12 | last post by:
Heres the story, I have to create a doubly linked list for class, and i have no clue on how to do it, i can barely create a single linked list. It has to have both a head and a tail pointer, and each node in the list must contain two pointers, one pointing forward and one pointing backwards. Each node in the list will contain 3 data values: an item ID (string), a quantity (integer) and a price (float). The ID will contain only letters and...
2
898
by: murali | last post by:
Hi, I want to insert a node in an sorted doubly linked list of integers in ascending order. The list should not have duplicate nodes. I need an algorithm (assuming an object oriented language) bye
3
2671
by: maruf.syfullah | last post by:
Consider the following Class definitions: class AClass { int ai1; int ai2; public: CClass* c; AClass(){}
10
1979
by: kalar | last post by:
Hello. we have this struct and we must to make a linked list struct node { char name; char phone; struct node *prevName; // previous node alphabetically struct node *prevNumber; // previous node sorted by numbers struct node *nextName; // next node alphabetically struct node *nextNumber; // next node sorted by numbers } ;
4
1429
by: valt | last post by:
public class DLLNode { String Item; int Time; DLLNode Next; DLLNode Prev; public DLLNode(String newItem,int newTime,DLLNode newNext,DLLNode newPrev) { Item = newItem;
4
2941
by: arnuld | last post by:
This program follows from the section 6.5 of K&R2 where authors created a doubly-linked list using a binary-tree based approach. The only thing I have rewritten myself is the getword function. I am using it because there are many concepts involved in this program that I want to understand like: 1.) degree of efficiency of this program as compared to sort using hash-table based approach. 2.) Keeping the program well structured so...
0
9632
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9471
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10302
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10136
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10071
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8958
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7478
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5501
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2867
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.