my professor asked me a question , the question is to implement a singly linked list and when I create a new node , I must get the 2 contiguous address of this newly created node. for example this newly created node has address 0x6efe18 I must get the previous and next for this address in this case the next contiguous address is 0x6efe1c and previous contiguous address is 0x6efe14. I have assumed that the data is integer but I must handle any data. integer , float ,image or video whatever. I know this is so confused But that's it :(
Asked
Active
Viewed 184 times
-1
-
2Clarify it more please - make a [mcve] - demonstrating the issue you are having with the code you have written so far. C++ or assembly is quite a jump, so please decide that too. Are you sure the professor meant the next address, perhaps they meant the next and previous nodes? – Quimby Jan 25 '21 at 08:32
-
2"_What should I do to solve it using c++ or assembly ?_" - Design it on paper, then open an editor and start coding. We have no idea where you have problems implementing this. A singly linked list node could be as simple as `template
struct Node { Node* next; T data; };` – Ted Lyngmo Jan 25 '21 at 08:33 -
2This may be of use: https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list – eerorika Jan 25 '21 at 08:35
-
1Is that an exact quote of the question your professor asked? If not, post it exactly if it was in English, in case you're accidentally changing the meaning when you restate it in your own words. – Peter Cordes Jan 25 '21 at 08:56
-
Yes it's. And that's what makes me confused. – Hussein Sarea Jan 25 '21 at 09:05
-
1I'd ask for clarification from your professor. I have _one_ picture in my head that could be what he/she meant, but it's just a guess. – Ted Lyngmo Jan 25 '21 at 09:08
-
Tell me what your guess. It might be very useful for me – Hussein Sarea Jan 25 '21 at 09:11
-
I'm guessing the the linked list is supposed to be sorted (or else either the previous or next node after the insertion would always be `nullptr`). So, find the place where to insert the node and return a `std::pair
` where the first `Node*` is pointing at the previous `Node` and the second `Node*` is pointing at the next `Node`. It's just a wild guess though. – Ted Lyngmo Jan 25 '21 at 09:18 -
"get whatever the data in previous and next address of this newly created node" is barely grammatical, if at all. If it was "whatever the data *is*", it would be a clunky way of saying "the data" (whatever it might be). So I'm surprised that you'd have an assignment written in English worded this way. I get that even professors don't always have perfect English, but this case leaves significant ambiguity and lack of clarity. – Peter Cordes Jan 25 '21 at 09:22
-
I'm sure that this is still not clear to you , But I appreciate you taking the time to help me.Ted Lyngmo – Hussein Sarea Jan 25 '21 at 09:25
-
Peter Cordes ,Thank you for being so helpful! – Hussein Sarea Jan 25 '21 at 09:29
-
@HusseinSarea Here's [an implementation of how I interpret the question](https://godbolt.org/z/1ac8db). It's a wild guess though so don't pick too many ideas from it until you've confirmed what you're supposed to do. It's also lacking a lot (like a proper `forward_list` class template) so don't think you can use it as-is :-) – Ted Lyngmo Jan 25 '21 at 09:58
-
Ted Lyngmo Thanks man , I really appreciate this. I've asked my professor again, he just makes it more complicated. he said even if the data was an image or video I must get it . – Hussein Sarea Jan 25 '21 at 18:50
-
@HusseinSarea You're welcome! Yes, what the data actually _is_ shouldn't matter - but that doesn't clarify the question. The guess I made previously still holds and the implementation I made above could actually work. (Note: I don't see that you mention me unless you tag me with @ TedLyngmo (without the space between @ and my name) - i was just lucky to have this window still open) – Ted Lyngmo Jan 26 '21 at 08:50
-
But "_contiguous address_" sounds a bit strange. That's not how linked lists works. The implementation I made returns the address of the previous and next `Node` which I think makes more sense. – Ted Lyngmo Jan 26 '21 at 08:58
-
@TedLyngmo I really don't know what to say . I asked my professor again . he said that he wants to confuse me and see if i can get it .i told him if his question is about virtual and physical address . I was right. this is what he wants . but I do know that i can't do it . he asked me this question to see if i can solve it even with help of others . he didn't ask other students because he knows they wouldn't be able to do things like that . i really don't know what to do right now. – Hussein Sarea Jan 27 '21 at 00:16
-
@TedLyngmo He clarify the question . when i create a new node . the address that will be returned bythe new keyword is a virtual address , so he wants the physical address when i get the physical address let's say it's 0x6efe04 the previous address of it is 0x6efe00 and the next is 0x6efe08 . i have to get the data present there . i just assumed the addresses. – Hussein Sarea Jan 27 '21 at 00:25
-
@TedLyngmo thank you so much for being so kind . I can't thank you enough – Hussein Sarea Jan 27 '21 at 00:29
-
@HusseinSarea I see - The question just became even more complex :-) There's nothing in standard C++ that even requires virtual memory so whatever the solution is, it'll depend on the environment in which the program runs. Here's one question that deals with [virtual-to-physical address mapping](https://stackoverflow.com/questions/40292822/translate-virtual-address-to-physical-address). It's for a 16 bit system but the calculations looks sound both in the accepted answer and the answers that follows. – Ted Lyngmo Jan 27 '21 at 06:04
-
1@TedLyngmo Thanks again… your help made a huge difference :) – Hussein Sarea Jan 27 '21 at 18:13
1 Answers
1
It is not possible to get previous data in constant time in a singly linked list unless you are saving previous value since beginning in a variable.
You can only move forward(not back) in Singly linked list. So you have two options either get the previous data using a loop or to save it in a variable when you are traversing through the linked list.
Also the next pointer in this new node will be a null pointer.

Great Sage
- 46
- 5
-
I don't mean the previous node's data . Let me clarify it more , let's say you created a new node and this node is allocated in some memory address assume this address is 100 so I want whatever the data present in previous and next address (contiguous address of node 100 ) – Hussein Sarea Jan 25 '21 at 08:55
-
@HusseinSarea Still not clear to me what you want, but in any case if you are clarifying the question you should edit the question, not add a comment. – john Jan 25 '21 at 08:57