You are given a linked list of N nodes. How would you prove or disprove that this is a Doubly linked list or not?
-
assume it is a normal list, i mean if it is a - singly linked list then each node has 2 fields - 1st is data field and 2nd is pointer to next node. doubly linked list then each node has 3 fields - 1st is pointer to previous node, 2nd is data field and 3rd is pointer to next node. PS: This is not a circular linked list. MethodsI thought of - 1) try to find out the structure of a node (I mean how many fields a node have)
-
Answer:
Since this question has been filed under under , I'm assuming this is a question about C. In that context this question doesn't make sense. Truthfully, I can't think of a context where it does make sense, but I'm hoping the explanation in C will show why. If you have a struct which represents a node in a doubly linked list you can "cast" any region of memory to that struct. The underlying region might not contain sensible data, but your C program will happily treat it like a doubly-linked list until something breaks. Likewise, if you start with a valid doubly-linked list there's nothing preventing you from inserting rubbish data, e.g., node->next = 0xFACEFEED;node->prev = 0xDEADBEEF; How do you differentiate between a region of memory that contains rubbish by accident and a region of memory that contains rubbish because your program inserted it there? Heck, how do you differentiate rubbish from non-rubbish? So neither "you are given N nodes" nor "prove that this is a doubly-linked list" make sense to me if you're talking about . What is a "node" if not a struct? If it's a struct, why can't we just look at its definition? If we're not talking about a struct, what are we talking about? A void * pointer? How do we know how many bytes one of your voodoo "nodes" takes up? At the end of the day your memory is just a bunch of undifferentiated 0s and 1s. The semantics of the data are defined by how our program interacts with it. A struct is just a convenient way for us to segment a larger region of memory into smaller, named chunks of a pre-determined width. In fact, one way to achieve polymorphism in C is to design clever structs such that the same region of memory can be cast to either struct and work as planned
Jesse Farmer at Quora Visit the source
Other answers
This is a general answer Say you have a linked list like:- Node1: NULL,1,2000 Node2: 1000,2,3000 Node3: 2000,3,4000 Node4: 3000,4,5000 and so on. Where first value is the address of previous node, 2nd value is the information field of the node and 3rd value is the address of the next node. Take two pointer arrays, say previous and this. The code below in a do while loop do { Step 1:Start with Node1 and capture values in array previous. Values like previous address(NULL in this case), current address(1000) and next address(2000). So previous array is NULL,1000,2000 Step 2: Move to the next node if Node1->NEXT!=NULL and capture values in this array. Values like previous address(1000), current address(2000) and next address(4000). So this array is 1000,2000,3000. Now compare previous array's and this array's, last two and first two pointers respectively. If they match implies node1 and node2 are linked. Similarly continue for other nodes. }while(NEXT pointer is not NULL); This is a very basic approach that i've followed in my answer provided we are checking a doubly linked list. I agree that conditions will have to be added to the above approach to counter for input errors like we are supplied with a single linked list, circular linked list, etc. I hope it gives you a basic algorithm for your problem.
Dhruv Singh
Related Q & A:
- How to display indexes for UITableView in a complex list?Best solution by User Experience
- How to add a contact to a specific list?Best solution by Stack Overflow
- How to block a friend in a friend list?Best solution by Yahoo! Answers
- How to traverse linked list in c++?Best solution by algolist.net
- How to delete a contact from a favorite list?Best solution by Yahoo! Answers
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.